登录后使用cURL将数据POST到URL

时间:2013-10-13 20:11:40

标签: php curl

我正在尝试使用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);

1 个答案:

答案 0 :(得分:0)

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

查看PHP Curl And Cookies