Skip to content

Instantly share code, notes, and snippets.

@plasticbrain
Created September 11, 2012 01:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plasticbrain/3695243 to your computer and use it in GitHub Desktop.
Save plasticbrain/3695243 to your computer and use it in GitHub Desktop.
PHP: Using cURL
<?php
// Initialize cURL
$ch = curl_init();
// The URL to load
curl_setopt($ch, CURLOPT_URL, 'http://www.url-to-load.com');
// Uncomment the following two lines to send POST data
// curl_setopt($ch,CURLOPT_POST, count($post_data_array));
// curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($post_data_array));
// Fail on errors?
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
// Timeout after X seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// Set a custom useragent
// curl_setopt($ch, CURLOPT_USERAGENT, "custom user agent");
// Do not include headers in the response
curl_setopt($ch, CURLOPT_HEADER, 0);
// Uncomment the following 2 lines to ONLY return the headers (Good for debugging)
// curl_setopt($ch, CURLOPT_HEADER, 1); // header will be at output
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
// Uncomment to allow cURL to follow any redirects
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Some servers (like Lighttpd) will not process the curl request without this
// header and will return error code 417 instead. Apache does not need it,
// but it is safe to use it there as well.
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
// Read the response 64000 byte chunks
curl_setopt($ch, CURLOPT_BUFFERSIZE, 64000);
// Return the result to a variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
// Get the results and the HTTP status code
$results = @curl_exec($ch);
$http_status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Be sure to close the connection
@curl_close($ch);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment