PHP正确设置file_get_contents函数请求头

2,656次阅读

共计 370 个字符,预计需要花费 1 分钟才能阅读完成。

通常我们使用 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 类库.

正文完
 
Blood.Cold
版权声明:本站原创文章,由 Blood.Cold 2019-06-02发表,共计370字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。