Determine in php script if connected to internet?

2019-01-08 07:24发布

How can I check if I'm connected to the internet from my PHP script which is running on my dev machine?

I run the script to download a set of files (which may or may not exist) using wget. If I try the download without being connected, wget proceeds to the next one thinking the file is not present.

10条回答
姐就是有狂的资本
2楼-- · 2019-01-08 07:45

You can always ping good 'ol trusty google:

$response = null;
system("ping -c 1 google.com", $response);
if($response == 0)
{
    // this means you are connected
}
查看更多
Juvenile、少年°
3楼-- · 2019-01-08 07:45

There are various factors that determine internet connection. The interface state, for example. But, regardles of those, due to the nature of the net, proper configuration does not meen you have a working connection.

So the best way is to try to download a file that you’re certain that exists. If you succeed, you may follow to next steps. If not, retry once and then fail.

Try to pick one at the destination host. If it’s not possible, choose some major website like google or yahoo.

Finally, just try checking the error code returned by wget. I bet those are different for 404-s and timeouts. You can use third parameter in exec call:

string exec ( string $command [, array &$output [, int &$return_var ]] )

查看更多
倾城 Initia
4楼-- · 2019-01-08 07:50

You could ping to a popular site or to the site you're wgetting from (like www.google.nl) then parse the result to see if you can connect to it.

<?php
$ip = '127.0.0.1'; //some ip
exec("ping -n 4 $ip 2>&1", $output, $retval);
if ($retval != 0) { 
echo "no!"; 
} 
else 
{ 
echo "yes!"; }
?>
查看更多
男人必须洒脱
5楼-- · 2019-01-08 07:52
<?php
function is_connected()
{
    $connected = @fsockopen("www.example.com", 80); 
                                        //website, port  (try 80 or 443)
    if ($connected){
        $is_conn = true; //action when connected
        fclose($connected);
    }else{
        $is_conn = false; //action in connection failure
    }
    return $is_conn;

}
?>
查看更多
劳资没心,怎么记你
6楼-- · 2019-01-08 07:52

Just check the result of wget. A status code of 4 indicates a network problem, a status code of 8 indicates a server error (such as a 404). This only works if you call wget for each file in sequence, rather than once for all the files.

You can also use libcurl with PHP, instead of calling wget. Something like:

foreach (...) {
    $c = curl_init($url);
    $f = fopen($filepath, "w")
    curl_setopt($c, CURLOPT_FILE, $f);
    curl_setopt($c, CURLOPT_HEADER, 0);
    if (curl_exec($c)) {
        if (curl_getinfo($c, CURLINFO_HTTP_CODE) == 200) {
            // success
        } else {
            // 404 or something, delete file
            unlink($filepath);
        }
    } else {
        // network error or server down
        break; // abort
    }
    curl_close($c);
}
查看更多
成全新的幸福
7楼-- · 2019-01-08 07:53

The accepted answer did not work for me. When the internet was disconnected it threw a php error. So I used it with a little modification which is below:

if(!$sock = @fsockopen('www.google.com', 80))
{
    echo 'Not Connected';
}
else
{
echo 'Connected';
}
查看更多
登录 后发表回答