2

I've figured out how to use the php curl functions to automatically log into 2 different radios but am having trouble with netgear routers.

The problem is that its login screen consists of a popup window where you enter user/pass and I can't figure out how to process it with the php curl functions.
I tried adding
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
as suggested in an answer to this question post, but it just returned an "invalid login" screen (before, it would just hang at this point so I suppose it is an improvement).

I also tried adding both
curl_setopt($curl_conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
and
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
before it and turning CURLOPT_FOLLOWLOCATION on and off but same result.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $LoginPage);//IP Address followed by :8080
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl_conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $Username . ":" . $Password);
$data = curl_exec($ch);

Update(2018/2/5):
The following gave me part of the settings page for the router! I'll probably have to mess with the html/javascript to get the rest...

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);//10 seconds
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);//kept trying options till it worked
    curl_setopt($ch, CURLOPT_USERPWD, "$Username:$Password");        
    $result = curl_exec($ch);        
    //$resultStatus = curl_getinfo($ch); 
    //print 'ResultStatus:'.print_r($resultStatus) . "<br>"; 
    curl_close($ch);  
    echo($result);
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Dustin Soodak
  • 553
  • 5
  • 15
  • You need to use the header authentication field of the post request . I'll post a complete answer as soon as I have the time for it. – Samuel Yvon Jan 04 '17 at 17:38
  • Possible duplicate of [How do I make a request using HTTP basic authentication with PHP curl?](http://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl) – Sᴀᴍ Onᴇᴌᴀ Jan 04 '17 at 18:02
  • I wonder if [Barmar's suggestion](https://stackoverflow.com/questions/48548667/php-script-to-automatically-log-into-browsers-native-login-prompt#comment84093757_48548667) of using the `username:password` format in the URL (i.e. `https://username:password@www.routerlogin.net`) would work... – Sᴀᴍ Onᴇᴌᴀ Jan 31 '18 at 19:08

2 Answers2

1

Borrowing from this answer, a header needs to be set. Instead of:

curl_setopt($curl_conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $Username . ":" . $Password);

use these options:

$headers = array(
    'Content-Type:application/json',
    'Authorization: Basic '. base64_encode("$Username:$Password")
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

And the option to verify peer's SSL certificate might also need to be set to false:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
  • When I tried replacing those options it returned "404" error but only when I tried to print or var_dump the output of curl_exec. – Dustin Soodak Jan 31 '18 at 18:44
  • Hmm.. if you get the curl info (i.e. by calling [`curl_getinfo($ch)`](http://php.net/manual/en/function.curl-getinfo.php)) after the call to `curl_exec($ch)`, does the 'url' of the resulting array match the URL you set? – Sᴀᴍ Onᴇᴌᴀ Jan 31 '18 at 18:50
  • what returns `false`? the call to `curl_exec($ch);`? if so, that isn't what is desired, is it? – Sᴀᴍ Onᴇᴌᴀ Jan 31 '18 at 19:00
  • In , curl_exec() returns false on failure (which I guess is marginally better than a result that makes php crash when it tries to do anything with it). At this point I've every combination of the responses to this question and similar ones with no result. – Dustin Soodak Feb 06 '18 at 00:13
  • Did you try [Barmar's suggestion](https://stackoverflow.com/questions/48548667/php-script-to-automatically-log-into-browsers-native-login-prompt#comment84093757_48548667) of using the `username:password` format in the URL (e.g. `https://username:password@www.routerlogin.net`) in `$LoginPage`? – Sᴀᴍ Onᴇᴌᴀ Feb 06 '18 at 00:16
1

Finally got it! The last thing I needed was curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY), which I had to get by trying the settings one at a time (see full answer in edit of question). Thanks for all the useful suggestions.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
Dustin Soodak
  • 553
  • 5
  • 15