2014-05-06 85 views
0

尝试使用curl和php登录到远程服务器,但获取有关未启用cookie的错误。这里是我的代码:使用curl和php登录到远程服务器

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; 

这是我收到的错误:“你需要在你的浏览器使用SongSelect网站作为身份验证的用户启用cookie”

任何想法为什么会发生这种情况?

谢谢!

回答

0

要将Cookie添加到您的卷曲的要求:

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

试过了,它不会改变任何东西。仍然得到相同的错误 –

1

我猜你需要初始化与保存在TMP/cookie.txt饼干另一cUrl作者通道和exec它。