I'm not sure of how interesting this will be to my blog following, however this is a problem I continuously come up against in my javascript programming and even with lots of google it seems to take me ages to find a solution. So the problem is this - you have an application that needs to make ajax requests to an external server. To take a real example maybe you want to find out geographical details about the town of Billericay. To do this you can query the url
http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=billericay To get this working on your website you need to workaround
cross site scripting security as your website is unlikely to live on the same address as the script you need to call (in this example googleapis.com) To do this you need a proxy script. You create a file called proxy.php which looks at the external eg. the one above and prints the page contents. Now your query url becomes proxy.php?feed=
http://ajax.googleapis.com/ajax/services/search/local%3Fv%3D1.0%26q%3Dbillericay - so you access the googleapi url through a webpage living on your own website. As a result, you now you get exactly the same result as the one above produces however now it lives on say jonrobson.me.uk/proxy.php so any page that also lives on jonrobson.me.uk can now access that data through javascript.
Anyway I'm probably talking rubbish now, like I usually am, and getting too techy. The problem is that most companies operate something called a proxy server which prevents employees from getting to pornographic websites, scan outbound content, log usage amongst many things. The proxy is usually configured on your browser level,and as a result your normal proxy.php script which does not use that proxy server typically cannot access any external websites without that proxy. So your code is now useless.
After a day and a half of agony, my colleague Simon McManus finally found a way that our proxy.php script could access websites using our internal networks server via curl using php. I'm pretty sure I will lose this code and that this code is of interest to others so I now paste it below for your viewing pleasure.
Hope it helps you like it helped me..
$feed = $_REQUEST['feed'];
$url = parse_url($feed);
// security checks
$url = $feed;
// Attempt different proxy methods.
$url = str_replace(" ","%20",$url);
if(curl_version())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); /* Max redirection to follow */
curl_setopt($ch, CURLOPT_PROXY, "your.proxy.server:8080");
curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
curl_exec($ch);
$info = curl_getinfo($ch);
curl_error($ch);
// HANDLE 301 redirects
if($info['http_code']==301)
{
//echo $info['url'];
}
curl_close($ch);
exit;
} elseif(readfile())
{
readfile($feed);
} else
{
$params = array('http' => array(
'method' => 'GET',
'header'=> 'accept:application/json',
'content' => $data));
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
//echo $response = stream_get_contents($fp);
}
// in some situtations this needs to replace the above line.
//echo $response = readExternalFile($feed);
?>
Thanks for reading this far. Did I bore you or interest you? Let me get better at doing the latter and focus more on working on the good stuff...