Skip to content

Instantly share code, notes, and snippets.

@maxchirkov
Created January 26, 2012 18:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
HTTP XML Request
<?php
/**
* Simple HTTP request for API usage - converts XML into object
*/
function http_xml($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if(!$code){
preg_match('@^(?:http://)?([^/]+)@i', $url, $matches);
$host = $matches[1];
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
srp_debug( 'Something went wrong. No data is being returned from ' . $matches[0] . '.' );
return;
}elseif($code != 200){
$message = 'Request to URL: "' . $url . '" failed. Response code: ' . $code;
srp_debug( $message );
return;
}
$xml = @simplexml_load_string($result);
return $xml;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment