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 伺服器在做驗簽的過程中,經常需要向渠道伺服器擷取某個使用者的資訊。一般有兩種方法,...