我一直在抨擊我的頭。 我想登錄到一個頁面,並使用PHP和捲曲,但有兩個問題,我懷疑他們是相關的。php curl:返回登錄頁面
cookie文件保持爲空
嘗試與錯誤登錄頁面就會返回到登錄頁面後(可能是因爲失敗的登錄嗎?):登錄超時。
首先,我去的網頁提取時間敏感的變量:?
// $ urlString =「https://domain.com/portal/gas.nsf/Tool
OpenAgent&action=TNQuery&ordernumber=".$order."&tn=".$tn."&User=".$username."&lang=en_US";
$loginUrl = 'https://domain.com/login';
$randnum = rand(5, 15);
$ckfile = tempnam ($cwd, "cookie.txt");
$ch = curl_init($loginUrl);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile); //write
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
curl_close($ch);
//var_dump($result);
$dom = new DOMDocument();
@$dom->loadHTML($result);
$xml = simplexml_import_dom($dom);
// extract time sensitive variable from the login page
$genData = $xml->xpath("//input[@name='pageGenTime']");
$epoch = $genData[0][value];
數據發佈:
$postdata = array(
'HiddenURI' => 'https://domain.com/loadPortal.htm',
'AUTHMETHOD' => 'UserPassword',
'pageGenTime' => $epoch,
'LOCALE' => "en_US",
'usr_name' => $username,
'usr_password' => $password
);
cURL處理程序:
$ch = curl_init();
// Now login to the portal
curl_setopt_array($ch, array(
CURLOPT_URL => $loginUrl,
CURLOPT_USERAGENT => $agent,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt'
));
curl_exec($ch);
$exec = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close ($ch);
print_r的回報:
Array
(
[url] => https://domain.com/login
[content_type] => text/html;charset=iso-8859-1
[http_code] => 200
[header_size] => 253
[request_size] => 304
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.08823
[namelookup_time] => 2.7E-5
[connect_time] => 3.1E-5
[pretransfer_time] => 8.0E-5
[size_upload] => 744
[size_download] => 9992
[speed_download] => 113249
[speed_upload] => 8432
[download_content_length] => 9992
[upload_content_length] => 744
[starttransfer_time] => 0.026516
[redirect_time] => 0
[certinfo] => Array
(
)
[primary_ip] => 206.47.249.9
[primary_port] => 443
[local_ip] => [concealed]
[local_port] => 41349
[redirect_url] =>
)
大,HTTP代碼200 ..但它實際上沒有登錄 我在做什麼錯? POST數據是正確的。
謝謝您寶貴的時間!
很難說不知道API的完整響應是什麼。僅僅因爲你得到一個200並不意味着API調用成功(如果它是一個設計不好的API,不會修改響應代碼)。 – 2014-11-14 14:44:28
如何獲得完整回覆?上面的代碼返回一個html登錄頁面。有沒有一種方法可以獲得我們正在尋找的信息@MikeBrant? – Shampie 2014-11-14 17:13:17
我的意思是例如你從'var_dump($ exec)'得到什麼? API響應本身告訴你什麼? – 2014-11-14 17:43:41