5

我正在尝试使用 CURL 登录远程网站,但似乎无法正常工作。

我尝试登录的页面是:https ://vp1-voiceportal.megapath.com/Login/

到目前为止,我已经尝试了以下方法:

$username="username"; 
$password="password"; 
$url="https://vp1-voiceportal.megapath.com/Login/servlet/com.broadsoft.clients.oam.servlets.Login"; 
$cookie="cookie.txt"; 

$postdata = "EnteredUserID=".$username."&password=".$password."&domain=&UserID=&rememberPass="; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

echo $result;  
curl_close($ch);
4

1 回答 1

2

编辑:您指定的 URL 错误,应该是:

https://vp1-voiceportal.megapath.com/servlet/com.broadsoft.clients.oam.servlets.Login

并不是:

https://vp1-voiceportal.megapath.com/Login/servlet/com.broadsoft.clients.oam.servlets.Login

看起来您需要遵循重定向并指定 cookie 文件(用于读取),请尝试:

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

echo $result;  

curl_close($ch);

这也是一个很好的做法,因此请指定 cookie 文件的绝对(和可写)路径。

于 2012-05-01T07:11:43.777 回答