简体   繁体   English

用Php和Curl无法登录到远程服务器

[英]Login to remote server with Php and Curl not working

trying to login to a remote server using curl and php but getting errors about cookies not enabled. 尝试使用curl和php登录到远程服务器,但未获得有关cookie的错误。 Here's my code: 这是我的代码:

error_reporting(E_ALL);
ini_set("display_errors", 1);
$url = 'http://uk.songselect.com/account/login/';
$username = '*****';
$password = '******';
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'tmp/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'tmp/cookie.txt');


//run the process and fetch the document
$doc = curl_exec($ch);



// extract __RequestVerificationToken input field
preg_match('#<input name="__RequestVerificationToken" type="hidden" value="(.*?)"#is',            $doc, $match);

 $token = $match[1];



// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS,         '__RequestVerificationToken='.$token.'&UserName='.$username.'&Password='.$password.'');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);



//run the process and fetch the document
$doc = curl_exec($ch);
echo $doc;

//terminate curl process
curl_close($ch);

//print output


echo $token;

This is the error I'm getting: 'You need to have cookies enabled in your browser to use the SongSelect site as an authenticated user.' 这是我得到的错误:“您需要在浏览器中启用cookie才能将SongSelect网站用作经过身份验证的用户。”

Any ideas why this is happening? 任何想法为什么会这样?

thanks! 谢谢!

我想您需要使用保存在tmp / cookie.txt中的cookie来初始化另一个cUrl通道并执行它。

To add cookies to your curl request : 要将cookie添加到您的curl请求中:

curl_setopt( $ch, CURLOPT_COOKIESESSION, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, uniquefilename );
curl_setopt( $ch, CURLOPT_COOKIEFILE, uniquefilename );

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

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