<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>
            php語言

            PHP實現抓取HTTPS內容

            時間:2025-02-11 03:10:02 php語言 我要投稿
            • 相關推薦

            PHP實現抓取HTTPS內容

              文章主要介紹了PHP實現抓取HTTPS內容,以及遇到的問題的解決方法,需要的朋友可以參考下。

              最近在研究Hacker News API時遇到一個HTTPS問題。因為所有的Hacker News API都是通過加密的HTTPS協議訪問的,跟普通的HTTP協議不同,當使用PHP里的函數 file_get_contents() 來獲取API里提供的數據時,出現錯誤,使用的代碼是這樣的:

              <?php

              $data = file_get_contents("https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty");

              ......

              當運行上面的代碼是遇到下面的錯誤提示:

              PHP Warning:  file_get_contents(): Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?

              下面是截圖:

              php https error

              為什么會出現這樣的錯誤?

              在網上經過一番搜索,發現遇到這樣錯誤的人還不少,問題很直接,是因為在PHP的配置文件里沒有開啟一個參數,在我本機上是 /apache/bin/php.ini 里的 ;extension=php_openssl.dll 這一項,需要將前面的分號去掉。你可以用下面的腳本來檢查你的PHP環境的配置:

              $w = stream_get_wrappers();

              echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "\n";

              echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";

              echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";

              echo 'wrappers: ', var_dump($w);

              運行上面的這個腳本片段,在我的機器上得到的結果是:

              openssl: no

              http wrapper: yes

              https wrapper: no

              wrappers: array(10) {

              [0]=>

              string(3) "php"

              [1]=>

              string(4) "file"

              [2]=>

              string(4) "glob"

              [3]=>

              string(4) "data"

              [4]=>

              string(4) "http"

              [5]=>

              string(3) "ftp"

              [6]=>

              string(3) "zip"

              [7]=>

              string(13) "compress.zlib"

              [8]=>

              string(14) "compress.bzip2"

              [9]=>

              string(4) "phar"

              }

              替代方案

              發現錯誤,改正錯誤,這很簡單,困難的是,發現錯誤后無法改正錯誤。我原本是想將這個腳本方法遠程主機上,但我無法修改遠程主機的PHP配置,結果是,我無法使用這一方案,但我們不能在一棵樹上吊死,這條路走不通,看看有沒有其它路。

              另外一個我經常用的PHP里抓取內容的函數是 curl ,它比 file_get_contents() 更強大,提供了很多的可選參數。對于訪問 HTTPS 內容的問題,我們需要使用的 CURL 配置參數是:

              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

              你可以從語義上看出,它是忽略/跳過了SSL安全驗證。也許這不是一個很好的做法,但對于普通的場景中,這幾經足夠了。

              下面是利用 Curl 封裝的一個能訪問HTTPS內容的函數:

              function getHTTPS($url) {

              $ch = curl_init();

              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

              curl_setopt($ch, CURLOPT_HEADER, false);

              curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

              curl_setopt($ch, CURLOPT_URL, $url);

              curl_setopt($ch, CURLOPT_REFERER, $url);

              curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

              $result = curl_exec($ch);

              curl_close($ch);

              return $result;

            【PHP實現抓取HTTPS內容】相關文章:

            php抓取https的內容的代碼08-18

            PHP如何使用curl實現數據抓取09-27

            php抓取頁面的方法08-24

            php抓取頁面的的方法06-09

            php抓取頁面的幾種方法10-01

            實現PHP實現限制IP訪問11-11

            php使用curl訪問https示例分享09-15

            php實現無限級分類實現代碼07-03

            php頁面緩存實現方法07-20

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看