« YouTube RSS feeds don't validate | Main | Yahoo! Avatar World - Yet another Flash application »

May 31, 2007

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: , ,

Posted by Abdul Qabiz at May 31, 2007 04:42 PM

Comments

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

Posted by: Bhasker V Kode at June 1, 2007 12:17 PM

@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

Posted by: Abdul Qabiz at June 1, 2007 09:39 PM

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

Posted by: Jiten at July 21, 2007 01:38 PM

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.

Posted by: Schimmi at August 10, 2007 04:13 AM

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.

Posted by: Lanny at August 20, 2007 11:16 PM

@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

Posted by: Abdul Qabiz at August 20, 2007 11:25 PM

@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

Posted by: Abdul Qabiz at August 20, 2007 11:31 PM

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

Posted by: Lucas at October 2, 2007 04:48 PM

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

Falopa!

Posted by: John-Henrique at October 4, 2007 12:16 AM

WOW! thanks for sharing the script. excellent work

Posted by: Learn PHP at October 8, 2007 05:47 AM

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=<url_encoded_desitnation_url>[&mimeType=<mimeType>]

Example:

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


 

Posted by: AL at October 25, 2007 01:27 AM


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

Posted by: Inder at November 29, 2007 06:28 PM

Nice script gonna try my hand with it...
regards..

mohnkhan
http://www.mohitech.com

Posted by: Mohiuddin Khan Inamdar at December 27, 2007 11:48 AM

Will this work with displaying YouTube videos?

Posted by: Carl at January 27, 2008 03:46 PM

@Carl: You can use other code available on this blog for YouTube.

http://www.abdulqabiz.com/blog/archives/general/update_getting_youtu.php
http://www.abdulqabiz.com/blog/archives/flash_and_actionscript/constructing_youtube_1.php
http://www.abdulqabiz.com/blog/archives/flash_and_actionscript/youtube_actionscript.php

Read the comments on all of above posts, you would fine more details and code.

-abdul

Posted by: Abdul Qabiz at January 27, 2008 09:49 PM

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.

Posted by: Jay at January 31, 2008 07:15 AM

Really tanks for that.

Posted by: Marcos at February 5, 2008 04:43 AM

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

Posted by: kevin at March 17, 2008 04:45 AM

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

Posted by: Eddie at May 7, 2008 10:38 PM

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???)

Posted by: CB at May 9, 2008 08:23 AM

Post a comment




Remember Me?


Please enter the security code you see here

(you may use HTML tags for style)