crossdomain gateway or “How can I get that data!”

A lot of sites today are very open with their APIs allowing users from other sites to retrieve their data, but sometimes you’ll come across websites that may not have that same open access. When their sites limit the access that other sites can retrieve data from via the crossdomain file it’s frustrating to say the least.

This issue also comes into play when you’re working on a site that may have strict crossdomain policies and you want to be able to test on a local server and call this data.

So what to do? How can we access this data easily when we don’t have the ability to simply change the crossdomain file?

It’s known that you can use php to act as a gateway to pull data from other sites, but what if you have a url that you need to call and pass various parameters through to? You could just use the $_GET[‘myParam’] to retrieve each and every variable you may need to reference but that can get cumbersome if you start needing to set lots of parameters or it changes from time to time.

So here’s what I’ve found to be the easiest way to access this. With this script I’ve gone ahead and placed the base url within the php but you could also pass the url to the php in the params and just run a check for that param name probably.

$key) {
 $paramStr = $paramStr . $param . '=' . str_replace(" ", "+", $key) . '&' ;
}
$url = 'http://www.otherDomain.com/script?'.$paramStr;

$post_data = $HTTP_RAW_POST_DATA;
$header[] = "Content-type: text/xml";
$header[] = "Content-length: ".strlen($post_data);

// create a new cURL resource
$ch = curl_init( $url );
// set Return Transfer, Timout and Htttp Header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

// grab URL and pass it to the browser
$xmlData = curl_exec($ch);

if (curl_errno($ch)) {
	echo curl_error($ch);
} else {
	curl_close($ch);
	echo $xmlData;
}

?>

Now you can use this and it will retrieve the data from the other site no prob. I’m sure there could be an even easier way to do this so if you know comment and let me know!


Posted

in

, ,

by

Tags:

Comments

Leave a Reply