php如何使用 file_get_contents 函数发送post与get请求的方法

这时我轻轻地闭上了眼睛,我好像来到童话世界,好像在和小鸟讨论秋天的美景,好像在和小草拍秋天的照片。农民伯伯在田野里收获了庄稼,果农们在果园里收获了果子,我们在学校里收获快乐、收获知识、收获成长。

在php脚本中请求远程资源链接一般都是使用curl工具,curl即可以发送GET请求也可以发送POST请求,除了curl工具之外php也提示一个 file_get_content() 工具,他的作用可curl差不多,即可以发送GET请求也可以发送POST请求,具体的使用方法,可以参考下面的内容。

php file_get_contents() 请求一个网址的方法

示例

//这里的网址请求没有任何的参数
$response = file_get_contents('http://localhost/get.php');

php file_get_contents() 发送GET请求的方法

示例:

$data = array( 'username'=>'zhezhao','userpass'=>'123');
$query = http_build_query($data);
//请求的地址
$url = 'http://localhost/get.php';
$result = file_get_contents($url.'?'.$query);

php file_get_contents() 发送POST请求的方法

示例:

$data = array( 'username'=>'zhezhao','userpass'=>'123');
$requestBody = http_build_query($data);
$context = stream_context_create(['http' => ['method' => 'POST', 'header' => "Content-Type: application/x-www-form-urlencoded\r\n"."Content-Length: " . mb_strlen($requestBody), 'content' => $requestBody]]);
$response = file_get_contents('http://localhost/get.php', false, $context);

以上就是php如何使用 file_get_contents 函数发送post与get请求的方法。作为一个领导,你可以不知道下属的短处,却不能不知道下属的长处。更多关于php如何使用 file_get_contents 函数发送post与get请求的方法请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
php get_magic_quotes_gpc()函数介绍与用法

php取消运行时间限制的方法

php中ini_set函数介绍

php获取文件夹下所有文件/文件夹

php如何实现文件下载的方法