Created
January 26, 2012 18:08
-
-
Save maxchirkov/1684099 to your computer and use it in GitHub Desktop.
HTTP XML Request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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