美文网首页
laravel简易接口开发测试

laravel简易接口开发测试

作者: 积_渐 | 来源:发表于2018-11-14 14:43 被阅读48次

    1、找到Routes下的api.php 配置访问路由

    <?php
    
    use Illuminate\Http\Request;
    
    /*
    |--------------------------------------------------------------------------
    | API Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register API routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | is assigned the "api" middleware group. Enjoy building your API!
    |
    */
    
    Route::middleware('auth:api')->get('/user', function (Request $request) {
        return $request->user();
    });
    
    
    Route::group(['namespace' => 'Api'], function(){
        Route::any('home/cesi','IndexController@index');
    });
    

    2、在Controllers路径下创建Api/IndexController.php 内容如下:

    <?php
        namespace App\Http\Controllers\Api;
        use Illuminate\Http\Request;
        use App\Http\Controllers\Controller;
        use Illuminate\Support\Facades\Storage;
        use Illuminate\Support\Facades\Input;
    
        class IndexController extends Controller
        {
            public function index()
            {
                $a = empty(Input::get('a')) ? '' : Input::get('a');
                $qq = empty(Input::get('qq')) ? '' : Input::get('qq');
                //假设这是数据源,如MySQL
                $data = array();
                $data[123456] = array('qq'=>123456, 'vip'=>5,'level'=>128, 'reg_time'=>1376523234, 'qb'=>300);
                $data[654321] = array('qq'=>654321, 'vip'=>8,'level'=>101, 'reg_time'=>1377123144, 'qb'=>300);
                preg_match('/^[a-zA-Z]+$/', $a) || $this->var_json('非法调用');
                isset($data[$qq]) || $this->var_json('用户不存在', 100001);
                switch ($a) {
                    //获取用户基本信息
                    case 'info': 
                        //你的更多业务逻辑 ...
                       $this->var_json('success', 0, $data[$qq]);
                        break;
                    //获取动态消息
                    case 'message':
                       $this->var_json('您正在调用动态消息接口', 0);
                        break;
                    //获取好友列表
                    case 'friends':
                        $this->var_json('你正在调用好友列表接口', 0);
                        break;
                    default:
                        $this->var_json('非法调用');
                }
            }
            public function var_json($info = '', $code = 10000, $data = array(), $location = '') {
                $out['code'] = $code ?: 0;
                $out['info'] = $info ?: ($out['code'] ? 'error' : 'success');
                $out['data'] = $data ?: array();
                $out['location'] = $location;
                header('Content-Type: application/json; charset=utf-8');
                echo json_encode($out);
                exit(0);
            }
        }
    
    

    3、前台页面代码如下(apirequesttest.php):

    <?php 
        header('Content-type:text/html;charset=utf-8');
        $url = "http://localhost/laravel-test/laravel-test/public/api/home/cesi";
        $arg = array(
            'a'  => 'info',
            'qq' => '123456',
        );
        $query_string = http_build_query($arg);
        $ch = curl_init($url.'?'.$query_string);
        curl_setopt($ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT , 'QQ_Mobile_V5.5');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 60 );
        curl_setopt($ch, CURLOPT_TIMEOUT , 60);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        $response = curl_exec($ch);
        $httpcode = curl_getinfo($ch , CURLINFO_HTTP_CODE);
        curl_close($ch);
    
        if ($response === false) {
            var_dump(curl_error($ch));
        } else if ($httpcode != 200) {
            var_dump($httpcode, '接口请求失败');
        } else {
            $ret = json_decode($response, true);
            var_dump($ret);
        }
    

    4、浏览器访问地址如下:
    http://localhost/apirequesttest.php

    5、返回结果如下:

    array (size=4)
      'code' => int 0
      'info' => string 'success' (length=7)
      'data' => 
        array (size=5)
          'qq' => int 123456
          'vip' => int 8
          'level' => int 101
          'reg_time' => int 1377123144
          'qb' => int 300
      'location' => string '' (length=0)
    

    相关文章

      网友评论

          本文标题:laravel简易接口开发测试

          本文链接:https://www.haomeiwen.com/subject/zjhjfqtx.html