Archive for category curl

PHP/CURL

cURL is used to interact with remote URLs without needing a user to initiate the process (e.g. by clicking on a form submit button). cURL is useful for submitting HTTP POST/PUT/DELETE requests when dealing with web services. PHP has inbuilt cURL support since PHP 4.0.2 using cURL's libcurl library. Find out more about PHP/CURL -- using libcurl with PHP here.

A fullset of cURL options and what they mean can be found in
PHP's manual entry for curl_setopt().

PHP:
  1. //Contains encoded string to pass along for basic authentication purposes
  2. $auth_token = base64_encode($username . '-' . $password);
  3.  
  4. //Target URL - the URL you want to submit a form to
  5. $target_url = 'http://www.remotesite.com/post_target.php';
  6.  
  7. //Create  a new cURL handle
  8. //Passing the target URL to curl_init allows you to bypass the call curl_setopt($ch, CURLOPT_URL, $target_url);
  9. $ch = curl_init($target_url);
  10.  
  11. //Tell the handler that the info is to be sent using an HTTP POST request
  12. curl_setopt($ch, CURLOPT_POST, true);
  13.  
  14. //Set other relevant headers.  Place each header as an array element
  15. //An alternative to building the Authorization header is to use curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
  16. $headers = array('Authorization: Basic ' . $auth_token,
  17.                  'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3');
  18. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  19.  
  20. //Pass the POST fields - be sure to urlencode your value strings (hint: http_build_query() will do this for you; PHP5)
  21. //Below we assume values have already been posted to this script and kept in $_POST.  We have validated the submission and
  22. // are now posting the same values to a remote URL
  23. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
  24.  
  25. //When we execute the handle, we want curl_exec() to return to a string rather than directly outputting it
  26. curl_setopt($ch, CURLOPT_RETURNTRANSER, true);
  27.  
  28. //Don't use a cached connection - explicitly create a new one
  29. curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
  30.  
  31. //Fail if cannot connect to the target server within 5 seconds
  32. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  33.  
  34. //If the target server returns a redirect request using the "Location:" header directive, then follow it.
  35. //To prevent recursive redirects, only do a max of 5 follows
  36. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  37. curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
  38.  
  39. //Let's now execute the handler
  40. //Because CURLOPT_RETURNTRANSFER is true, we need to capture the return value of curl_exec()
  41. $response_data = curl_exec($ch);
  42.  
  43. //Was there an error?
  44. //curl_errno() returns the error code
  45. //curl_error() returns a clear text message for the last cURL operation
  46. if (curl_errno($ch)> 0){
  47.     die('There was a cURL error: ' . curl_error($ch));
  48. } else {
  49.     //Close the handler and release resources
  50.     curl_close($ch);
  51. }
  52.  
  53.  
  54. //Now do something with your data
  55. return myProcessingFunction($data);

No Comments