一波PHP中cURL庫的常見用法程式碼範例_php執行個體

來源:互聯網
上載者:User

php 的CURL是不錯的功能,下面收藏幾段不錯的片段

0、基本例子
一般流程:

$to_url=$_GET['url'];print_r($_GET);if(substr($to_url,0,1)=='/'){ $to_url="http://www.amazon.com".$to_url;}echo $to_url;//初始化$ch = curl_init();//設定選項,包括URLcurl_setopt($ch, CURLOPT_URL, $to_url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HEADER, 0);//執行並擷取HTML文檔內容$output = curl_exec($ch);$output=preg_replace("#href=\"#","href=\"http://in2.qq-ex.com/amazon.php?url=",$output);// 釋放curl控制代碼curl_close($ch);echo $output;// 指定Proxy 位址curl_setopt($ch, CURLOPT_PROXY, '11.11.11.11:8080');// 如果需要的話,提供使用者名稱和密碼curl_setopt($ch, CURLOPT_PROXYUSERPWD,'user:pass');

1、測試網站是否運行正常

 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的操作

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、儲存某個網站下的所有圖片

 function getImages($html) {  $matches = array();  $regex = '~yun_qi_img/(.&~i';  preg_match_all($regex, $html, $matches);  foreach ($matches[1] as $img) {   saveImg($img);  } }  function saveImg($name) {  $url = 'yun_qi_img/'.$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應用

// 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、使用curl發送JSON資料

$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); 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.