php訪問url的四種方式

來源:互聯網
上載者:User

標籤:

php訪問url的四種方式

1.fopen方式
//訪問指定URL函數

[php] view plain copy print?
  1. function access_url($url) {    
  2.     if ($url==‘‘) return false;    
  3.     $fp = fopen($url, ‘r‘) or exit(‘Open url faild!‘);    
  4.     if($fp){  
  5.     while(!feof($fp)) {    
  6.         $file.=fgets($fp)."";  
  7.     }  
  8.     fclose($fp);    
  9.     }  
  10.     return $file;  
  11. }  


2.file_get_contents方式(開啟遠程檔案的時候會造成CPU飆升。file_get_contents其實也可以post)

[php] view plain copy print?
  1. $content = file_get_contents("http://www.google.com");  


3.curl方式

[php] view plain copy print?
  1. function curl_file_get_contents($durl){  
  2.     $ch = curl_init();  
  3.     curl_setopt($ch, CURLOPT_URL, $durl);  
  4.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 擷取資料返回    
  5.     curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在啟用 CURLOPT_RETURNTRANSFER 時候將擷取資料返回    
  6.     $r = curl_exec($ch);  
  7.     curl_close($ch);  
  8.     return $r;  
  9. }  


4.fsockopen方式(只能擷取網站首頁資訊,其他頁面不可以)

[php] view plain copy print?
    1. $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);     
    2. if (!$fp) {     
    3.     echo "$errstr ($errno)<br />\n";     
    4. } else {     
    5.     $out="GET / HTTP/1.1\r\n";     
    6.     $out.="Host: www.example.com\r\n";     
    7.     $out.="Connection: Close\r\n\r\n";     
    8.     fwrite($fp, $out);     
    9.     while (!feof($fp)) {     
    10.         echo fgets($fp, 128);     
    11.     }  
    12.     fclose($fp);     
    13. }

php訪問url的四種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.