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

Ana*_*nt 12 php get http 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);
Run Code Online (Sandbox Code Playgroud)

我收到服务器错误.

web*_*ave 29

content选项与POSTPUT请求一起使用.因为GET您可以将其作为查询字符串附加:

file_get_contents('http://example.com/send.php?'.$getdata, false, $context);
Run Code Online (Sandbox Code Playgroud)

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