使用原生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
评论前必须登录!
注册