Possible to use Javascript to get data from other sites? - php

Is it possible for a web page using Javascript to get data from another website? In my case I want to get it for calculations and graphing a chart. But I'm not sure if this is possible or not due to security concerns. If it is considered a no no but there is a work around I would appreciate being told the work around. I don't want to have to gather this information on the server side if possible.
Any and all help is appreciated.

Learn about JSONP format and cross-site requests (http://en.wikipedia.org/wiki/JSON#JSONP).
You may need to use the "PHP-proxy" script at your server side which will get the information from the websites and provide it to yours Javascript.

The only reliable way is to let "your" webserver act as a proxy. In PHP you can use curl() to fire a HTTP request to an external site and then just echo the response.

You can't pull data from another server due to the same origin policy. You can do some tricks to get around it, such as putting the URL in a <script> tag, but in your case it wouldn't work for just parsing HTML.
Use simple_dom_html, to parse your data server side. it is much easier than doing it in JavaScript anyways.
A simple way you might be able to do this is to use an inline iframe. If the web page you are getting the data from has no headers, or you can isolate the data being pulled in (to say an image or SWF), this might work.

cross-domain javascript used to be impossible, using a (php-)proxy was a workaround for that.
jsonp changes this entirely, it allows to request javascript from another server (if it has an API that supports jsonp, a lot of the bigger webplayers like google, twitter, yahoo, ... do), specifying the callback-function in your code that needs to be triggered to act on the response.
the response in javascript will contain:
a call to a callback-function you defined
the actual payload as a javascript-object.
frameworks like jquery offer easy support for jsonp out of the box.
once you have the raw data you could tie into google chart tools to create graphs on the fly and insert them in your webapp.

Also worth considering is support for XMLHttpRequest Access Control which is support in some modern browsers.
If the service provider that you are trying to access via a web page has this set up, it is a very simple call to XMLHttpRequest and you will get access to the resources on that site without the need for JSONP (especially useful for requests that are not GET, i.e. POST, HEAD etc)

Related

I have implemented Adobe Analytic for my client. I read the response from network panel of browser through php

I want to fetch the response from adobe analytics which I get in network panel of browser and display in it the page. This there any way to read the response through php. I don't have access to core files. i work as third party implementation team.
The short answer is you can't. php is server-side. Assuming you did the standard javascript Adobe Analytics implementation; well that's client-side. Your server can't see that stuff.
The longer answer is redeploying Adobe Analytics through a server-side implementation. Basically you setup the code but have it point to your server and then you forward it to adobe (proxying), and so now it is exposed to you server-side.
The alternative answer is depending on what it is you are actually trying to accomplish, you can make use of Adobe Analytics' s.registerPostTrackCallback function. Basically it lets you register a callback function to be called after every s.t or s.tl call, and it gives you the full/final request URL sent to the AA collection server. You can then make an AJAX request to pass it to your server and do whatever with it. Or since you mentioned displaying it on page, maybe consider using javascript to render it on the page? But if you're looking for actual response stuff (headers, content) well you're out of luck on this option.
The other other alternative answer is.. this almost sounds like you are looking to make some kind of browser plugin? If so, then on a plugin/extension level, the request/response stuff (including header stuff) is exposed on that level. But again, ultimately this is really a client-side solution..
But first step back and more clearly define what it is you are trying to do. Or if you've done that already, then try (more clearly) to convey that here.

Javascript to PHP rather then using Ajax?

I need to call a PHP Script and Run (a function) on the Server .. by calling from client side by using Javascript. I know only Ajax Call from Javascript.
Is there any other standardized way to communicate from Javascript to PHP?
Please correct me if i'm wroing. Is XMLRPC an another approach?
Nothing that is well supported or practical.
Ajax is just shorthand for "Making an HTTP request from JavaScript without leaving the page".
PHP is heavily geared towards being a server side web language (so it is optimised for being accessed over HTTP). Browsers are focused on accessing content over HTTP.
No, you use a XmlHttpRequest (that is, I assume you don't want the user to experience any sort of page refresh).
To work cross browser easily I'd recommend using a library like jQuery which handles everything for you, everything is nicely encapsulated and abstracted so you don't need to worry about any of the details. That way calling your script becomes extremely easy.
XMLHttpRequest is the best way, as far as I know, but there are other techniques too. There is the old school way some sites still use. Using hidden iframes and sending request through it.You create an iframe with javascript, append it with 0 width and height and the request the php file. The output must be script that somehow communicates with the parent window script.

Getting remote data - PHP or javascript?

What method would you choose for retrieving tweets from Twitter, for example?
Using json with jQuery (like http://juitter.com/), where client does the request.
using PHP, for eg. CURL, where server does the request
What are the advantages/disadvantages of both?
Using server side approach, you may benefit from caching facilities, and having an overall faster page load (no roundtrips to twitter, or any other 3rd party service on each request).
The other benefit of the server side approach is, users coming from countries (or even companies) which twitter is filtered in, won't see a big empty pane, or even worse a mangled layout. And even when twitter goes bad, you have a cache somewhere to show the recent tweets.
But the most obvious advantage of client side approach is being hassle free.
There is a distinction between the both approaches. When using server side outside HTTP requests, you retrieve the data to the server. If you want that data to be seen to client, you have to send it with the next request the client does to your server side. With server side requests you can do native HTTP requests and cross-domain requests also.
Client side cross-domain requests retrieve the data directly to the client. You can display that to the client in the same instance when the request returns data but if you want the data on the server side (storing the tweets in db) you have to send them from the client, back to the server. Javascript can't do cross-domain XHR requests. In order to do that, you (or the libs that do that) make some workarounds: Using iframes, using including of JS files that already have the info that you need etc.
If you need to consume a web service, I advice on using the backend as the service client and either use timely pull from the client side, or use some of the "comet" techniques.
I think it depends on how frequently the stream you are pulling in gets updated. If it isvery frequently then JS is better, because you can continually run the call without refreshing the page, whereas not so frequently, and you can pull all the data in using CURL and use JS for animations.
Client-side requests are better when you have to do many requests (e.g. for use by a public site) as you lower your server load / avoid a bottleneck, possibly benefitting from content-delivery networks (CDN) caching the requests on behalf of your web clients and you move some of the liability from yourself to the users of your site as they are actually accessing the third-party API (which may be more relevant if you have really many requests; some API terms of use even limit the number of requests per time unit so that client-side requests are the only option for big sites).
Server-side requests have the advantage of not requiring JavaScript to be enabled on the client side and can also be logged easily for statistical purposes or processed further.
using server as "MITM" you may cache, alter, insert data from 3rd part before published to your users. Your users may dislike it, though...
I would prefer client side for displaying tweets rather than the server side because of the following reasons:-
Since tweets are displayed directly on the browser in real time, using jQuery complements well with this scenario. This refreshes the page quickly along with your tweets (using multiple asynchronous ajax calls ofcourse).
Well when you take the server side, it takes an extra roundtrip to the server and back to the client. Do you need that trip when all you need is to display just tweets??. Tweets are just social messages (say hello!!!) stuff and they are not supposed to contain any harmful stuff.
You generally take the server side approach when you need to do data validation before displaying on the browser. For tweets, it's not necessary.
//for client making request
jQuery.ajax({
url:'target_url',
type:'get',
dataType:'jsonp',
success:function(data){
//do something with data
}
});
//note: dataType is jsonp not JSON as calling twitter from your domain would
//not be allowed by browser(cross domain request are not allowed)
//so u have use jsonp.

Simulating Browser Clicks In PHP

I want to write a PHP script that performs a routine task in a web app I use. I am trying to figure out the easiest way to submit a form, click a link, and get some information. What's the easiest way to do this (keeping the session open, etc.).
Javascript would be a better solution than PHP. You can use it in tandem with PHP to submit a form that references the same page, ie. <form method='index.php' action='post'>
If method is GET then you ought to be able to work it out form the URLs of a few real world attempts.
It POST then you are probably SOL unless it's your own web page./app and you know what $_POST it expects ... unless you find a tool to snoop your HTTP traffic and get the POST info from observing a few real wrold examples.
You can use CURL in PHP to simulate submitting data, clicked links, etc., I suppose, but a client-side scripting language like Javascript--as opposed to a server-side language like PHP--is more suited to what you're describing. I'd need more info to give you a specific example.
You will not be able directly emulate those events in PHP as web apps use Javascript on the client side and PHP is a different language and operates on the server side.
Firstly, I would see if there is an open API available for the web app you're wondering about, e.g. Gmail: http://code.google.com/apis/gmail/ . Not all APIs can do what the web app can do, so you'll need to check the documentation to make sure the API does what you want and has an easy way to interface with PHP.
The other option is to essentially reverse engineer how the web app communicates with it's server. Most all web apps operate by sending POST or GET HTTP data in some sort of serialized format like XML, JSON or text. You can use something like the Firebug add-on for Firefox to view POST/GET data. If you know what the server sends to the client and what the client sends to the server, you can essentially write a script using something like CURL to emulate the client in PHP instead of JavaScript. This would take quite a bit of work and probably involves a lot of trail & error.

How to use soap in javascript

I am trying to use SOAP in javascript but i am not getting how to start it.
Here is the code sample i write in PHP and it works fine. I want to write this code in Javascript. In following code i call one api from a www.example.com and for calling certain api we require to pass some parameters.
$soapClient = new SoapClient("https://www.example.com/WSDL");
$param_sh = array( );
$header = new SoapHeader('http://somesite.com/action/', 'user_credential', $param_sh);
$soapClient->__setSoapHeaders(array($header))
$param = array("with some parameter");
$contents = $soapClient->__call("name_of_method",array($param));
print($contents);
Now, after more than 5 years since this question was asked, lots of stuff has happened to Javascript and web in general, so:
you can send cross-origin requests and modern browsers are 100% OK with that (if the server you're sending them to allows CORS, of course)
there are lots of libraries both for server and client side javascript.
there are lots of questions on the topic on StackOverflow.
Assuming you're talking about in-browser Javascript, you won't be able to use SOAP. Browsers obey something called the Same Origin Policy, which says (loosely) that you can't make cross domain requests from javascript. That means you can only make requests to a SOAP service (or any HTTP request) if it's on the same domain that the browser page is currently on. Because of this limitation/feature, no one ever went to a lot of trouble to implement a SOAP client in Javascript (although they probably do exist).
Your best bet is to make your SOAP calls via PHP, and then make AJAX requests from your web page (via javascript) to the PHP pages that make the actual SOAP request.
JavaScript doesn't have a SOAP library out of the box, though you can google around and find them, e.g., here. Not that this won't do anything to work around the limitations #Alan cites. The web services still need to be on the origin server.

Categories