我想向外部站点发送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
选项与POST
和PUT
请求一起使用。对于 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/