curl post与get请求
get 请求 [常用]
// curl初始化
$ch = curl_init($url);
// 设置 curl 参数
// 将头部文件的信息作为数据流输出
curl_setopt($ch, CURLOPT_HEADER, false);
// 执行 curl
$ret = curl_exec($ch);
// curl 关闭请求
curl_close();
post 请求[常用]
// curl初始化
$ch = curl_init($url);
// 设置 curl 参数
// 将头部文件的信息作为数据流输出
curl_setopt($ch, CURLOPT_HEADER, false);
// post 请求
curl_setopt($ch, CURLOPT_POST. true);
// post 传参
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
// 设置头部信息 [get 方式一般不用]
$headerArr = [
'Content-Type: application/json',
'Content-Length: '.strlen($jsonStr)
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
// 执行 curl
$ret = curl_exec($ch);
// curl 关闭请求
curl_close();
注:
1,当传参类型为 application/json 时,只能用 file_get_content(‘php://input’) 来接收。
2,$_POST 只能接收传参类型为 application/x-www-form-urlencoded || multipart/from-data 两种类型