php的file_get_contents可以指定请求参数吗?
file_get_conntents函数经常被我们用来获取网络请求,比如获取url源码,获取图片。
但是有时候,获取图片数据需要指定referer,我们的file_get_contents还可以使用吗?答案是肯定的。
这里我们通过file_get_contents的第三个函数,就可以指定header,比如发送cookie,发送header等。
所以我们学习知识一定要扎实,如果只是一知半解,很容易不了解这个函数的其它参数的用途,也就不能更好的,而优雅的写出代码。
它的五个参数分别是:
-
filename
-
要读取的文件的名称。
-
use_include_path
-
context
-
A valid context resource created with stream_context_create(). 如果你不需要自定义 context,可以用
NULL
来忽略。 -
offset
-
The offset where the reading starts on the original stream.
Seeking (
offset
) is not supported with remote files. Attempting to seek on non-local files may work with small offsets, but this is unpredictable because it works on the buffered stream. -
maxlen
-
Maximum length of data read. The default is to read until end of file is reached. Note that this parameter is applied to the stream processed by the filters.