View Full Version : AJAX Client-side Get Code Of Another Site

10-04-2010, 03:52 AM

Is it possible to create a client-side script to get code of another website, similar to PHP get_file_contents but from client-side not server side?

If so can you please tell me how or provide some code to use ajax to return the code of another website into a variable please.

Any help would be greatly appreciated.

10-04-2010, 09:22 AM
It isn't possible. Client side (browser) security restrictions will not allow it.

10-04-2010, 05:41 PM
As John said, it's intentionally blocked. (It's theoretically possible and there's no reason that it won't work except that browsers block it.)
There are two possible ways around this. However, in most cases this is a problem anyway, since you may not have permission from the external site. In some cases (if you own both, for example) it may be fine, so I will post the workarounds anyway.

1. I believe that there is some way to create a document that allows this in your server: crossdomain.xml. It may not work in all cases, and you might need the same file on both servers. The intent of that is to allow this sort of thing if you have permission. I don't know any more information about this and the one time I tried it I didn't get it to work-- then I find another approach and didn't need to do it any more. Try google or maybe someone here will know more than I do.

2. Since you are already using PHP, you could easily get around this by doing an Ajax request to a PHP page on your server that uses file_get_contents() to grab the external website. There may be problems with this, such as if the website doesn't respond quickly or if there is a typo (or intentional bad request) from Javascript. Verify the URL first if you can.
This should work in all cases. But still be careful and think about whether this is really the right approach to solving your problem.