LeeTee
LeeTee

Reputation: 6601

HTTP response header code ignored when using cURL

I have the below code but for some reason I cannot get the correct HTTP response code as it always comes back as 0. I have tried testing a page that only has "header("HTTP/1.1 404 Not Found");" and run cURL on this page bit still I get 0. When I go direct to the page in the browser, the header code is correct. Any ideas?

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_HTTPHEADER, Array (
        "Content-Type: " . $content_type
        ));         
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPGET, TRUE);
        curl_setopt($curl, CURLOPT_USERAGENT, RESTClient :: USER_AGENT);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_HTTPHEADER, TRUE);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ); 
        curl_setopt($curl, CURLOPT_USERPWD, $auth);     
        $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        echo $httpcode;
        $result = curl_exec($curl);
        curl_close($curl);

Upvotes: 0

Views: 741

Answers (1)

Dan
Dan

Reputation: 340

Naturally, the return code can't be retrieved until after the request has been performed, which happens with the curl_exec() call. Move your curl_getinfo after that line and it should work.

Upvotes: 1

Related Questions