开发者

log into a site using curl

开发者 https://www.devze.com 2023-02-02 00:10 出处:网络
I want to loginto a site using curl and then get resulting page. for example i want to run a script and see my yahoo mail page. i wrote something like this.

I want to loginto a site using curl and then get resulting page. for example i want to run a script and see my yahoo mail page. i wrote something like this.

//set POST variables
$url = 'http://localhost/test/login';
$fields = array(
                        'username'=>'abc',
                        'password'=>'def'
                );
$fields_string='';
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
echo $result;

login.php

echo $_POST['username'];
echo $_POST['password'];

header('Location: http://www.xyz.com/');

scr开发者_运维问答ipt posts data to login.php but contents of xyz.com are not shown.


By logging into a Website you normaly generate a Session ID which is stored in a Session. By logging in with CURL you didn't save this cookie in your Client Browser, you maybe logged in with your server, but there is no way to be logged in with your client.

And of course change this line:

echo $_POST['username'];
echo $_POST['password'];

header('Location: http://www.xyz.com/');

to the following:

header('Location: http://www.xyz.com/');
echo $_POST['username'];
echo $_POST['password'];

or use ob_start(); on the beginning of your script.

...which doesn't make sence because you'll never see that content ;)


Your problem is the two echo calls before the call to header.

According to the documentation header must be called before any other output is sent.

http://php.net/manual/en/function.header.php

0

精彩评论

暂无评论...
验证码 换一张
取 消