Getting HTTP code in PHP using curl

后端 未结 9 2091
无人共我
无人共我 2020-11-28 03:43

I\'m using CURL to get the status of a site, if it\'s up/down or redirecting to another site. I want to get it as streamlined as possible, but it\'s not working well.

<
相关标签:
9条回答
  • 2020-11-28 04:16

    Here is my solution need get Status Http for checking status of server regularly

    $url = 'http://www.example.com'; // Your server link
    
    while(true) {
    
        $strHeader = get_headers($url)[0];
    
        $statusCode = substr($strHeader, 9, 3 );
    
        if($statusCode != 200 ) {
            echo 'Server down.';
            // Send email 
        }
        else {
            echo 'oK';
        }
    
        sleep(30);
    }
    
    0 讨论(0)
  • 2020-11-28 04:17
    // must set $url first....
    $http = curl_init($url);
    // do your curl thing here
    $result = curl_exec($http);
    $http_status = curl_getinfo($http, CURLINFO_HTTP_CODE);
    curl_close($http);
    echo $http_status;
    
    0 讨论(0)
  • 2020-11-28 04:17

    curl_getinfo — Get information regarding a specific transfer

    Check curl_getinfo

    <?php
    // Create a curl handle
    $ch = curl_init('http://www.yahoo.com/');
    
    // Execute
    curl_exec($ch);
    
    // Check if any error occurred
    if(!curl_errno($ch))
    {
     $info = curl_getinfo($ch);
    
     echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
    }
    
    // Close handle
    curl_close($ch);
    
    0 讨论(0)
提交回复
热议问题