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

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

ApiServer.PHP

450_2@数据
  • @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="http://code.js-code.com/tag/fangfa/" target="_blank" class="keywords">方法</a>
     * GET /users: 逐页列出所有<a href="http://code.js-code.com/tag/yonghu/" target="_blank" class="keywords">用户</a>;
     * POST /users: 创建<a href="http://code.js-code.com/tag/yige/" target="_blank" class="keywords">一个</a><a href="http://code.js-code.com/tag/xinyonghu/" target="_blank" class="keywords">新用户</a>;
     * GET /users/123: 返回<a href="http://code.js-code.com/tag/yonghu/" target="_blank" class="keywords">用户</a>为123的详细信息;
     * PUT /users/123: 更<a href="http://code.js-code.com/tag/xinyonghu/" target="_blank" class="keywords">新用户</a>123;
     * deletE /users/123: <a href="http://code.js-code.com/tag/shanchu/" target="_blank" class="keywords">删除</a><a href="http://code.js-code.com/tag/yonghu/" target="_blank" class="keywords">用户</a>123;
     *
     * 根据以上规则,将pathinfo第<a href="http://code.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,请注明来意。