HTTP requests with file_get_contents, getting the response code

前端 未结 5 2144
眼角桃花
眼角桃花 2020-11-27 15:39

I\'m trying to use file_get_contents together with stream_context_create to make POST requests. My code so far:

    $options = arra         


        
5条回答
  •  渐次进展
    2020-11-27 16:11

    Adding few more lines to the accepted response to get the http code

    function getHttpCode($http_response_header)
    {
        if(is_array($http_response_header))
        {
            $parts=explode(' ',$http_response_header[0]);
            if(count($parts)>1) //HTTP/1.0  
                return intval($parts[1]); //Get code
        }
        return 0;
    }
    
    @file_get_contents("http://example.com");
    $code=getHttpCode($http_response_header);
    

    to hide the error output both comments are ok, ignore_errors = true or @ (I prefer @)

提交回复
热议问题