使用带参数的file_get_contents从PHP获取请求 [英] GET Request from PHP using file_get_contents with parameters

查看:150
本文介绍了使用带参数的file_get_contents从PHP获取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向外部网站发送一个GET请求,但也想发送一些参数。例如,我必须发送一个获取请求到示例。

com



我想执行www.example.com/send.php?uid=1&pwd=2&msg=3&phone=3&provider=xyz



我的代码是:

  $ getdata = http_build_query(
'阵列(
'uid'=>'1',
'pwd'=>'2',
'msg'=>'3',
'电话'=>'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);

我得到一个服务器错误。

> c 选项与 POST一起使用 PUT 请求。对于 GET ,您可以将其附加为查询字符串:

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

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


I want to send a GET request to an external site, but also want to send some parameters

for example i've to send a get request to example.com

i want to execute www.example.com/send.php?uid=1&pwd=2&msg=3&phone=3&provider=xyz

My code is :

$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);

I get a server error .

解决方案

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);

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获取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