m

全干工程师

PHP正确设置file_get_contents函数请求头

通常我们使用file_get_contents来获取一个本地文件或者网络文件内容。在请求网络文件的时候,比如要设置请求超时间,或者cookie这些。

// Create a stream
$opts = [
    "http" => [
        "method" => "GET",
        "header" => "Accept-language: en\r\n" .
            "Cookie: foo=bar\r\n",
        "timeout" => 3 //time out 3 seconds
    ]
];

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);

 建议网络文件还是使用Curl类库.

留言