博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP用curl发送get post put delete patch请求
阅读量:4646 次
发布时间:2019-06-09

本文共 2554 字,大约阅读时间需要 8 分钟。

function getUrl($url) {
$headerArray = array("Content-type:application/json;", "Accept:application/json"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray); $output = curl_exec($ch); curl_close($ch); $output = json_decode($output, true); return $output; } function postUrl($url, $data) {
$data = json_encode($data); $headerArray = array("Content-type:application/json;charset='utf-8'", "Accept:application/json"); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return json_decode($output, true); } function putUrl($url, $data) {
$data = json_encode($data); $ch = curl_init(); //初始化CURL句柄 curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); //设置请求方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串 $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); } function delUrl($url, $data) {
$data = json_encode($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $output = curl_exec($ch); curl_close($ch); $output = json_decode($output, true); } function patchUrl($url, $data) {
$data = json_encode($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); }

转载于:https://www.cnblogs.com/ryanlamp/p/7743645.html

你可能感兴趣的文章
Windows Phone开发基础(7)《101 Windows Phone 7 Apps》Weight Tracker 提供几种体重发展(折线图 趋势图)...
查看>>
Cookie seesion 赋值
查看>>
winFrom程序更新自动安装
查看>>
Mysql数据类型
查看>>
1.2 日志框架
查看>>
Python 多进程、多线程效率比较
查看>>
设计模式之(十一)代理模式(Proxy)
查看>>
OWC组件生成Excel数据表
查看>>
MySQL基础
查看>>
Largest Rectangle in a Histogram
查看>>
仿QQ右下角弹出可关闭的消息框: 转载
查看>>
ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(七) 之 历史记录查询(时间,关键字,图片,文件),关键字高亮显示。...
查看>>
Unity 游戏框架搭建 (二十三) 重构小工具 Platform
查看>>
软件工程结对作业02
查看>>
【设计模式】策略模式与状态模式。
查看>>
Eclipse经验总结
查看>>
(转)[Unity3D]UI方案及制作细节(NGUI/EZGUI/原生UI系统) 内附unused-assets清除实例
查看>>
免费收录网站搜索引擎登录口
查看>>
配置Nginx反向代理服务器
查看>>
浅析敏捷开发与传统软件开发的区别
查看>>