php基于curl扩展制作跨平台的restfule 接口

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php基于curl扩展制作跨平台的restfule 接口脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

restfule 接口 适用的平台:跨平台 所依赖:curl扩展 gIT

@H_403_4@ApiServer.PHP

<PRe class="brush:PHP;">

class apiServer
{
/**

  • 客户端请求的方式
  • @var string
    */
    private $method = '';

/**

  • 客户端发送的数据
  • @var [type]
    */
    protected $param;

/**

  • 要操作的资源
  • @var [type]
    */
    protected $resourse;

/**

  • 要操作的资源id
  • @var [type]
    */
    protected $resourseId;

/**

  • 构造函数, 获取client 请求的方式,以及传输的数据
  • @param object 可以自定义传入的对象
    */
    public function __construct()
    {
    //首先对客户端的请求进行验证
    $this->authorization();
$this->method = strtolower($_SERVER['REQUEST_METHOD']);

//所有的请求都是pathinfo模式
$pathinfo = $_SERVER['PATH_INFO'];

//将pathinfo数据信息映射为实际请求<a href="https://www.js-code.com/tag/fangfa/" target="_blank" class="keywords">方法</a>
$this->getResourse($pathinfo);

//<a href="https://www.js-code.com/tag/huoqu/" target="_blank" class="keywords">获取</a>传输的具体参数
$this->getData();

//执行响应
$this->doResponse();

}

/**

  • 根据不同的请求方式,获取数据
  • @return [tyPE]
    */
    private function doResponse(){
    switch ($this->;method) {
    case 'get':
    $this->_get();
    break;
    case 'post':
    $this->_post();
    break;
    case 'delete':
    $this->_delete();
    break;
    case 'put':
    $this->_put();
    break;
    default:
    $this->_get();
    break;
    }
    }

// 将pathinfo数据信息映射为实际请求方法
private function getResourse($pathinfo){

/**
 * 将pathinfo数据信息映射为实际请求<a href="https://www.js-code.COM/tag/fangfa/" target="_blank" class="keywords">方法</a>
 * GET /users: 逐页列出所有<a href="https://www.js-code.com/tag/yonghu/" target="_blank" class="keywords">用户</a>;
 * POST /users: 创建<a href="https://www.js-code.com/tag/yige/" target="_blank" class="keywords">一个</a><a href="https://www.js-code.com/tag/xinyonghu/" target="_blank" class="keywords">新用户</a>;
 * GET /users/123: 返回<a href="https://www.js-code.com/tag/yonghu/" target="_blank" class="keywords">用户</a>为123的详细信息;
 * PUT /users/123: 更<a href="https://www.js-code.com/tag/xinyonghu/" target="_blank" class="keywords">新用户</a>123;
 * DELETE /users/123: <a href="https://www.js-code.com/tag/shanchu/" target="_blank" class="keywords">删除</a><a href="https://www.js-code.com/tag/yonghu/" target="_blank" class="keywords">用户</a>123;
 *
 * 根据以上规则,将pathinfo第<a href="https://www.js-code.com/tag/yige/" target="_blank" class="keywords">一个</a>参数映射为需要操作的数据表,
 * 第二个参数映射为操作的id
 */

$info = explode('/',ltrim($pathinfo,'/'));
list($this->resourse,$this->resourseid) = $info;

}

/**

  • 验证请求
    */
    private function authorization(){
    $token = $_SERVER['HTTP_CLIENT_TOKEN'];
    $authorization = md5(substr(md5($token),8,24).$token);
    if($authorization != $_SERVER['HTTP_CLIENT_CODE']){
    //验证失败,输出错误信息给客户端
    $this->outPut($status = 1);
    }
    }

/**

  • [getData 获取传送的参数信息]
  • @param [type] $pad [description]
  • @return [type] [description]
    */
    private function getData(){
    //所有的参数都是get传参
    $this->param = $_GET;
    }

/**

  • 获取操作
  • @return [type] [description]
    */
    protected function _get(){
    //逻辑代码根据自己实际项目需要实现
    }

/**

  • 新增资源操作
  • @return [type] [description]
    */
    protected function _post(){
    //逻辑代码根据自己实际项目需要实现
    }

/**

  • 删除资源操作
  • @return [type] [description]
    */
    protected function _delete(){
    //逻辑代码根据自己实际项目需要实现
    }

/**

  • 更新资源操作
  • @return [type] [description]
    */
    protected function _put(){
    //逻辑代码根据自己实际项目需要实现
    }

/**

  • 出入服务端返回的数据信息 json格式
    */
    public function outPut($stat,$data=array()){
    $status = array(
    //0 状态表示请求成功
    0 => array(
    'code' => 1,'info' => '请求成功','data' =>$data
    ),//验证失败
    1 => array(
    'code' => 0,'info' => '请求不合法'
    )
    );
try{
  if(!in_array($stat,array_keys($status))){
    throw new Exception('输入的状态码不合法');
  }else{
    echo json_encode($status[$stat]);
  }
}catch (Exception $e){
  die($e->getMessage());
}

}
}

脚本宝典总结

以上是脚本宝典为你收集整理的php基于curl扩展制作跨平台的restfule 接口全部内容,希望文章能够帮你解决php基于curl扩展制作跨平台的restfule 接口所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。