Home > General, Web Development > PHP Proxy Script for cross-domain requests

PHP Proxy Script for cross-domain requests

I wrote this php-proxy-script and have been using for different purpose (loading cross-domain data like text, xml, swf, image etc or for YouTube REST API from Flash-clients). Lots of folks keep asking about asking about proxy-scripts in forums, now they can download the code and use :)

Download the proxy.php.txt

Usage:-

http://yourserver.com/proxy.php?url=<url_encoded_desitnation_url>[&mimeType=<mimeType>]

Examples:-

To load XML/Text:
http://yourserver.com/proxy.php?url=http%3A//abdulqabiz.com/blog/index.xml

To load a SWF (binary-data):
http://yourserver.com/proxy.php?url=http%3A//abdulqabiz.com/files/some.swf&mimeType=application/x-shockwave-flash

Technorati tags: , ,
Categories: General, Web Development Tags:
  1. June 1st, 2007 at 12:17 | #1

    How about without using curl and relying just on he php fopen. I use this often for tackling any same-origin / cross-domain issues since the fopen can open any remote address ,no strings attached .
    I’ve briefly talked about it over at demo here .
    Keep Clicking,
    Bhasker V Kode,
    returnable.org

  2. June 1st, 2007 at 21:39 | #2

    @Bhaskar: I use both approaches, CURL gives more control over things… I never tried, but can you handle HTTP 30x redirects etc with fopen technique?
    -abdul

  3. Jiten
    July 21st, 2007 at 13:38 | #3

    Thank you, but…. what do I write on my Action Script (SWF File) aaand what do I write exactly on the proxy.php file ? please, just explain with detail this and I will appreciate, tahnks a lot,
    Sw. Jiten

  4. August 10th, 2007 at 04:13 | #4

    Do I misunderstand anything or does this proxy script also opens up your server to the world to be, possibly, misused for things like hiding one’s identity, i.e. the IP? I don’t have a good feeling with such a script.

  5. August 20th, 2007 at 23:16 | #5

    I had issues with this script (and others) returning 0 for the bytesTotal in flash. Basically, the Content-Length header was absent from the response. By simply adding
    header(”Content-length: “.strlen($response)) before the echo, it resolved the issue. I don’t know if there is a more appropriate fix to account for character encoding, etc, but it seems to work.

  6. August 20th, 2007 at 23:25 | #6

    @Schimmi: Well, if you can add some checks there (like who is referring your script) and allow the access to whitelisted clients (served from your domain)… I think, you can totally make it used applications from same-domain….So it would not be open to world. Yeah above script doesn’t have those things..
    -abdul

  7. August 20th, 2007 at 23:31 | #7

    @Lanny: Thanks for that, I would update it my code also…I need to find a way to return correct content-length for all content-type (text/binary/gzip etc)…
    Thanks
    -abdul

  8. Lucas
    October 2nd, 2007 at 16:48 | #8

    God bless you! I was tearing my hair out. Works like a charm – but you knew that…

  9. October 4th, 2007 at 00:16 | #9

    The PHP proxy script have support of GET and POST solicitations?
    Falopa!

  10. October 8th, 2007 at 05:47 | #10

    WOW! thanks for sharing the script. excellent work

  11. October 25th, 2007 at 01:27 | #11

    Here is a ASP Proxy Script.

    <!– Code starts Below this line –>
    <%
    set objHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
    strURL = Request("url")"
    objHttp.open "GET", strURL, False
    objHttp.Send

    If objHttp.status = 200 Then
        Response.Expires = 90
        Response.ContentType = Request("mimeType")
        Response.BinaryWrite objHttp.responseBody
        set objHttp = Nothing
    End If
    %>
    <!– Code ends above this line –>

    Usage:

    http://yourserver.com/proxy.asp?url=&lt;url_encoded_desitnation_url&gt;&mimeType=&lt;mimeType&gt;

    Example:

    http://yourserver.com/proxy.asp?url=http%3A//www.templatesoft.com/images/16596.jpg&mimeType=image/jpg

     

  12. Inder
    November 29th, 2007 at 18:28 | #12

    Thank you very much for this PHP script…. It really solved my big problem. Once again Thanks

  13. December 27th, 2007 at 11:48 | #13

    Nice script gonna try my hand with it…
    regards..
    mohnkhan
    http://www.mohitech.com

  14. January 27th, 2008 at 15:46 | #14

    Will this work with displaying YouTube videos?

  15. January 27th, 2008 at 21:49 | #15
  16. Jay
    January 31st, 2008 at 07:15 | #16

    Thanks for this script.
    It helped me to solve my problem.
    While implementing this, I found out two minor problems and possible solutions
    that I would like to share with you guys:

    1) If current($_POST) is empty (blank), the condition (while) becomes false. The
    subsequent post fields cannot be processed (not included in $postvars).

    while ($element = current($_POST)) {
    $postvars .= key($_POST).’=’.$element.’&';
    next($_POST);
    }

    Solution:
    while ($key = key($_POST)) {
    $postvars .= $key.’=’.urlencode(current ($_POST)).’&';
    next($_POST);
    }

    2) I think urlencode() should be used as shown below:

    $postvars .= $key.’=’.urlencode(current ($_POST)).’&';

    I hope this helps.

  17. February 5th, 2008 at 04:43 | #17

    Really tanks for that.

  18. March 17th, 2008 at 04:45 | #18

    sure.. nice code, butt…
    unhappily images are not stored in cache..

  19. Eddie
    May 7th, 2008 at 22:38 | #19

    Hello
    I am using your proxy.php script to grab iGoogle weather in flash. I have php enabled and configured on our IIS 6 server, but nothing loads when going to the page. Do you have any ideas what could be causing this? Thanks

  20. CB
    May 9th, 2008 at 08:23 | #20

    This looks like a simple modification of http://developer.yahoo.com/javascript/samples/proxy/php_proxy_simple.txt but with the original author’s name conveniently removed (and not retained for giving proper credit???)

  21. Anthony
    July 1st, 2008 at 20:56 | #21

    I have changed the source file to point to the proxy.php file in the same directory, and exported the swf. The swf cannot load the data. If i try to look at the proxy.php file directly, i just get errors. Any suggestions?

  22. Alex
    October 10th, 2008 at 20:10 | #22

    Excuse me, but i need a little help. i want to use your proxy for cross-domain request. i tryed it to make a request from a java Requestbuilder with an xml file as content and an POST as method, but my php isnt good and i cant get the content from the request to use ist in your proxy. some advices?

  23. Marissa
    November 18th, 2008 at 19:34 | #23

    I am using this script for flash and google calendar and it works pretty great. the only problem is that it seems to change the sort order of google calendar. the feed I’m trying to get is https://www.google.com/calendar/feeds/doug%40dougmishkin.com/public/full?futureevents=true&orderby=starttime&sortorder=ascending&max-results=5 but it defaults to the orderby default.
    Any ideas?

  24. Maria
    December 23rd, 2008 at 00:51 | #24

    You should make this able so that people can get on myspace.com from here.

  25. February 13th, 2009 at 08:35 | #25

    Totally brilliant, elegant and simple. Thanks so much for your work on this.

  26. Avery
    March 4th, 2009 at 11:43 | #26

    Fantastic. Absolutly fantastic. Thank you so much for this excellent little file.
    Life saver!

  27. jaris
    March 5th, 2009 at 19:54 | #27

    I have tried the script to access a distant flv file. It works, but I have to wait a lot of time to see the video. Have you an idea to have a progressive download of the file.

  28. March 7th, 2009 at 01:48 | #28

    Hi Guys
    Thanks for feedback.
    @jaris Yeah, this script can do better, if tweaked. I can imaging, for progressive video you would need to have logic so it can buffer the output. Unfortunately, I don’t have time right now to add or test that.
    If you figure out the fix, please let me know.
    Thanks
    -abdul

  29. jayp
    April 3rd, 2009 at 05:42 | #29

    Thanks!
    I didn’t set mime-type and everything but MSIE worked fine. So if MSIE not working, make sure mime type is specified after url.

  30. Chris
    April 23rd, 2009 at 01:10 | #30

    Hi I’m trying to use this with Google Calendar and AS3 but its throwing up an IOError. The AS3 causing the problem is:
    var xmlLoader:URLLoader = new URLLoader( new URLRequest(’proxy.php?url=’+ xmlURL) );
    Any ideas? I have the proxy.php file in the same folder as the swf.
    @Alex: If you’re putting the calendar events into an array, you could try sorting that using myArray.sortOn(’myProperty’, Array.DESCENDING);

  31. TELLO
    May 6th, 2009 at 12:25 | #31

    Hi Abdul,
    i have problem. I use the script to get XML datas from other domain. I use the url urlencoded like your advice. It works in FireFox perfect, but not in IE. I tested IE 6, 7 and 8, but no result.
    You write it works in IE with mime type after URL. So i need XML, what must i put on the end that it works in IE, because i guess it is my problem…
    THANKS

  32. Tibin mathew
    June 16th, 2009 at 14:06 | #32

    // If it’s a POST, put the POST data in the body
    if ($_POST['https://newport.ingrammicro.com/mustang']) {
    echo(”hai”);
    $postvars = “2.014-01811114-018111MDt8772CG4339pW824D7441″;
    while ($element = current($_POST)) {
    $postvars .= key($_POST).’=’.$element.’&’;
    next($_POST);
    }
    curl_setopt ($session, CURLOPT_POST, true);
    curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
    }

    here in your code its saying if its a post put the post data in the body, is it the html body of my php page or any thing else, plz say about this, where should i give my xml document to post to the url????