GET Request from PHP using file_get_contents with parameters(使用带有参数的 file_get_contents 从 PHP 获取请求)
问题描述
我想向外部站点发送GET请求,但也想发送一些参数
I want to send a GET request to an external site, but also want to send some parameters
例如,我必须向 example.com 发送获取请求
for example i've to send a get request to example.com
我想执行 www.example.com/send.php?uid=1&pwd=2&msg=3&phone=3&provider=xyz
i want to execute www.example.com/send.php?uid=1&pwd=2&msg=3&phone=3&provider=xyz
我的代码是:
$getdata = http_build_query(
array(
'uid' => '1',
'pwd' => '2',
'msg'=>'3',
'phone'=>'9999',
'provider'=>'xyz'
)
);
$opts = array('http' =>
array(
'method' => 'GET',
'content' => $getdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/send.php', false, $context);
我收到服务器错误.
推荐答案
content 选项用于 POST 和 PUT 请求.对于 GET,您可以将其附加为查询字符串:
The content option is used with POST and PUT requests. For GET you can just append it as a query string:
file_get_contents('http://example.com/send.php?'.$getdata, false, $context);
此外,method 默认为 GET,因此您甚至不需要设置选项,也不需要创建流上下文.因此,对于这种特殊情况,您可以根据需要简单地使用第一个参数调用 file_get_contents.
Furthermore, the method defaults to GET so you don't even need to set options, nor create a stream context. So, for this particular situation, you could simply call file_get_contents with the first parameter if you wish.
这篇关于使用带有参数的 file_get_contents 从 PHP 获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用带有参数的 file_get_contents 从 PHP 获取请求
基础教程推荐
- Web 服务器如何处理请求? 2021-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- php中的PDF导出 2022-01-01
- php中的foreach复选框POST 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
