Skip to content

Instantly share code, notes, and snippets.

@Toxiapo
Created July 11, 2019 19:12
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 Toxiapo/0e90191eeda2ed32c6b4627fde049bff to your computer and use it in GitHub Desktop.
Save Toxiapo/0e90191eeda2ed32c6b4627fde049bff to your computer and use it in GitHub Desktop.
curl helper function
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