L.S。
我有一些开发技巧,但我是php curl的新手。 我正在尝试创建一个php脚本,在任何用户交互中登录到intraship页面。
我使用fiddler来确定发布的值并在登录表单中搜索隐藏值。
加载登录页面时,会生成jsessionid和唯一的RRP编号,并在提交时发布。
我在下面创建了一个脚本,它从登录页面收集唯一的数字并提交数据,但它只显示登录页面... 有人可以帮帮我,告诉我我错过了什么。
<?php
//set var
$ch = curl_init();
$url = "www.intraship-dhl.nl/intraship/jsp/SessionCtrl.jsp";
$jTFName = "foobar";
$jPFPassword = "foo";
$jCBLanguage = "NL";
$jsstate = "false";
$jCBLanguage = "NL";
$jBtnSendLogin = "1";
$cookie = "C:/wamp/www/dhl.txt";
//start with curl
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "X11; U; Linux x86_64; en-US; rv:1.9.2.15) Gecko/20110303 Ubuntu/10.04 (lucid) Firefox/3.6.15");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt ($ch, CURLOPT_REFERER, $url);
$output = curl_exec($ch);
//echo $output;
//start processing output to determine jsessionid, URL and RRP
$jsessionid_sub = array();
$var1 = preg_match('/jsessionid=(.*?)\?/s', $output, $jsessionid_sub);
$jsessionid = ($jsessionid_sub[1]);
//print_r($jsessionid_sub[1]);
$URL_sub = array ();
$var2 = preg_match('/URL=(.*?)\&/s', $output, $URL_sub);
$URL = ($URL_sub[1]);
//print_r($URL_sub[1]);
$RRP_sub = array();
$var3 = preg_match('/RRP=(.*?)\"/s', $output, $RRP_sub);
$RRP = ($RRP_sub[1]);
//print_r($RRP_sub[1]);
//continue post the data and collect the result page
$postdata = "jBtnSendLogin=1&jBtnOK=Login&jTFName=".$jTFName."&jPFPassword=".$jPFPassword."&jsessionid=".$jsessionid."&RRP=".$RRP."&URL=".$URL."&jsstate=".$jsstate."&jCBLanguage=".$jCBLanguage."&jBtnSendLogin=".$jBtnSendLogin."&jBtnRefreshPage=1";
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo $result;
curl_close($ch);
?>
答案 0 :(得分:0)
在第一次请求后执行curl_close
,否则curl不会将cookie保存到文件中。这就是你一次又一次获得登录页面的原因!
curl_close($ch);
现在为第二个POST打开一个新的curl会话:
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
// and the other options you might add from previous request, just add here!
这应该适合你。
答案 1 :(得分:0)
在做了更多研究之后,我有以下代码,但它只生成一个空页。
有人可以告诉我我错过了什么吗?
<?php
$ch = curl_init();
$url="http://www.intraship-dhl.nl/intraship/jsp/SessionCtrl.jsp";
$cookie = "C:/wamp/www/cookie.txt";
curl_setopt_array(
$ch, array(
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.15) Gecko/20110303 Ubuntu/10.04 (lucid) Firefox/3.6.15',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => $cookie,
CURLOPT_COOKIEJAR => $cookie
));
$output = curl_exec($ch);
//echo $output;
$post_sub = array();
$var1 = preg_match('/LoginCtrl.jsp;(.*?)\" class/s', $output, $post_sub);
$post = ($post_sub[1]);
$jTFName = "foobar";
$jPFPassword = "foo";
$jsstate = "true";
$jCBLanguage = "NL";
$jBtnSendLogin = "1";
$postData = "jsstate=".$jsstate."&jTFName=".$jTFName."&jPFPassword=".$jPFPassword."&jCBLanguage=".$jCBLanguage."&jBtnSendLogin=".$jBtnSendLogin;
$url2 = $url.$post;
$c = curl_init();
curl_setopt_array(
$c, array(
CURLOPT_URL => $url2,
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.15) Gecko/20110303 Ubuntu/10.04 (lucid) Firefox/3.6.15',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => $cookie,
CURLOPT_COOKIEJAR => $cookie,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_POST => true
));
$result = curl_exec($c);
echo $result;
?>