Posted By

DrewDouglass on 09/23/09


Tagged

php


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

vas3k
Nettuts
mogwi
laurentzziu
qubestream


Get Web Page With cURL


 / Published in: PHP
 

URL: http://www.php.net/manual/en/function.curl-setopt-array.php

Helpful for remembering useful CURLOPT constants. Taken from an example in the php manual comments.

  1. <?php
  2.  
  3. function get_web_page( $url,$curl_data )
  4. {
  5. $options = array(
  6. CURLOPT_RETURNTRANSFER => true, // return web page
  7. CURLOPT_HEADER => false, // don't return headers
  8. CURLOPT_FOLLOWLOCATION => true, // follow redirects
  9. CURLOPT_ENCODING => "", // handle all encodings
  10. CURLOPT_USERAGENT => "spider", // who am i
  11. CURLOPT_AUTOREFERER => true, // set referer on redirect
  12. CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
  13. CURLOPT_TIMEOUT => 120, // timeout on response
  14. CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
  15. CURLOPT_POST => 1, // i am sending post data
  16. CURLOPT_POSTFIELDS => $curl_data, // this are my post vars
  17. CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
  18. CURLOPT_SSL_VERIFYPEER => false, //
  19. CURLOPT_VERBOSE => 1 //
  20. );
  21.  
  22. $ch = curl_init($url);
  23. curl_setopt_array($ch,$options);
  24. $content = curl_exec($ch);
  25. $err = curl_errno($ch);
  26. $errmsg = curl_error($ch) ;
  27. $header = curl_getinfo($ch);
  28. curl_close($ch);
  29.  
  30. // $header['errno'] = $err;
  31. // $header['errmsg'] = $errmsg;
  32. // $header['content'] = $content;
  33. return $header;
  34. }
  35.  
  36. $curl_data = "var1=60&var2=test";
  37. $url = "https://www.example.com";
  38. $response = get_web_page($url,$curl_data);
  39.  
  40. print '<pre>';
  41. print_r($response);
  42.  
  43. ?>

Report this snippet  

You need to login to post a comment.