Getting HTTP code in PHP using curl

后端 未结 9 2090
无人共我
无人共我 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 03:52

    use this hitCurl method for fetch all type of api response i.e. Get / Post

            function hitCurl($url,$param = [],$type = 'POST'){
            $ch = curl_init();
            if(strtoupper($type) == 'GET'){
                $param = http_build_query((array)$param);
                $url = "{$url}?{$param}";
            }else{
                curl_setopt_array($ch,[
                    CURLOPT_POST => (strtoupper($type) == 'POST'),
                    CURLOPT_POSTFIELDS => (array)$param,
                ]);
            }
            curl_setopt_array($ch,[
                CURLOPT_URL => $url,
                CURLOPT_RETURNTRANSFER => true,
            ]);
            $resp = curl_exec($ch);
            $statusCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
            curl_close($ch);
            return [
                'statusCode' => $statusCode,
                'resp' => $resp
            ];
        }
    

    Demo function to test api

     function fetchApiData(){
            $url = 'https://postman-echo.com/get';
            $resp = $this->hitCurl($url,[
                'foo1'=>'bar1',
                'foo2'=>'bar2'
            ],'get');
            $apiData = "Getting header code {$resp['statusCode']}";
            if($resp['statusCode'] == 200){
                $apiData = json_decode($resp['resp']);
            }
            echo "<pre>";
            print_r ($apiData);
            echo "</pre>";
        }
    
    0 讨论(0)
  • 2020-11-28 03:58

    curl_exec is necessary. Try CURLOPT_NOBODY to not download the body. That might be faster.

    0 讨论(0)
  • 2020-11-28 04:05

    Try PHP's "get_headers" function.

    Something along the lines of:

    <?php
        $url = 'http://www.example.com';
        print_r(get_headers($url));
        print_r(get_headers($url, 1));
    ?>
    
    0 讨论(0)
  • 2020-11-28 04:08

    function getStatusCode()
    {
        $url = 'example.com/test';
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, true);    // we want headers
        curl_setopt($ch, CURLOPT_NOBODY, true);    // we don't need body
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT,10);
        $output = curl_exec($ch);
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
    
        return  $httpcode;
    }
    print_r(getStatusCode());
    
    0 讨论(0)
  • 2020-11-28 04:09
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $rt = curl_exec($ch);
    $info = curl_getinfo($ch);
    echo $info["http_code"];
    
    0 讨论(0)
  • 2020-11-28 04:14

    First make sure if the URL is actually valid (a string, not empty, good syntax), this is quick to check server side. For example, doing this first could save a lot of time:

    if(!$url || !is_string($url) || ! preg_match('/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/i', $url)){
        return false;
    }
    

    Make sure you only fetch the headers, not the body content:

    @curl_setopt($ch, CURLOPT_HEADER  , true);  // we want headers
    @curl_setopt($ch, CURLOPT_NOBODY  , true);  // we don't need body
    

    For more details on getting the URL status http code I refer to another post I made (it also helps with following redirects):

    • How can I check if a URL exists via PHP?

    As a whole:

    $url = 'http://www.example.com';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, true);    // we want headers
    curl_setopt($ch, CURLOPT_NOBODY, true);    // we don't need body
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT,10);
    $output = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    echo 'HTTP code: ' . $httpcode;
    
    0 讨论(0)
提交回复
热议问题