php利用curl发送 post get del put patch 请求
因为需要在php开发中对接其它接口需要用php curl去对接其它接口 我把他们封装成函数 希望能对大家有所帮助
这里面是封装好的 会自动把data进行转成json格式 同时解码成php数组输出
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($url,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,$put_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); //20170611修改接口,用/id的方式传递,直接写在url中了
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output);
return $output;
}
php利用curl发送 post get del put patch 请求的更多相关文章
- PHP 利用 curl 发送 post get del put patch 请求
因为需要在 php 开发中对接其它接口需要用 php curl 去对接其它接口 我把他们封装成函数 希望能对大家有所帮助. 这里面是封装好的会自动把 data 进行转成 json 格式,同时解码成 p ...
- PHP用curl发送get post put delete patch请求
function getUrl($url){ $headerArray = array("Content-type:application/json;", "Accept ...
- 利用 CURL 发送JSON格式字符串
/* * post 发送JSON 格式数据 * @param $url string URL * @param $data_string string 请求的具体内容 * @return array ...
- php 中使用cURL发送get/post请求,上传图片,批处理
cURL是利用url语法规定传输文件和数据的工具.php中有curl拓展,一般用来实现网络抓取,模拟发送get post请求,文件上传. 在php中建立curl的基本步骤如下: 1 初始化 ...
- PHP利用Curl实现多线程抓取网页和下载文件
PHP 利用 Curl 可以完成各种传送文件操作,比如模拟浏览器发送GET,POST请求等等,然而因为php语言本身不支持多线程,所以开发爬虫程序效率并不高,一般采集 数据可以利用 PHPquery ...
- linux下利用curl监控网页shell脚本
#!/bin/bash smail() {mail -s "$1" gjw_apparitor@gmail.com <<EOF$1$2====report time: ...
- php 利用socket发送GET,POST请求
作为php程序员一定会接触http协议,也只有深入了解http协议,编程水平才会更进一步.最近我一直在学习php的关于http的编程,许多东西恍然大悟,受益匪浅.希望分享给大家.本文需要有一定http ...
- 利用curl并发来提高页面访问速度
在我们平时的程序中难免出现同时访问几个接口的情况,平时我们用curl进行访问的时候,一般都是单个.顺序访问,假如有3个接口,每个接口耗时500毫 秒那么我们三个接口就要花费1500毫秒了,这个问题太头 ...
- 学了C语言,如何利用cURL写一个程序验证某个网址的有效性?
在<C程序设计伴侣>以及这几篇关于cURL的文章中,我们介绍了如何利用cURL写一个下载程序,从网络下载文件.可是当我们在用这个程序下载文件时,又遇到了新问题:如果这个网址是无效的,那么我 ...
随机推荐
- 关于torch.norm函数的笔记
先看一下它的参数: norm(p='fro', dim=None, keepdim=False, dtype=None) p: the order of norm. 一般来说指定 $p = 1, 2$ ...
- LVM中逻辑卷的最大大小限制
前言: 本文是对这篇博客Maximum Size Of A Logical Volume In LVM的翻译,敬请尊重原创和翻译劳动成果,那些随意转载的大爷们,好歹也自觉注明出处.谢谢! 英文原文地址 ...
- 一天一个设计模式——Builder建造者模式
一.模式说明 在现实世界中,当我们要构造一个大型工程时(建一个大楼),通常的做法是先建造工程的每个独立部分,然后再逐步构造完成(先打地基,再搭框架,最后逐层累造).在程序设计领域,构造一个复杂的类时( ...
- 不同的二叉搜索树&II
不同的二叉搜索树 只要求个数,递推根节点分割左右子树即可 class Solution { public int numTrees(int n) { int []dp=new int[n+1]; fo ...
- 洛谷 P5661 公交换乘(队列)
题目传送门 解题思路: 暴力模拟. AC代码: #include<iostream> #include<cstdio> #include<queue> using ...
- cors跨域和jsonp劫持漏洞 和 同源策略和跨域请求解决方案
cors跨域和jsonp劫持漏洞: https://www.toutiao.com/a6759064986984645127/ 同源策略和跨域请求解决方案:https://www.jianshu.co ...
- tomcat8.5的安装、卸载、配置和部署
安装和卸载 下载 http://tomcat.apache.org/ 环境变量 1.点击此电脑 右键—>属性. 2.创建变量名为CATALINA_HOME,的值为所解压安装tomcat的本地目录 ...
- Python—异步任务队列Celery简单使用
一.Celery简介 Celery是一个简单,灵活,可靠的分布式系统,用于处理大量消息,同时为操作提供维护此类系统所需的工具.它是一个任务队列,专注于实时处理,同时还支持任务调度. 中间人boker: ...
- Cannot find tomcat目录/bin/setclasspath.sh This file is needed to run this program
首先如果直接使用 root 用户来启动 tomcat 的话,是可以正常启动的. 但是我们在 Linux 中使用普通用户启动 tomcat 报了如下错误 Cannot find /developer/a ...
- 3)在View中添加LBUTTONDOWN(标准消息)
1)消息一共分为四类: (1)标准消息-->以WM_ 开头的都是标准消息 (2)命令消息----> 菜单 工具条 快捷键(两个按键的组合是快捷键,一个按键是 WM_KEYDOWN( ...