Common usage code example of cURL library in a wave of PHP _ php instance

Source: Internet
Author: User
This article mainly introduces common code examples of cURL library usage in PHP. cURL in the Unix-like world is built in PHP, which makes Linux and MacOS users very friendly, if you need a friend, you can refer to the php CURL which is a good function. Below are some of the best clips to add to your favorites.

0. Basic example
General process:

$ To_url = $ _ GET ['url']; print_r ($ _ GET); if (substr ($ to_url, 0, 1) = '/') {$ to_url = "http://www.amazon.com ". $ to_url;} echo $ to_url; // initialize $ ch = curl_init (); // set options, including URLcurl_setopt ($ ch, CURLOPT_URL, $ to_url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_HEADER, 0); // execute and obtain the HTML document content $ output = curl_exec ($ ch ); $ output = preg_replace ("# href = \" # "," href = \ "http://in2.qq-ex.com/amazon.php? Url = ", $ output); // release curl handle curl_close ($ ch); echo $ output; // specify the proxy address curl_setopt ($ ch, CURLOPT_PROXY, '11. 11.11.11: 8080 '); // if necessary, provide the user name and password curl_setopt ($ ch, CURLOPT_PROXYUSERPWD, 'user: pass ');

1. test whether the website is running normally.

 if (isDomainAvailible('http://gz.itownet.cn'))   {     echo "Up and running!";   }   else   {     echo "Woops, nothing found there.";   }    //returns true, if domain is availible, false if not   function isDomainAvailible($domain)   {     //check, if a valid url is provided     if(!filter_var($domain, FILTER_VALIDATE_URL))     {       return false;     }      //initialize curl     $curlInit = curl_init($domain);     curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);     curl_setopt($curlInit,CURLOPT_HEADER,true);     curl_setopt($curlInit,CURLOPT_NOBODY,true);     curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);      //get answer     $response = curl_exec($curlInit);      curl_close($curlInit);      if ($response) return true;      return false;   } 

2. file_gecontents operations can be replaced.

function file_get_contents_curl($url) {  $ch = curl_init();   curl_setopt($ch, CURLOPT_HEADER, 0);  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.  curl_setopt($ch, CURLOPT_URL, $url);   $data = curl_exec($ch);  curl_close($ch);   return $data; } 

3. Save all images of a website

 function getImages($html) {  $matches = array();  $regex = '~http://somedomain.com/images/(.*?)\.jpg~i';  preg_match_all($regex, $html, $matches);  foreach ($matches[1] as $img) {   saveImg($img);  } }  function saveImg($name) {  $url = 'http://somedomain.com/images/'.$name.'.jpg';  $data = get_data($url);  file_put_contents('photos/'.$name.'.jpg', $data); }  $i = 1; $l = 101;  while ($i < $l) {  $html = get_data('http://somedomain.com/id/'.$i.'/');  getImages($html);  $i += 1; } 

4. FTP application

// open a file pointer $file = fopen("/path/to/file", "r");  // the url contains most of the info needed $url = "ftp://username:password@mydomain.com:21/path/to/new/file";  $ch = curl_init();  curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  // upload related options curl_setopt($ch, CURLOPT_UPLOAD, 1); curl_setopt($ch, CURLOPT_INFILE, $fp); curl_setopt($ch, CURLOPT_INFILESIZE, filesize("/path/to/file"));  // set for ASCII mode (e.g. text files) curl_setopt($ch, CURLOPT_FTPASCII, 1);  $output = curl_exec($ch); curl_close($ch); 

5. Use curl to send JSON data

$data = array("name" => "Hagrid", "age" => "36");                                   $data_string = json_encode($data);                                             $ch = curl_init('http://api.local/rest/users');                                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                        'Content-Type: application/json',                                           'Content-Length: ' . strlen($data_string))                                     );                                                             $result = curl_exec($ch); 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.