中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

php發送get、post請求的6種方法簡明總結

PHP
小云
161
2023-08-16 13:09:16
欄目: 編程語言

  1. 使用原生的PHP函數發送GET請求:
$data = array(
'param1' => 'value1',
'param2' => 'value2'
);
$url = 'http://example.com/api?' . http_build_query($data);
$response = file_get_contents($url);
  1. 使用原生的PHP函數發送POST請求:
$data = array(
'param1' => 'value1',
'param2' => 'value2'
);
$options = array(
'http' => array(
'method' => '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);
  1. 使用cURL庫發送GET請求:
$ch = curl_init();
$data = array(
'param1' => 'value1',
'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);
  1. 使用cURL庫發送POST請求:
$ch = curl_init();
$data = array(
'param1' => 'value1',
'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);
  1. 使用Guzzle庫發送GET請求:
$client = new GuzzleHttp\Client();
$data = array(
'param1' => 'value1',
'param2' => 'value2'
);
$response = $client->get('http://example.com/api', [
'query' => $data
]);
$body = $response->getBody();
  1. 使用Guzzle庫發送POST請求:
$client = new GuzzleHttp\Client();
$data = array(
'param1' => 'value1',
'param2' => 'value2'
);
$response = $client->post('http://example.com/api', [
'form_params' => $data
]);
$body = $response->getBody();

0
建平县| 兰西县| 甘南县| 西畴县| 融水| 临城县| 新巴尔虎右旗| 桐柏县| 阿拉善左旗| 嫩江县| 漯河市| 霞浦县| 夏河县| 江永县| 长子县| 万载县| 台东县| 霍州市| 长泰县| 曲松县| 合水县| 淮滨县| 赞皇县| 临颍县| 收藏| 集贤县| 青冈县| 抚顺市| 青神县| 天峨县| 灵山县| 辽宁省| 临沧市| 安徽省| 子长县| 永州市| 汽车| 蒲江县| 手机| 平顶山市| 克拉玛依市|