2017-07-26 159 views
1

我無法使用PHP解析curl中的重定向。我打電話的網址爲https://data.fei.org/Horse/Search.aspx,但結果我得到一個登錄網站。這不會發生在所有服務器上。在測試環境中,它起作用,但不是生產服務器。這是我的捲曲初始化PHP curl重定向到其他網站

$url = "https://url.org/Search.aspx"; 
$checkFile = tempnam('/tmp', 'cookie.txt'); 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_PROXY, "my.proxy.com:8080"); 
curl_setopt($ch, CURLOPT_PROXYPORT, 8080); 
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded' 
)); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false); 
curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $checkFile); 
curl_exec($ch); 

任何想法,爲什麼我重定向生產,但不是在測試環境?

+0

良好的開端是調試卷曲請求找出發生了什麼事:https://stackoverflow.com/questions/3757071/php-debugging-curl –

+0

可能你需要檢查代理設置? –

回答

0

最佳猜測:在測試服務器上,您有對/ tmp的寫入訪問權限,並且確實可以在那裏創建新文件,但在生產服務器上,您沒有對/ tmp的寫入訪問權限。因爲您沒有檢查tempnam的返回類型,所以您在生產服務器上將curl bool(false)指定給CURLOPT_COOKIEFILE,並且curl無法保存/加載cookie,並且未能將請求1中收到的cookie提供給後續服務位置重定向請求#2 - 因爲它有一個無效的cookie文件。

驗證,增加更多的錯誤檢查(這實際上是什麼,你應該首先做了,甚至要求對計算器之前)

$checkFile = tempnam('/tmp', 'cookie.txt'); 
if(false===$checkFile){ 
    throw new \RuntimeException('tmpnam failed to create cookie temp file!'); 
} 

也普羅蒂普,調試卷曲代碼時,始終啓用CURLOPT_VERBOSE,它會打印大量有用的調試信息,包括所有重定向,以及所有與所有重定向一起發送的Cookie。

在同一筆記上,也添加了對curl_setopt的錯誤檢查。如果出現問題,請設置curl選項curl_setopt返回bool(false)。在我捲曲的包裝,我做

$ret = curl_setopt ($this->curlh, $option, $value); 
    if (! $ret) { 
     throw new InvalidArgumentException ('curl_setopt failed. errno: ' . $this->errno() . '. error: ' . $this->error() . '. option: ' . var_export ($this->_curlopt_name ($option), true) . ' (' . var_export ($option, true) . '). value: ' . var_export ($value, true)); 
    } 

(從https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php#L997

另一種可能是你督促服務器IP-禁止登錄。

+0

設置Web應用程序內部目錄的路徑,現在可以使用 – Marlowe

相關問題