5

I'm trying to login to a remote web site using CURL, but can't seem to get it to work.

The page I'm trying to login to is: https://vp1-voiceportal.megapath.com/Login/

So far, I've tried the following:

$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);
jeffo
  • 53
  • 1
  • 1
  • 4
  • I get the following response, which reroutes me to the login page: 302 Found

    Found

    The document has moved here.


    Apache Server at vp1-voiceportal.megapath.com Port 443
    – jeffo May 01 '12 at 07:01
  • 1. How did you get your cookie file? Maybe cookies expired. 2. What is the result? What did exactly curl return? – Igor Kuznetsov May 01 '12 at 07:03
  • I might be missing something about the cookie file, how should I get it? – jeffo May 01 '12 at 07:05

1 Answers1

2

EDIT: The URL you specified is wrong, it should be:

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

And not:

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

It looks like you need to follow redirects and specify the cookie file (for reading), try:

$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);

It's also a good practice so specify an absolute (and writable) path to the cookie file.

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • Thanks, I've made the changes you suggest, but still get the same problem. – jeffo May 01 '12 at 07:17
  • @jeffo: Is the `cookie.txt` file empty? And it exists? – Alix Axel May 01 '12 at 07:18
  • @jeffo: Also, it seems to me that the URL is wrong... Shouldn't it be `https://vp1-voiceportal.megapath.com/servlet/com.broadsoft.clients.oam.servlets.Login`? – Alix Axel May 01 '12 at 07:24
  • I tried the URL you suggest, but that didn't work. The cookie file exists and is not empty. – jeffo May 01 '12 at 07:27
  • @jeffo: The URL is clearly wrong, but without having credentials or seeing the response there isn't much more I can do to help you, sorry. – Alix Axel May 01 '12 at 07:41
  • ok, thanks for your help, I'll plug away at it a bit more. The credentials are for a client, so I can't really pass them on. – jeffo May 01 '12 at 07:49
  • I made some tweaks to the $postdata variable and now it's working. Thanks to @Alix Axel. – jeffo May 01 '12 at 11:33
  • @jeffo: Really? What was the problem? Usually I just pass the array to POSTFIELDS or a string from `http_build_query()`, but unless `$username` or `$password` contain URL characters I don't see any problem in that. – Alix Axel May 01 '12 at 11:39