php curl操作API接口类完整示例

yipeiwu_com6年前PHP代码库

本文实例讲述了php curl操作API接口类。分享给大家供大家参考,具体如下:

<?php
namespace curl;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/16
 * Time: 9:54
 */
class ApiClient
{
//请求的token
 const token='token_str';
 //请求url
 private $url;
 //请求的类型
 private $requestType;
 //请求的数据
 private $data;
 //curl实例
 private $curl;
 public $status;
 private $headers = array();
 /**
  * [__construct 构造方法, 初始化数据]
  * @param [type] $url  请求的服务器地址
  * @param [type] $requestType 发送请求的方法
  * @param [type] $data 发送的数据
  * @param integer $url_model 路由请求方式
  */
 public function __construct($url, $data = array(), $requestType = 'get') {
  //url是必须要传的,并且是符合PATHINFO模式的路径
  if (!$url) {
   return false;
  }
  $this->requestType = strtolower($requestType);
  $paramUrl = '';
  // PATHINFO模式
  if (!empty($data)) {
   foreach ($data as $key => $value) {
    $paramUrl.= $key . '=' . $value.'&';
   }
   $url = $url .'?'. $paramUrl;
  }
  //初始化类中的数据
  $this->url = $url;
  $this->data = $data;
  try{
   if(!$this->curl = curl_init()){
    throw new Exception('curl初始化错误:');
   };
  }catch (Exception $e){
   echo '<pre>';
   print_r($e->getMessage());
   echo '</pre>';
  }
  curl_setopt($this->curl, CURLOPT_URL, $this->url);
  curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
  //curl_setopt($this->curl, CURLOPT_HEADER, 1);
 }
 /**
  * [_post 设置get请求的参数]
  * @return [type] [description]
  */
 public function _get() {
 }
 /**
  * [_post 设置post请求的参数]
  * post 新增资源
  * @return [type] [description]
  */
 public function _post() {
  curl_setopt($this->curl, CURLOPT_POST, 1);
  curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->data);
 }
 /**
  * [_put 设置put请求]
  * put 更新资源
  * @return [type] [description]
  */
 public function _put() {
  curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
 }
 /**
  * [_delete 删除资源]
  * delete 删除资源
  * @return [type] [description]
  */
 public function _delete() {
  curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
 }
 /**
  * [doRequest 执行发送请求]
  * @return [type] [description]
  */
 public function doRequest() {
  //发送给服务端验证信息
  if((null !== self::token) && self::token){
   $this->headers = array(
    'Client-Token:'.self::token,//此处不能用下划线
    'Client-Code:'.$this->setAuthorization()
   );
  }
  //发送头部信息
  $this->setHeader();
  //发送请求方式
  switch ($this->requestType) {
   case 'post':
    $this->_post();
    break;
   case 'put':
    $this->_put();
    break;
   case 'delete':
    $this->_delete();
    break;
   default:
    curl_setopt($this->curl, CURLOPT_HTTPGET, TRUE);
    break;
  }
  //执行curl请求
  $info = curl_exec($this->curl);
  //获取curl执行状态信息
  $this->status = $this->getInfo();
  return json_decode($info);
 }
 /**
  * 设置发送的头部信息
  */
 private function setHeader(){
  curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->headers);
 }
 /**
  * 生成授权码
  * @return string 授权码
  */
 private function setAuthorization(){
  $authorization = md5(substr(md5(self::token), 8, 24).self::token);
  return $authorization;
 }
 /**
  * 获取curl中的状态信息
  */
 public function getInfo(){
  return curl_getinfo($this->curl);
 }
 /**
  * 关闭curl连接
  */
 public function __destruct(){
  curl_close($this->curl);
 }
}

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php curl用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》及《PHP中json格式数据操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

浅谈ThinkPHP5.0版本和ThinkPHP3.2版本的区别

5.0版本和之前版本的差异较大,本篇对熟悉3.2版本的用户给出了一些5.0的主要区别。 URL和路由 5.0的URL访问不再支持普通URL模式,路由也不支持正则路由定义,而是全部改为规则...

php中序列化与反序列化详解

把复杂的数据类型压缩到一个字符串中 serialize() 把变量和它们的值编码成文本形式 unserialize() 恢复原先变量 eg: $stooges = array('Mo...

关于Intype一些小问题的解决办法

 A:选择中文字体,如果你安装了雅黑字体,但是却无法调用。你可以打开安装目录下\Intype\user\preferences.itConfig, font_name...

php正则表达式(regar expression)

php正则表达式(regar expression)

引言: 在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串 的需要。正则表达式就是用于描述这些规则的语法。 例:在判断用户邮件地址格式、手机号码格式或者采集别人网页内容...

PHP使用memcache缓存技术提高响应速度的方法

本文实例讲述了PHP使用memcache缓存技术提高响应速度的方法。分享给大家供大家参考。具体分析如下: php虽然己经做到很好很快了,但是如果大数据量时还是会有些卡了,这里介绍一下PH...