通过php curl登录远程站点

时间:2017-01-30 11:18:18

标签: php curl

登录URL为:https://192.X.X.X/abc/Login.aspx?FromMasterLogin=true

登录页面发送的标题数据:

  

__ EVENTTARGET:btnLogin   __EVENTARGUMENT:   __VIEWSTATE:/ wEPDwULLTEwNzI1MzU5MzBkGAIFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYGBQhidG5Mb2dpbgUPYnRuQ2xlYXJTZXNzaW9uBRFSYWRXaW5kb3dNYW5hZ2VyMQUOcmR3aW5kb3dGb3JnZXQFD3Jkd2luZG93RW5mb3JjZQUYcmRXaW5kb3dQdWJsaWNOZXdzQWxlcnRzBQpyYWRDYXB0Y2hhDxQrAAIFJDQyM2FlNDE3LTEwMTctNDE2OS1hNjgzLTBmMjMyZDZkMDdmZAYAAAAAAAAAAGQdkHIfEfL2XAG + 8 + / wu30lMfjmEwOeIiiC7jveX5PnZg ==   __EVENTVALIDATION:/ wEdAAfBlkUqNKBEV3moC9pS8IJTY3plgk0YBAefRz3MyBlTcJxpWckI3qdmfEJVCu2f5cGinihG6d / Xh3PZm3b5AoMQf2Dr69OxAarGhVFbQWZWFpd + ecw9lQ5sg8SY03yGmgNKhPS / + + yQ5 zLwEb8uDfAwYKkBfoLWkbIJoPnHfXTqz5B / GZyy44ThZCPCAskCEVA =   txtUserName:管理员   txtUserName_ClientState:{"启用":真," emptyMessage":""}   txtpassword:管理员@ 123   txtpassword_ClientState:{"启用":真," emptyMessage":""}   btnLogin_ClientState:btnClearSession_ClientState:   rdwindowForget_ClientState:rdwindowEnforce_ClientState:   rdWindowPublicNewsAlerts_ClientState:RadWindowManager1_ClientState:

以下是代码

<?php
//username and password of account
$username = 'admin';
$password = 'admin@123';


//login form action url
$url="https://192.168.X.X/abc/Login.aspx?FromMasterLogin=true"; 
$postinfo ='txtUserName:admin&txtpassword:admin@123';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

//set the cookie the site has for certain features, this is optional
curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
curl_setopt($ch, CURLOPT_USERAGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
curl_exec($ch);

//page with the content I want to grab
//curl_setopt($ch, CURLOPT_URL, "https://192.168.X.X/abc/MemberManagement/MemberFileDownload.aspx");
//do stuff with the info with DomDocument() etc
$html = curl_exec($ch);
echo $html;
curl_close($ch);
?>

但结果显示会话已过期。

1 个答案:

答案 0 :(得分:0)

我意识到这并没有回答你关于curl的具体问题,但是可能值得尝试使用Guzzle HTTP客户端进行PHP,其中包括一些维护会话的功能,并减少了直接使用curl的困惑。

以下是Guzzle documentation

的摘录
// Use a specific cookie jar
$jar = new \GuzzleHttp\Cookie\CookieJar;
$r = $client->request('GET', 'http://httpbin.org/cookies', [
    'cookies' => $jar
]);

// Use a shared client cookie jar
$client = new \GuzzleHttp\Client(['cookies' => true]);
$r = $client->request('GET', 'http://httpbin.org/cookies');