3

我正在尝试使用 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 回归:会话已过期

所以这是我的完整代码:

<?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;
?>

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

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

非常感谢您。

4

1 回答 1

0

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

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

谢谢

莫因

于 2014-05-12T16:19:31.027 回答