ThinkPHP5开发API接口实例

接口功能说明: 前端提交学生学号(sno)给Api,Api接口返回此学生的基本信息

API接口端

fetch();
    }
    // 客户端提交学生学号(sno)给api,api返回此学生的基本信息
    public function api($sno='0001') {
        // 查询 并把数据赋值给 $data
        $data = Student::getBysno($sno);
        // 返回数据
        return json($data);
    }

}

(请求端) HTML




    
    TP5通过API查询数据


    

(请求端) C层控制器

fetch();
    }
    public function capi() {
        // http协议请求
        $url = 'http://localhost/index.php/index/index/api/';
        // input('sno') 是前端的from传过来的name值
        $ch = curl_init($url.'?sno='.input('sno'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 执行 并把执行后的数据赋值给 $data
        $data = curl_exec($ch);
        // 关闭
        curl_close($ch);
        // 返回数据
        return $data;
    }

}

你可能感兴趣的:(PHP)