0
我需要使用POST(asp.net),我讀到有關從這裏這個話題,外部網站提交登錄表單和幾個例子我不能使它工作。登錄從HTTPS提交表單使用curl
我知道我發出正確的參數,因爲使用郵遞員進行測試,當我回到家HTML的響應,所以它一定是壞了,我的代碼做的,這就是我想要的此刻:
// Create temp file to store cookies
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
// URL to login page
$url = "https://site/Login.aspx";
// Get Login page and its cookies and save cookies in the temp file
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Accepts all CAs
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$fields = array(
'ctl00$ContentPlaceHolder1$txtUser' => 'user',
'ctl00$ContentPlaceHolder1$txtPassword' => '******',
'__VIEWSTATEGENERATOR'=> '716EFC3E',
'__EVENTVALIDATION'=> 'dfsgsdf5463456',
'__LASTFOCUS'=> '',
'__EVENTTARGET'=> '',
'__EVENTARGUMENT'=> '',
'__VIEWSTATE'=> 'wEPDwUKLTM1Mjk0NzM0Mw9kFgJmD2QWAgIDD2QWAgID',
'ctl00$ContentPlaceHolder1$btnLogin'=> 'In',
);
$fields_string = '';
foreach($fields as $key=>$value) {
$fields_string .= $key . '=' . $value . '&';
}
rtrim($fields_string, '&');
// Post login form and follow redirects
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // I also try it with false
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
這是一個通用的服務器運行時錯誤。
我在做什麼錯了?
如果這是真的,如何使用時,郵遞員來與相同參數的實際工作?這就是爲什麼我很迷惑 – elunap
您的代碼可能沒有發送一模一樣的餅乾和/或參數,但我們需要看到真正的網站才能夠告訴你更多。你可以使用一個數據包嗅探器工具(在PC上是一個很好的Fiddler2)捕獲郵遞員流量和你的php流量? –