Created
July 11, 2019 19:12
-
-
Save Toxiapo/0e90191eeda2ed32c6b4627fde049bff to your computer and use it in GitHub Desktop.
curl helper function
This file contains hidden or 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
function fetchUrl($uri,$method=false) { | |
$handle = curl_init(); | |
curl_setopt($handle, CURLOPT_URL, $uri); | |
curl_setopt($handle, CURLOPT_POST, $method); | |
curl_setopt($handle, CURLOPT_BINARYTRANSFER, false); | |
curl_setopt($handle, CURLOPT_HEADER, true); | |
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 10); | |
$response = curl_exec($handle); | |
$hlength = curl_getinfo($handle, CURLINFO_HEADER_SIZE); | |
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); | |
$body = substr($response, $hlength); | |
// If HTTP response is not 200, throw exception | |
if ($httpCode != 200) { | |
throw new Exception($httpCode); | |
} | |
return $body; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment