【发布时间】:2014-09-24 12:05:01
【问题描述】:
我正在尝试使用 PHP cURL 登录并提交另一个联系表单。
下面是我的脚本,
<?php
echo login_curl();
function login_curl() {
//First Form post data's
$postfields = array();
$postfields["formUsername"] = urlencode("xxx");
$postfields["formPassword"] = urlencode("xxx123");
//Define option variables
$action ="http://www.websss.com/websss/authenticate.php";
$cookie = "cookie.txt";
$agent = "Mozilla/26.0";
$referer = "http://www.websss.com/websss/login.php";
//Second form post data's
$secpostfields = array();
$secpostfields["form_url"] = urlencode("http://web.com");
$secpostfields["form_desc"] = urlencode("Jquery web Link");
//Define option variables
$secondaction ="http://www.websss.com/websss/insert_link.php";
$secondreferer = "http://www.websss.com/websss/login.php";
// Submit Login Form
$ch = curl_init($action);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//Capture output and close session
$result = curl_exec($ch);
curl_close($ch);
//submit second form after login
$secondch = curl_init($secondaction);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $secpostfields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $secondreferer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//Capture output and close session
$resultsecond = curl_exec($secondch);
curl_close($secondch);
return $resultsecond;
}
?>
我可以注意到,上面的脚本可以正确登录,但它没有提交第二个表单。
有人知道我哪里出错了吗?
【问题讨论】:
-
首先从第一个 curl 结果中获取 curl 响应,然后调用第二个 curl。
-
是的,我明白了。我也将其分配给变量 $result。我在处理第二种形式时遇到问题。