关注分享主机优惠活动
国内外VPS云服务器

php发送get和post请求的六种方法的简明摘要(php发送get请求以获取回数据)

使用原生PHP函数发送GET请求:$data = array(
' param1' = > '值1 ',
' param2' => 'value2 '
);
$url = 'http://example.com/api?'。http _ build _ query($ data);
$ response = file _ get _ contents($ URL);使用原生PHP函数发送POST请求:$data = array(
' param1' = > '值1 ',
' param2' => 'value2 '
);
$options = array(
http' = >数组(
方法' => 'POST ',
header ' = > ' Content-Type:application/x-www-form-urlencoded ',
content ' = > http _ build _ query($ data)
)
);
$ context = stream _ context _ create($ options);
$ response = file _ get _ contents(' http://example . com/API ',false,$ context);使用cURL库发送GET请求:$ ch = cURL _ init();
$data = array(
' param1' = > '值1 ',
' param2' => 'value2 '
);
$url = 'http://example.com/api?'。http _ build _ query($ data);
curl_setopt($ch,CURLOPT_URL,$ URL);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$ response = curl _ exec($ ch);
curl _ close($ ch);使用cURL库发送POST请求:$ ch = cURL _ init();
$data = array(
' param1' = > '值1 ',
' param2' => 'value2 '
);
curl_setopt($ch,CURLOPT_URL,' http://example . com/API ');
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,http _ build _ query($ data));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$ response = curl _ exec($ ch);
curl _ close($ ch);使用Guzzle库发送GET请求:$ Client = new Guzzle http \ Client();
$data = array(
' param1' = > '值1 ',
' param2' => 'value2 '
);
$ response = $ client-> get(' http://example . com/API ',[
query' => $data
]);
$ body = $ response-> getBody();使用Guzzle库发送POST请求:$ Client = new Guzzle http \ Client();
$data = array(
' param1' = > '值1 ',
' param2' => 'value2 '
);
$ response = $ client-> post(' http://example . com/API ',[
form_params' => $data
]);
$ body = $ response-> getBody();

以上内容来自互联网,不代表本站全部观点!欢迎关注我们:zhujipindao。com

未经允许不得转载:主机频道 » php发送get和post请求的六种方法的简明摘要(php发送get请求以获取回数据)

评论 抢沙发

评论前必须登录!