Skip to content

Instantly share code, notes, and snippets.

@stojg stojg/curl.php
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.