file_get_contents 擷取不了網頁內容,filegetcontents_PHP教程

來源:互聯網
上載者:User

file_get_contents 擷取不了網頁內容,filegetcontents


伺服器在做驗簽的過程中,經常需要向渠道伺服器擷取某個使用者的資訊。一般有兩種方法,curl和file_get_contents。

一般情況下,像這樣用,不會有問題。

 1 public function OauthPostExecuteNew($sign,$requestString,$request_serverUrl){ 2     $opt = array("http"=>array( 3         "method"=>"GET", 4         "header"=>array("param:".$requestString,"oauthsignature:".$sign), 5         "request_fulluri"=>true 6         ) 7     ); 8  9     $context = stream_context_create($opt);10     $res=file_get_contents($request_serverUrl, false, $context);11 12     return $res;13 }

但是由於我司伺服器連外網時通過代理,所以在使用stream_context_create時需要帶上proxy參數,才能訪問到渠道的伺服器。

所以在上面代碼 $opt 數組中帶上"proxy"=>$proxy欄位。加上之後發現file_get_contents仍然不能正常驗簽。

百思不解,遂到官網上來查查file_get_contents,發現並沒有關於proxy的解釋,然後搜stream_context_create,官方解釋有這句話

params

必須是 $arr['parameter'] = $value 格式的關聯陣列。 請參考 context parameters 裡的標準資源流參數列表。

那麼 我們進入context_parameters 查看參數配置。因為我們使用的是HTTP方式,所以查看HTTP context

查看跟proxy相關的

proxy string

URI 指定的Proxy 伺服器的地址。(e.g. tcp://proxy.example.com:5100).

request_fulluri boolean

當設定為 TRUE 時,在構建請求時將使用整個 URI 。(i.e. GET http://www.example.com/path/to/file.html HTTP/1.0)。 雖然這是一個非標準的請求格式,但某些Proxy 伺服器需要它。

預設值是 FALSE.

發現只配置了proxy,而並沒有配置request_fulluri,遂加上request_fulluri=true,驗證通過。

注意:使用proxy參數時需要把http 改為tcp 具體什麼原因,不知道。等我查到了再到這裡更新。

http://www.bkjia.com/PHPjc/1097795.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1097795.htmlTechArticlefile_get_contents 擷取不了網頁內容,filegetcontents 伺服器在做驗簽的過程中,經常需要向渠道伺服器擷取某個使用者的資訊。一般有兩種方法,...

  • 聯繫我們

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