Skip to content

Instantly share code, notes, and snippets.

@maxchirkov
Created January 26, 2012 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxchirkov/1684099 to your computer and use it in GitHub Desktop.
Save maxchirkov/1684099 to your computer and use it in GitHub Desktop.
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