Accessing Actiontec modem screens via PHP - php

I have an Actiontec V1000H router. I want to access its "WAN Ethernet Status" page using a script (which will extract the sent and received packet counts for plotting). From a browser, this URL works fine:
http://192.168.1.1/modemstatus_wanethstatus.html
But, when I use that URL in my script, I nearly always get the main screen. (It works on rare occasions.) Here's my script:
$wanStatusUrl = "http://192.168.1.1/modemstatus_wanethstatus.html";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $wanStatusUrl);
curl_setopt($ch, CURLOPT_USERPWD, 'admin:myPassword');
$output = curl_exec($ch);
curl_close($ch);
I need help accessing the modemstatus_wanethstatus.html page. I believe the issue is due to some idiocycracy of the modem.

Use this so that curl return you the html source as response into your $output:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

The main screen has a "login" button, and adding the equivalent of that prior to accessing the WAN Status screen made it work. So, for the record:
// login
$loginUrl = 'http://192.168.1.1/login.cgi?inputUserName=admin&inputPassword=myPassword&nothankyou=1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_exec($ch);
curl_close($ch);
// get status page
$wanStatusUrl = "http://192.168.1.1/modemstatus_wanethstatus.html";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $wanStatusUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // so curl_exec returns the response
$responseText = curl_exec($ch);
curl_close($ch);
// print $responseText; // contains wanEthStatus_ReceivedPackets and wanEthStatus_SendPackets
// get the two packet counts ... wanEthStatus_ReceivedPackets and wanEthStatus_SendPackets
preg_match( "/wanEthStatus_ReceivedPackets.*?\'(\d+)\';.*?\'(\d+)\';.*?wanEthStatus_TimeSpan/s", $responseText, $matches );
print_r( $matches );
"Man Always Wins in the End."

Related

CURL Sends Requests Twice For Single Request

I am using the following code to send a CURL request to send a SMS. But the SMS is being sent twice.
$message = urlencode($message);
$smsurl = "http://$url/sendmessage.php?user=matkaon&password=$password&mobile=$mobile&message=$message&sender=$sender&type=3";
$ch = curl_init($smsurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$sentsms = curl_exec($ch);
curl_close($ch);
I tried commenting some of the lines which solved the issue but gives an output as below:
What is the proper method to send a CURL request only once?
Try this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $smsurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
Don't pass the URL as argument on the init function.
I don't know why the function is being called twice, but I never pass the URL as argument and always work great this way.
You'd usually use curl_init() with no parameters, then pass the URL into curl_exec.
Modified example 1 from curl_exec docs:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $smsurl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>

Retreiving a 3rd-party webpage with CURL/PHP - not entirely working

I am writing a tool that accesses a set of external website pages. Here is my test code to see if I can retrieve the page:
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/* curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); */
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data('http://www.imdb.com/');
echo $returned_content;
The thing is when I pass Google (for example) as the URL, I get the Google homepage in my browser (sans images for obvious reasons), but when I pass the site I want to see, www.imdb.com, I get nothing. Why is this, and what can I do about it?

why Instagram returns blank to CURL request?

i write following code to get html data from url and its working for https site like Facebook but not working for Instagram only.
Instagram returns the blank
<?php
$url = 'https://www.instagram.com';
$returned_content = get_data($url);
print_r($returned_content)
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
The Instagram will return only javascript, that can't be render by your browser because it uses dynamic path, so <script src='/path/file.js'> will try to get localhost/path/file.js instead of instagram.com/path/file.js and in this situation the localhost/path/file.js not will exist, so the page will be blank.
One solution is find a way to give the full HTML instead of the Javascript, in this case you can use the "User-Agent" to do this trick. You might know that JS not handle by the search-engine, so for this situation the Instagram (and many websites) give the page without JS that is supported by the bot.
So, add this:
curl_setopt($ch, CURLOPT_USERAGENT, "ABACHOBot");
The "ABACHOBot" is one Crawler. In this page you can found many others alternatives, like a "Baiduspider", "BecomeBot"...
You can use "generic" user-agent too, like "bot", "spider", "crawler" and probably will work too.
Here try this on
<?php
$url = 'https://www.instagram.com';
$returned_content = get_data($url);
print_r($returned_content);
/* gets the data from a URL */
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//Update.................
curl_setopt($ch, CURLOPT_USERAGENT, 'spider');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
//....................................................
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
You should pass
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false)
and other header info as above.
For more detail,Please see
http://stackoverflow.com/questions/4372710/php-curl-https

CURL post sending PHP

Please look the codes as below :
$url='https://www.test.com/test.php';
$post='?field1=1&field2=2&filed3'; // no need array text as is
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_exec ($ch);
curl_close ($ch)
Should be simple code, I used as reference http://curl.haxx.se/libcurl/php/examples/simplepost.html
I modified the code by replacing to variables.
I need to send data to remote server which belong to third party. Other side's server have data base. When I copy manually the www.test.com/test.php?field1=1&field2=2&filed3 into web browser's then the data saved into data base at other server and having respond {"Code":15,"Msg":null"} on browser screen, that's mean data sent properly. When trying to send by PHP script, the data not save in remote data base also not getting respond message.
If the request works when you enter it into a web browser, chances are it is a GET rather than a POST request. A simple example of doing that would be as follows:
$url= 'http://www.test.com/test.php?field1=1&field2=2&filed3';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
var_dump($http_status);
var_dump($result);
I've also set the CURLOPT_RETURNTRANSFER option so you can capture the response in $result.

Unable to use file_get_contents(), returns nothing

I'm trying to get some data from a website that is not mine, using this code.
<?
$text = file_get_contents("https://ninjacourses.com/explore/4/");
echo $text;
?>
However, nothing is being echo'd, and the string length is 0.
I've done this method before, and it has worked no problem, but with this website, it is not working at all.
Thanks!
I managed to get the contents using curl like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://ninjacourses.com/explore/4/");
$result = curl_exec($ch);
curl_close($ch);
cURL is a way you can hit a URL from your code to get a html response from it. cURL means client URL which allows you to connect with other URLs and use their responses in your code
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://ninjacourses.com/explore/4/");
$result = curl_exec($ch);
curl_close($ch);
i think this is useful for you curl-with-php and another

Categories