2013-10-27 130 views
1

我正在爲包含登錄的網站編寫API。我正在使用cURL來實現這一點。該站點需要訪問一個頁面才能訪問另一個頁面;例如:在我可以去「Portal/AccountInfo」之前需要打開「Portal」。正如預料的那樣,當我手動使用Bash編寫腳本時,我會在調用/ Portal/AccountInfo時獲得所需的輸出。cURL PHP網站通過CookieJar

curl -c c.txt -d "Username=u&Password=p" https://somewebsite.com/Login 
curl -b c.txt -L https://somewebsite.com/Portal/ 
curl -b c.txt -L https://somewebsite.com/Portal/AccountInfo/ 

雖然在使用cURL將其轉換爲PHP時,我無法獲得第三個請求的正確輸出。見下:

<?php 

error_reporting(E_ALL); 
ini_set("display_errors", 1); 

$Username = $_POST["Username"]; 
$Password = $_POST["Password"]; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookie"); 
curl_setopt($ch, CURLOPT_URL,"https://somewebsite.com/Login"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "Username=$Username&Password=$Password"); 
curl_exec ($ch); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie"); 
curl_setopt($ch, CURLOPT_URL,"https://somewebsite.com/Portal"); 
curl_exec ($ch); 
curl_close ($ch); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie"); 
curl_setopt($ch, CURLOPT_URL,"https://somewebsite.com/Portal/AccountInfo"); 
$buf3 = curl_exec ($ch); 
curl_close ($ch); 

echo "<PRE>".htmlentities($buf3); //This output is not the same as the third command I wrote in bash 
unlink("/tmp/cookie"); 
?> 

任何幫助,非常感謝。乾杯!

回答

0

您還沒有將-L轉換成PHP代碼:CURLOPT_FOLLOWLOCATION,它可能是缺少的東西。

+0

工作完美。謝謝!! :) – ssabetan