php - 使用带参数的 file_get_contents 从 PHP 获取请求

标签 php http get file-get-contents

我想向外部站点发送GET请求,还想发送一些参数

例如,我必须向 example.com 发送一个获取请求

我想执行 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 选项与POSTPUT 请求一起使用。对于 GET,您可以将其附加为查询字符串:

file_get_contents('http://example.com/send.php?'.$getdata, false, $context);

此外,方法默认为GET,因此您甚至不需要设置选项,也不需要创建流上下文。因此,对于这种特殊情况,您可以根据需要简单地使用第一个参数调用 file_get_contents

关于php - 使用带参数的 file_get_contents 从 PHP 获取请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7352832/

相关文章:

javascript - 如何在php、jquery中设置任何浏览器的默认下载目录

java - 如何通过网络发送 FlatBuffers ByteBuffer?

java - 从 BufferedReader 中提取行时无法退出 Java 中的 while 循环

javascript - 使用 JavaScript jQuery 从 url 获取 GET 变量

java - HTTP客户端408状态码

php - 页面与页面之间的 session 不同的问题

php - 在 Controller 中使用爬虫

php - 提取多维数组一部分的有效方法

django - 插入 Django 网络应用程序中的基本安全漏洞

javascript - 是否可以读取另一台服务器中的 json 文件,如何读取?