php的file_get_contents可以指定请求参数吗?

作者: dreamfly 分类: php,个人博客 发布时间: 2019-04-14 21:35

file_get_conntents函数经常被我们用来获取网络请求,比如获取url源码,获取图片。

但是有时候,获取图片数据需要指定referer,我们的file_get_contents还可以使用吗?答案是肯定的。

$option = array(

'http' => array(

'header' => "Referer:$refer", ),

);

 


$img = @file_get_contents($url, false, stream_context_create($option));

这里我们通过file_get_contents的第三个函数,就可以指定header,比如发送cookie,发送header等。

所以我们学习知识一定要扎实,如果只是一知半解,很容易不了解这个函数的其它参数的用途,也就不能更好的,而优雅的写出代码。

它的五个参数分别是:

file_get_contents($path, $include_path, $context, 
                              $start, $max_length)
Parameters: The file_get_contents() function in PHP accepts one mandatory parameter and four optional parameters.

$path: It specifies the path of the file or directory you want to check.
$include_path: It is an optional parameter which searches for a file in the file in the include_path (in php.ini) also if it is set to 1.

$context: It is an optional parameter which is used to specify a custom context.

$start: It is an optional parameter which is used to specify the starting point in the file for reading.

$max_length: It is an optional parameter which is used to specify the number of bytes to be read.

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!