Skip to content

Instantly share code, notes, and snippets.

@stojg
Last active August 29, 2015 14:21
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 stojg/9f43597aee54655b78b0 to your computer and use it in GitHub Desktop.
Save stojg/9f43597aee54655b78b0 to your computer and use it in GitHub Desktop.
simple curl template
<?php
function get($url) {
$timeout = 2;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
if (curl_error($ch)) {
printf("%s\ncurl error: %s".PHP_EOL, $url, curl_error($ch));
exit(1);
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($code != 200) {
printf("non 200 response: %d".PHP_EOL.'%s'.PHP_EOL, $code, $result);
exit(2);
}
curl_close($ch);
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment