我正在尝试创建一个PHP脚本,该脚本将登录到网站,在登录后使用CURLOPT_RETURNTTRANSFER返回URL信息,然后分析这些数据以查找特定的匹配项。我想知道是否有一种方法可以刷新returntransfer数据,而无需注销并登录回网站?到目前为止,我必须完全注销,然后登录才能在returntransfer中获得新的http字符串。我是PHP和cURL的新手,所以如果这个问题含糊不清,请原谅我。下面是示例代码,正如您所看到的,基于标志,代码将继续循环,直到标志设置为true。
<?php
if(isset($_POST['userName']))
{
$cookie_file_path = "cookie.txt";
$data = array('Username' => $_POST['userName'],'Password' =>$_POST['userPassword']);
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7";
$reffer = "https://someurl/Login.aspx?action=login";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://someurl/Login.aspx?action=login");
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_fie_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_REFERER,$reffer);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$html= curl_exec($ch);
echo $html;
curl_close($ch);
$flagOfFound=0;
do {
//process the message
//......
//set $flagOfFound = true of there is match
//else
//refresh message/http returntransfer while still logged in
}
while ($flagOfFound == 0);
}
?>
无需注销并重新登录到站点
只是在你做这些请求时不要保存/存储任何cookie。
删除以下行:
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_fie_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
就是这样。