简体   繁体   English

PHP CURL登录到另一台服务器

[英]PHP CURL login to another server

$cookie="cookie.txt"; 
$data = array();
$data['global_id'] = "raj";
$data['global_password'] = "raj";

foreach ($data as $key => $value){
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);


$curl_connection = curl_init('http://somedomain.com/cgi-bin/login.pl');
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, 0);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_REFERER, 'http://somedomain.com/cgi-bin/login.pl');
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($curl_connection, CURL_COOKIEFILE, '');
$result = curl_exec($curl_connection);

if($result == "success"){
    header("Location: http://somedomain.com/cgi-bin/index.pl");
}
else{
    echo "Login failed";
}

//close the connection
curl_close($curl_connection);

Whats wrong with it... I cant login 怎么了...我无法登录

After executing this script if I goto http://somedomain.com/cgi-bin/index.pl it should logged in . 执行完该脚本后,如果我转到http://somedomain.com/cgi-bin/index.pl ,则应登录。 How can I do this ? 我怎样才能做到这一点 ?

You should set CURLOPT_RETURNTRANSFER to true . 您应该将CURLOPT_RETURNTRANSFERtrue Probably your $result is empty. 您的$ result可能为空。

Moreover while setting CURLOPT_REFERER you obviously used the wrong variable. 此外,在设置CURLOPT_REFERER您显然使用了错误的变量。 ($ch) ($ ch)

EDIT: 编辑:

It seems as if the following line is missing: 似乎缺少以下行:

curl_setopt($curl_connection, CURLOPT_POST, true);

Try this: 尝试这个:

curl_setopt($ch, CURLOPT_SSLVERSION, 3); // OpenSSL issue
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  // Wildcard certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

Otherwise you should know about SSL configuration of the server. 否则,您应该了解服务器的SSL配置。 It can be an issue. 这可能是一个问题。

Also you can set POST vars as array: 您也可以将POST变量设置为数组:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('param1' => 'value1', 'param2' => 'value2'));

UPDATE: And You are trying to use SSL over HTTP . 更新:并且您正在尝试通过HTTP使用SSL

$curl_connection = curl_init('http://somedomain.com/cgi-bin/login.pl');

Try correcting your URL to HTTPS . 尝试将URL更正为HTTPS

try fix ur curl opt code, like this: 尝试修复您的curl opt代码,如下所示:

curl_setopt($curl_connection, CURLOPT_POST, true);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURL_COOKIEFILE, $cookie);
curl_setopt($curl_connection, CURL_VERBOSE, true);

hope this helpfull.. 希望对您有所帮助。

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

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