繁体   English   中英

PHP CURL - 会话已过期

[英]PHP CURL - Session expired

我正在尝试使用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;
?>

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

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

非常感谢你提前。

好的,您需要在登录后转发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);

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

谢谢

莫因

暂无
暂无

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

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