Skip to content

Instantly share code, notes, and snippets.

@Barneybook
Created June 18, 2014 04:53
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 Barneybook/0a7f6166954832a79ef2 to your computer and use it in GitHub Desktop.
Save Barneybook/0a7f6166954832a79ef2 to your computer and use it in GitHub Desktop.
curl sample code
<?php // RAY_curl_example.php
error_reporting(E_ALL);
function my_curl($url, $timeout=2, $error_report=FALSE)
{
$curl = curl_init();
// HEADERS FROM FIREFOX - APPEARS TO BE A BROWSER REFERRED BY GOOGLE
$header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // browsers keep this blank.
// SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
// RUN THE CURL REQUEST AND GET THE RESULTS
$htm = curl_exec($curl);
$err = curl_errno($curl);
$inf = curl_getinfo($curl);
curl_close($curl);
// ON FAILURE
if (!$htm)
{
// PROCESS ERRORS HERE
if ($error_report)
{
echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
var_dump($inf);
}
return FALSE;
}
// ON SUCCESS
return $htm;
}
// USAGE EXAMPLE
$url = "http://twitter.com/Ray_Paseur";
$htm = my_curl($url);
if (!$htm) die("NO $url");
// SHOW WHAT WE GOT
echo "<pre>";
echo htmlentities($htm);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment