Last active
August 29, 2015 14:21
-
-
Save stojg/9f43597aee54655b78b0 to your computer and use it in GitHub Desktop.
simple curl template
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 | |
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