0

i want to get data from other sites using javascript executed from my website.

Brett Zamir
  • 13,309
  • 6
  • 48
  • 73
Speedy Wap
  • 478
  • 4
  • 7
  • 18

6 Answers6

5

The PHPJS website has some nice conversions of PHP functions into Javascript.

pritaeas
  • 2,073
  • 5
  • 36
  • 48
4

In general, unless they expose the data with JSON-P, you can't thanks to the security considerations imposed by the same origin policy.

Recent browsers support a permissions system where a remote site can allow JavaScript running on a remote site to make a request. Flash provides a similar system, so can act as an intermediary. Both of these require the cooperation of the remote site.

The usual work around is to use a proxy service, either running on your own system (so JS makes the request to the same server, which fetches the data from the remote site) or a third-party service like YQL.

Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
2

Javascript is limited by the same-domain security policy. The only way to get data from other sites is to use JSONP or build a proxy on your own host that lets you curl content from other sites.

Klemen Slavič
  • 19,431
  • 3
  • 32
  • 43
  • I'm pretty sure there's other PHP ways besides `curl` also, but I'm too lazy to look for them - `file_get_contents` of course springs to mind. – Ben Jan 03 '11 at 08:45
  • Sure, but that's just shorthand for `curl`. Not part of the question though, the user asked whether he/she could fetch content from other domains. – Klemen Slavič Jan 04 '11 at 13:44
1

Use jQuery:

$.post( 'http://some.website.com/file.js', function(result){
    alert(result);
});

You may not fetch anything but JavaScript or JSON.

Or try this answer: How do I send a cross-domain POST request via JavaScript?

Community
  • 1
  • 1
St.Woland
  • 5,297
  • 28
  • 30
1

It has to be done server side - send an ajax request, run the PHP you want, and check the responseText property to see the results.

Community
  • 1
  • 1
Ben
  • 51,262
  • 47
  • 169
  • 217
1

That really depends on what you mean by "data". Try using AJAX if its just for simple requests.

jerluc
  • 4,038
  • 2
  • 24
  • 44