class HttpCurl
{
//控客云平台的appid
private $appId = xxxxxx;
//控客云平台的appkey
private $appKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'; //控客接口请求地址
protected $url = 'https://xxxxxxxxxxx/1.0/';
//接口请求要求host必须为api.developer.hijaytech.com
protected $host = 'api.developer.hijaytech.com'; //设置的接口请求头信息集合
protected $headerArr = '';
protected $pragma = 'no-cache';
protected $accept = 'application/json';
protected $contentType = 'application/json'; public function __construct()
{
$this->setHeader();
} /**
* 设置请求接口的头信息,开发文档中的一些固定的头信息参数
*/
protected function setHeader()
{
$arr = array(
'Host:' . $this->host,
'Pragma:' . $this->pragma,
'Accept:' . $this->accept,
'Content-Type:' . $this->contentType,
'appId:' . $this->appId,
'appKey:' . $this->appKey,
);
$this->headerArr = $arr;
} /**
* 接口的请求方法
* @param string $url 接口的具体访问地址
* @param array $data 请求数据 默认是get方式请求。为空数组
* @param string $method 接口的请求方式post,get,delete,put
* @param array $header 接口的请求头,拓展,在默认固定的基础上可以再添加
* @return array 数据结果集合array(
* 'res' => $res, //请求状态是失败还是成功
* 'mes' => $mes, //请求失败的错误信息
* 'data' => $data //请求失败对应的数据
* );
*/
protected function curlRequest($url, $data = array(), $method = 'post', $header = array())
{ //初始化curl_init()
$ch = curl_init();
//设置请求的url
curl_setopt($ch, CURLOPT_URL, $url);
//设置是否接受返回结果
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 显示返回的Header区域内容
curl_setopt($ch, CURLOPT_HEADER, false); //设置端口号
curl_setopt($ch, CURLOPT_PORT, 4432); //https请求时会要求验证证书(ssl证书)
// $sslPath = ''; // $ssl = substr($url, 0, 8) == 'https://' ? true : false;
// if($ssl){
// curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true);
// curl_setopt($ch,CURLOPT_CAINFO,$sslPath);
// curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);
// }else{
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //这个是重点,跳过证书
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
// }
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //这个是重点,跳过证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在 //根据不同的请求方式设置对应的参数
switch ($method) {
case 'get':
curl_setopt($ch, CURLOPT_HTTPGET, true);
break;
case 'post':
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
break;
case 'delete':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
case 'put':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
break;
default :
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
break;
} //自定义请求头信息
if (count($header) <= 0) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headerArr);
} else {
$headers = $this->headerArr;
$headerArr = array_merge($headers, $header);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
}
//返回一个json数据
$result = curl_exec($ch); if (curl_error($ch)) {
$resArr = $this->returnRes('fail', curl_error($ch), $data);
curl_close($ch);
return $resArr;
}
curl_close($ch); //将返回数据格式化为数组形式
$resData = json_decode($result, true); if(isset($resData['error_code'])&&$resData['error_code']!==0){
$resArr = $this->returnRes('fail', $resData['error_msg'], $data);
return $resArr;
} //统一返回的数据格式
$resArr = $this->returnRes('success', '', $resData);
return $resArr;
} public function returnRes($res, $mes, $data)
{
return array(
'res' => $res, //请求状态是失败还是成功
'mes' => $mes, //请求失败的错误信息
'data' => $data //请求失败对应的数据
);
} }

