0

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);

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.