php curl登录远程服务器

时间:2014-10-21 20:08:33

标签: php curl

我已经尝试了解决这个问题的所有方法和方法,但我很难过。我试图用php登录www.senderscore.org。该网站的登录是其索引页面的帖子表格。我需要这样做,因为我需要查找几个IP并定期记录信息。我试过用curl发帖,但我没有运气。每次尝试都会返回页面,就好像我没有尝试登录一样。如果有人可以提供代码片段的帮助,我会非常感激。

编辑:最新尝试的代码块。这是类函数内的代码。

    $username= $this->username;
    $password= $this->password;

    $url = 'www.senderscore.org/index.php';

    $fields = array(
    'email'=>$username,
    'password'=>$password,
    'action'=>"localLogin",
    'Submit'=>"Sign in",
    'remember'=>'1'
    );

    $postvars='';
    $sep='';
    foreach($fields as $key=>$value)
    {
        $postvars.= $sep.urlencode($key).'='.urlencode($value);
        $sep='&';
    }

    $curl = curl_init();

    curl_setopt($curl,CURLOPT_URL,$url);
    curl_setopt($curl,CURLOPT_POST,true);
    curl_setopt($curl,CURLOPT_POSTFIELDS,$postvars);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);          
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

    $data = curl_exec($curl);

    if ($data === false) {
        $data = curl_error($curl);
    }
    curl_close($curl);

    return $data;

1 个答案:

答案 0 :(得分:0)

尝试查看此脚本,它应该与post一起使用;我还没有测试过它:

$username = 'your_username';
$password = 'your_password';
$loginUrl = 'http://www.path-to-login.com/login/';

//init curl
$ch = curl_init();

//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $loginUrl);

// ENABLE HTTP POST -  this is important in your case
curl_setopt($ch, CURLOPT_POST, 1);

//Set the post parameters - make sure to match username and password
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user='.$username.'&pass='.$password);

//Handle cookies for the login
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

//Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
//not to print out the results of its query.
//Instead, it will return the results as a string return value
//from curl_exec() instead of the usual true/false.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//execute the request (the login)
$store = curl_exec($ch);

//the login is now done and you can continue to get the
//protected content.

//set the URL to the protected file
curl_setopt($ch, CURLOPT_URL, 'http://www.site-logging-into.com/protected/file.zip');

//execute the request
$content = curl_exec($ch);

//save the data to disk
file_put_contents('~/file.zip', $content);