curl请求的get.post.put.delete对接其他系统接口方法的更多相关文章

  1. PHP 之CURL请求封装GET、POST、PUT、DELETE

    /** * @Description: curl请求 * @Author: Yang * @param $url * @param null $data * @param string $method ...

  2. php curl请求和获取接口数据

    curl请求和获取接口数据 class ToolModel{ /** * [http 调用接口函数] * @Author GeorgeHao * @param string $url [接口地址] * ...

  3. curl请求的url中含有空格

    curl请求的url中含有空格时(例如rul的参数是sql查询语句,url=www.tets.com/query.php?sql=select * from t1),curl_easy_perform ...

  4. Curl 请求数据多’1‘

    今天做curl请求时遇到一个问题 数据请求回来,无缘无故多了1 加上这一行代码就就可以了:curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

  5. php(curl请求)测试接口案例

    请求测试接口,如下: $data = [']; $result = curlrequest($apiUrl,$data); ){ echo json_encode($result); }else{ e ...

  6. php curl请求回来的中文为乱码

    在浏览器访问回来的编码格式是正常的,但是从php curl 请求过来的确实乱码,之前也试过这个函数好像不行,今天吧最后一个参数换了,简单粗暴,可以了mb_convert_encoding($res, ...

  7. php-fpm nginx 使用 curl 请求 https 出现 502 错误

    用php curl请求https的url出现502错误,请求帮忙解决. PHP版本:5.6.7Nginx版本:1.8.0 代码如下: $ch = curl_init(); curl_setopt($c ...

  8. php之接口内curl请求其他接口

    今天遇到一个需要写curl的需求,情况是这样的: 同一应用的A系统(购物系统),B系统(答题系统)相互独立,用户数据全部存在于A系统的数据库中, 现在处于B系统的某项操作中,需要在B系统中验证当前请求 ...

  9. PHP使用curl请求https站点的常见错误及解决方案

    使用curl请求http站点和https站点最大的不同就是https站点有证书验证这一环节,如果证书验证不通过则无法发起请求,不管是请求什么类型的站点遇到问题时先把错误码打印出来看一下,打印错误码的代 ...

随机推荐

  1. 一个RPC的demo (good)

    从下面的例子中可以看到,Consumer(client)的代码中引用了Provider部分的class,本例中是 com.provider.EchoServiceImpl和com.provider.E ...

  2. 【WPF】右下角弹出自定义通知样式(Notification)——简单教程

    原文:[WPF]右下角弹出自定义通知样式(Notification)--简单教程 1.先看效果 2.实现 1.主界面是MainWindow 上面就只摆放一个Button即可.在Button的点击事件中 ...

  3. Windows 10开发基础——文件、文件夹和库(二)

    主要内容: 使用选取器打开和保存文件 关于文件.文件夹和库,如果深究其实还是有比较多的内容,我们这一次来学习一下选取器就收了.还有上篇博文中读写文本文件的三种方式可以细细体会一下. 文件选取器包含文件 ...

  4. ARTS 1.7 - 1.11

    每周一个 Algorithm,Review 一篇英文文章,总结一个工作中的技术 Tip,以及 Share 一个传递价值观的东西! Algorithm: 学习算法 题目: https://leetcod ...

  5. Markdown 入门

    一. Markdown语法的简要规则 标题 标题是非常重要的一个标记,一段文字标记为标题,只需要在文字前加 #.具体可以支持到1到6个# 1 2 3 4 # 一级标题 ## 二级标题 ### 三级标题 ...

  6. 赵海军获任中芯国际CEO 邱慈云留任副董事长、非执行董事(年薪40万美元+300万股票的认购权)

    集微网消息,中芯国际今日宣布,由赵海军博士接替邱慈云博士担任中芯国际首席执行官,邱慈云博士将留任副董事长.非执行董事,并于2017年6月30日前担任公司全职顾问,于当日生效.邱博士将与赵博士紧密合作, ...

  7. Visual studio 创建通用项目失败vstemplate

    Visual studio 创建项目失败 提示 the vstemplate file references the wizard class 'Microsoft.VisualStudio.WinR ...

  8. Rendering in Delphi using TCanvas (FMX)

    BY CRAIG CHAPMAN · PUBLISHED 2015-08-05 · UPDATED 2015-08-20   I have a customer with an application ...

  9. delphi Stomp客户端连接 RabbitMQ(1)

    最近公司想上个消息推送系统,网上搜了很多,因公司主要产品是Delphi,我选择了开源的RabbitMQ,Erlang语言开发,天生并行. 代码下载地址:delphistomp下载地址 windows上 ...

  10. MAC和PHY的区别(网线上传递的是模拟信号)

    一块以太网网卡包括OSI(开方系统互联)模型的两个层.物理层和数据链路层.物理层定义了数据传送与接收所需要的电与光信号.线路状态.时钟基准.数据编码和电路等,并向数据链路层设备提供标准接口.数据链路层 ...