post请求
// 创建一个新cURL资源$ch = curl_init();// 设置URL和相应的选项curl_setopt($ch, CURLOPT_URL, $url); //$url设置需要请求的urlcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);curl_setopt($ch, CURLOPT_MAXREDIRS, 10);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);//设置请求头$headerArray = ["Content-type:application/json;charset='utf8'","Accept:application/json"]curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); //是否设置为post请求和请求参数curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));// 抓取URL并把它传递给浏览器$output = curl_exec($ch);// 关闭cURL资源,并且释放系统资源curl_close($ch);//print(json_decode($output,true))
get请求
// 创建一个新cURL资源$ch = curl_init();// 设置URL和相应的选项curl_setopt($ch, CURLOPT_URL, $url); //$url设置需要请求的urlcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);curl_setopt($ch, CURLOPT_MAXREDIRS, 10);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);//设置请求头$headerArray = ["Content-type:application/json;charset='utf8'","Accept:application/json"]curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); // 抓取URL并把它传递给浏览器$output = curl_exec($ch);// 关闭cURL资源,并且释放系统资源curl_close($ch);//print(json_decode($output,true))
put请求
// 创建一个新cURL资源$ch = curl_init()// 设置URL和相应的选项curl_setopt($ch, CURLOPT_URL, $url); //$url设置需要请求的urlcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置请求头$headerArray = ["Content-type:application/json"]curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); //设置请求方式和提交的字符串curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));// 抓取URL并把它传递给浏览器$output = curl_exec($ch);// 关闭cURL资源,并且释放系统资源curl_close($ch);//print(json_decode($output,true))
del请求
// 创建一个新cURL资源$ch = curl_init()// 设置URL和相应的选项curl_setopt($ch, CURLOPT_URL, $url); //$url设置需要请求的urlcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置请求头$headerArray = ["Content-type:application/json"]curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); //设置请求方式和提交的字符串curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"DELETE"); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));// 抓取URL并把它传递给浏览器$output = curl_exec($ch);// 关闭cURL资源,并且释放系统资源curl_close($ch);//print(json_decode($output,true))
patch请求
// 创建一个新cURL资源$ch = curl_init()// 设置URL和相应的选项curl_setopt($ch, CURLOPT_URL, $url); //$url设置需要请求的urlcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置请求头$headerArray = ["Content-type:application/json"]curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); //设置请求方式和提交的字符串curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PATCH"); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));// 抓取URL并把它传递给浏览器$output = curl_exec($ch);// 关闭cURL资源,并且释放系统资源curl_close($ch);//print(json_decode($output,true))
选项参数说明:
参数设置为 1 是把curl_exec()的结果转为字符串
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
以上是不同curl的请求实例。
来源地址:https://blog.csdn.net/qq_43636384/article/details/127884881