php使用file_get_contents请求url获取返回状态码

使用 file_get_contents 请求 URL 并获取返回状态码可以使用以下代码:

```php
$url = "http://www.example.com";
$options = array(
    'http' => array(
        'method' => 'GET'
    )
);
$context = stream_context_create($options);
$contents = file_get_contents($url, false, $context);
$headers = $http_response_header;
$status_code = explode(' ', $headers[0])[1];

echo $status_code;
```

这里的 `$url` 是你要请求的 URL,`$options` 数组是请求选项,`$context` 是根据选项创建的流上下文。`file_get_contents` 函数会返回 URL 的内容,并将响应头存储在变量 `$http_response_header` 中。从响应头获取状态码并将其存储在 `$status_code` 变量中,然后就可以进行处理了。

相关代码参考