PHP get_headers() alternative

后端 未结 3 2013
走了就别回头了
走了就别回头了 2020-12-28 10:46

I need a PHP script that reads the HTTP response code for each URL request.

something like

$headers = get_headers($theURL);
return substr($headers         


        
3条回答
  •  旧时难觅i
    2020-12-28 11:24

    If cURL is enabled, you can use it to get the whole header or just the response code. The following code assigns the response code to the $response_code variable:

    $curl = curl_init();
    curl_setopt_array( $curl, array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_URL => 'http://stackoverflow.com' ) );
    curl_exec( $curl );
    $response_code = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
    curl_close( $curl );
    

    To get the whole header you can issue a HEAD request, like this:

    $curl = curl_init();
    curl_setopt_array( $curl, array(
        CURLOPT_HEADER => true,
        CURLOPT_NOBODY => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_URL => 'http://stackoverflow.com' ) );
    $headers = explode( "\n", curl_exec( $curl ) );
    curl_close( $curl );
    

提交回复
热议问题