Check headers in PHP cURL server response

后端 未结 1 1400
忘了有多久
忘了有多久 2021-01-15 00:15

I\'ve been using PHP curl to get data I need from remote website. Here is the cURL function I used:

function get_content($adr)  
    {  
       $ch = curl_in         


        
1条回答
  •  别那么骄傲
    2021-01-15 00:53

    If my comment above is correct, change:

    curl_setopt($ch, CURLOPT_HEADER, 0);
    

    to:

    curl_setopt($ch, CURLOPT_HEADER, 1);
    

    and parse the returned headers out however you see fit. Note that just changing the above in your function will return both headers and content, so if you want only headers returned:

    function http_head_curl($url,$timeout=10)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // in seconds
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_NOBODY, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($ch);
        if ($res === false) {
            throw new RuntimeException("cURL exception: ".curl_errno($ch).": ".curl_error($ch));
        }
        return trim($res);
    }
    

    0 讨论(0)
提交回复
热议问题