简体   繁体   English

使用curl PHP登录和发布数据

[英]login and post data using curl php

i need to login to a website then it redirects me to an other page where i have to post data ; 我需要登录到一个网站,然后它将我重定向到另一个页面,我必须在该页面上发布数据; the code i already wrote is going to logged me on succeffuly and it redirects me to the page i have to post(send $_POST) data but i don't know how to post it (begining an other request using the some session/cookies that already started) this is the code . 我已经编写的代码将成功登录我,它将我重定向到我必须发布(发送$ _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);

To do a subsequent request with same session and cookie data, don't call curl_exec($ch); 要使用相同的会话和Cookie数据进行后续请求,请不要调用curl_exec($ch); too early. 太早了。 Simply work with the same curl handle, changing options as you go on. 只需使用相同的卷曲手柄,就可以继续更改选项。 Your code should look something like this: 您的代码应如下所示:

// 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM