0

我需要登录一个网站,然后它会将我重定向到另一个我必须发布数据的页面;我已经编写的代码将使我成功登录,并将我重定向到我必须发布(发送 $_POST)数据的页面,但我不知道如何发布它(使用某些会话/cookies 开始另一个请求已经开始)这是代码。

$url = "http://site.com/account.php?s_id=&d=y";
    $email = "EMAIL@EMAIL.COM";
    $password = "PASS";
    $postfields = "email=$email&password=$password&submit=1&do=login&forward=&communicate=&communicate_id=1";
    $postfield = "message=doit";
    $cookiefile  = tempnam("/tmp", "cookies");  
    $agent     = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";  
    $ch = curl_init();
    $ch2 = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);  
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);  
    curl_setopt($ch, CURLOPT_HEADER, 0); // Get the header
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // Allow redirection
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
    curl_exec($ch);
    curl_close($ch);
4

1 回答 1

0

要使用相同的会话和 cookie 数据执行后续请求,请不要curl_exec($ch);过早调用。只需使用相同的卷曲手柄,继续更改选项即可。您的代码应如下所示:

// do login request
curl_exec($ch);
echo '<hr>';

// change url
curl_setopt($ch, CURLOPT_URL,$url_to_post_to);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_to_post);

// do post request with your data
curl_exec($ch);

curl_close($ch);
于 2013-02-20T14:44:09.333 回答