久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

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

168次閱讀
沒有評論

共計 1464 個字符,預計需要花費 4 分鐘才能閱讀完成。

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

丸趣 TV 網 – 提供最優質的資源集合!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-12-13發表,共計1464字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 库尔勒市| 宽甸| 安乡县| 上林县| 乌恰县| 白朗县| 游戏| 延庆县| 沿河| 喀喇沁旗| 浦县| 开封市| 科技| 瓮安县| 蒙阴县| 枞阳县| 河东区| 秭归县| 壶关县| 衡南县| 册亨县| 慈溪市| 彰化县| 南阳市| 紫云| 鄂托克前旗| 沽源县| 建宁县| 思茅市| 新源县| 三明市| 大竹县| 玛纳斯县| 和政县| 尼木县| 田林县| 赤壁市| 武川县| 温泉县| 镇康县| 公主岭市|