PHP CURL - 会话已过期

时间:2013-03-18 13:04:32

标签: php session curl

我正在尝试使用PHP CURL和MEGAVOIP进行voip调用。 问题是我无法管理会话以访问受密码保护的页面。 我查看哪些变量发布到登录页面以使用Curl发布。 但我的代码不起作用。

按照Colin Morelli和Waygood的建议,我只是在两个命令中添加了这些行:

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

但它仍然是相同的:

Megavoip返回:SESSION EXPIRED

所以这是我的完整代码:

<?php
ini_set("display_errors", 1);  
$username="***"; 
$password="***"; 
$url="https://www.megavoip.com/login"; 
$url2="https://www.megavoip.com/phone_to_phone/";
$timeout = 10;
$cookies_file = 'cookies.txt';

// HERE I GET THE TOKEN

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies_file);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);


preg_match_all('/<input[^>]+>/i',$content, $result); 
preg_match_all('/(id|value)=("[^"]*")/i',$result[0][5], $img);
$img1=str_replace('"', '', $img[0][0]);
$img2=str_replace('"', '', $img[0][1]);
$img1=substr($img1,3);
$img2=substr($img1,6);
$postdata = "login%5Busername%5D=".$username."&login%5Bpassword%5D=".$password."&page_referrer=login&".$img1."=".$img2; 

// HERE I SEND THE VARIABLES

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies_file);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

$content = curl_exec($ch);

// IF LOGGED HERE I'LL MAKE THE CALL

curl_close($ch);

echo $content;
exit;
?>

有什么想法可以帮助我吗?

这是一个测试帐户,如果您想查看并帮助我,请随时使用我的登录名和密码!

提前多多谢谢你。

1 个答案:

答案 0 :(得分:-1)

好的,您需要在登录后转发cookie /会话,

您需要先登录后从Header中提取cookie,如下所示

// HERE I GET THE TOKEN

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);    
.... .... 
.... ....
$content = curl_exec($ch);

preg_match('/^Set-Cookie:\s*([^;]*)/mi', $content, $m);
parse_str($m[1], $cookies);
$cookie = $cookies['NAMEOFCOOKIEUNEEDHERE'];

之后需要在curl选项中使用$ cookie变量,如

// HERE I SEND THE VARIABLES

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIE, 'NAMEOFCOOKIEUNEEDHERE='.$cookie);

我希望你的问题能得到解决。

由于

莫因