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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

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.
Hmm.. if you get the curl info (i.e. by calling curl_getinfo($ch)) after the call to curl_exec($ch), does the 'url' of the resulting array match the URL you set?
what returns false? the call to curl_exec($ch);? if so, that isn't what is desired, is it?
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.
Did you try Barmar's suggestion of using the username:password format in the URL (e.g. https://username:[email protected]) in $LoginPage?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.