1

我正在尝试使用 PHP/cURL 将数据发布到需要登录的 URL。从 PHP 代码中,我可以成功执行登录,但是当我执行下一个 POST 时,站点会响应我没有登录。关于站点如何确定第二个 POST 的登录不再活动的任何想法?登录的 POST 选项之一是我设置的“在这台机器上记住我”。这是部分代码:

//#### Log IN

//create cURL connection
$curl_connection = curl_init('http://www.website.com/cgi-bin/fg.cgi');

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_POST, true);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $log_string);

//perform our request
$result = curl_exec($curl_connection);

//#####  Second POST to send data

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0;  Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_POST, true);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

//close the connection
curl_close($curl_connection);
4

1 回答 1

0

您需要处理 cookie。当您发布到登录时,会设置会话 cookie 或其他内容,您需要在下一个请求中重新发送该 cookie。幸运的是,这很容易。

查看PHP Curl 和 Cookies

于 2013-10-13T20:24:13.243 回答