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  in flash, flex applications).

People keep asking about proxy-scripts in forums to manage various things (cross-domain AJAX issues). Download the proxy.php.txt, and use to solve such problems. You can also fork it on Github, and improve it.

Usage:

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

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

<?php
/*
* PHP Proxy
* Responds to both HTTP GET and POST requests
* Author: Abdul Qabiz
* Created On: March 31st, 2006
* Last Modified: Feb 22, 2015
*/
// Get the url of to be proxied
// Is it a POST or a GET?
$url = ($_POST['url']) ? $_POST['url'] : $_GET['url'];
$headers = ($_POST['headers']) ? $_POST['headers'] : $_GET['headers'];
$mimeType =($_POST['mimeType']) ? $_POST['mimeType'] : $_GET['mimeType'];
//Start the Curl session
$session = curl_init($url);
// If it's a POST, put the POST data in the body
if ($_POST['url']) {
$postvars = '';
while ($element = current($_POST)) {
$postvars .= key($_POST).'='.$element.'&';
next($_POST);
}
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $postvars);
}
// Don't return HTTP headers. Do return the contents of the call
curl_setopt($session, CURLOPT_HEADER, ($headers == "true") ? true : false);
curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Make the call
$response = curl_exec($session);
if ($mimeType != "") {
// The web service returns XML. Set the Content-Type appropriately
header("Content-Type: ".$mimeType);
}
echo $response;
curl_close($session);
?>
view raw proxy.php hosted with ❤ by GitHub

Technorati tags: , ,