2015-07-01 43 views
1

我已經寫了一個函數來使用CURL刮網站,但它調用時不會返回任何內容,並且無法理解爲什麼。輸出是空如何刮SSL或HTTPS URL

<?php 
    function scrape($url) 
    { 
     $headers = Array(
        "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", 
        "Cache-Control: max-age=0", 
        "Connection: keep-alive", 
        "Keep-Alive: 300", 
        "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7", 
        "Accept-Language: en-us,en;q=0.5", 
        "Pragma: " 
       ); 
     $config = Array(
         CURLOPT_RETURNTRANSFER => TRUE , 
         CURLOPT_FOLLOWLOCATION => TRUE , 
         CURLOPT_AUTOREFERER => TRUE , 
         CURLOPT_CONNECTTIMEOUT => 120 , 
         CURLOPT_TIMEOUT => 120 , 
         CURLOPT_MAXREDIRS => 10 ,     
         CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8" , 
         CURLOPT_URL => $url , 
         ) ; 
     $handle = curl_init() ; 
     curl_setopt_array($handle,$config) ; 
     curl_setopt($handle,CURLOPT_HTTPHEADER,$headers) ; 
     $data = curl_exec($handle) ; 
     curl_close($handle) ; 
     return $data ; 
    } 

    echo scrape("https://www.google.com") ; 
?> 

回答

5

有試圖刮SSL或HTTPS時,2點可能的修復網址:

  1. 速戰速決
  2. 適當的修復

速戰速決,首先。

警告:這可能會引入SSL旨在防範的安全問題。

組:CURLOPT_SSL_VERIFYPEER => false

第二,和適當的修正。設置3個選項:

  1. CURLOPT_SSL_VERIFYPEER => true
  2. CURLOPT_SSL_VERIFYHOST => 2
  3. CURLOPT_CAINFO => getcwd() . '\CAcert.pem'

你需要做的最後一件事是下載CA證書。

前往, - http://curl.haxx.se/docs/caextract.html - >點擊'cacert.pem' - > copie /將文本粘貼到文本編輯器 - >將文件保存爲'CAcert.pem'檢查它不是'CAcert.pem。 TXT'

<?php 
    function scrape($url) 
    { 
     $headers = Array(
        "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", 
        "Cache-Control: max-age=0", 
        "Connection: keep-alive", 
        "Keep-Alive: 300", 
        "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7", 
        "Accept-Language: en-us,en;q=0.5", 
        "Pragma: " 
       ); 
     $config = Array(
         CURLOPT_SSL_VERIFYPEER => true, 
         CURLOPT_SSL_VERIFYHOST => 2, 
         CURLOPT_CAINFO => getcwd() . '\CAcert.pem', 
         CURLOPT_RETURNTRANSFER => TRUE , 
         CURLOPT_FOLLOWLOCATION => TRUE , 
         CURLOPT_AUTOREFERER => TRUE , 
         CURLOPT_CONNECTTIMEOUT => 120 , 
         CURLOPT_TIMEOUT => 120 , 
         CURLOPT_MAXREDIRS => 10 ,     
         CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8" , 
         CURLOPT_URL => $url 
         ) ; 
     $handle = curl_init() ; 
     curl_setopt_array($handle,$config) ; 
     curl_setopt($handle,CURLOPT_HTTPHEADER,$headers) ; 
     $output->data = curl_exec($handle) ; 

     if(curl_exec($handle) === false) { 
      $output->error = 'Curl error: ' . curl_error($handle); 
     } else { 
      $output->error = 'Operation completed without any errors'; 
     } 

     curl_close($handle) ; 
     return $output ; 
    } 

$scrape = scrape("https://www.google.com") ; 

echo $scrape->data; 

//uncomment for errors 
//echo $scrape->error; 
?> 
+0

現在我得到這個錯誤捲曲錯誤:錯誤設置證書驗證地點:憑證檔案錯誤:C:\ XAMPP \ htdocs中\ php_inc \ CAcert.pem CApath:無 –

+0

可能是一對夫婦的事情。但是,讓我們從你的路徑開始,複製/粘貼到你的瀏覽器的URL是否找到文件?如果不是,你沒有使用正確的道路。 – PHPhil

+0

我用'file_exists()',它工作。路徑存在 –