php 操作kong&oauth2.0插件

来源:互联网 发布:c语言两个指针相减 编辑:程序博客网 时间:2024/06/05 02:11
<?php/** * Created by PhpStorm. * User: he * Date: 17-10-31 * Time: 上午10:36 *//* * ===================================================================================================================== */// 添加API//print_r(add_api());/*    {        "created_at":1509416106000,        "strip_uri":true,        "id":"55b0dfb4-813e-4262-9498-ebd555266a80",        "hosts":[            "test.my"        ],        "name":"test-api",        "http_if_terminated":false,        "preserve_host":false,        "upstream_url":"http://test.my",        "upstream_connect_timeout":60000,        "upstream_send_timeout":60000,        "upstream_read_timeout":60000,        "retries":5,        "https_only":false    } */// 关联oauth插件//print_r(relate_oauth());/*    {        "created_at":1509416153000,        "config":{            "token_expiration":7200,            "mandatory_scope":true,            "hide_credentials":false,            "enable_authorization_code":true,            "enable_implicit_grant":false,            "global_credentials":false,            "scopes":[                "email",                "sms"            ],            "enable_password_grant":false,            "accept_http_if_already_terminated":false,            "anonymous":"",            "enable_client_credentials":false,            "provision_key":"64UKJL1Ypk29Gtxrgg1laX9biHibb9Mq"        },        "id":"1453ce8e-f303-45fa-af27-bb68806492e4",        "name":"oauth2",        "api_id":"55b0dfb4-813e-4262-9498-ebd555266a80",        "enabled":true    } */// 添加消费者//print_r(add_consumer());/*    {        "created_at":1509416185000,        "username":"test",        "id":"09e2fb41-eed5-4b72-bff8-57c6d4cdf0c9"    } */// 创建应用//print_r(create_app());/*    {        "client_id":"QJD043ipB43NdWsqrbkLiV4WWgkLaAt7",        "created_at":1509416415000,        "id":"d5c355e1-09a8-414a-a479-74243d1d5a77",        "redirect_uri":[            "http://test.my/"        ],        "name":"test",        "client_secret":"GAxHK4g1JxM6IsGPIbsQqxjCPyVbwP3U",        "consumer_id":"09e2fb41-eed5-4b72-bff8-57c6d4cdf0c9"    } */// 模拟授权// 获取code//print_r(//    code('QJD043ipB43NdWsqrbkLiV4WWgkLaAt7', '64UKJL1Ypk29Gtxrgg1laX9biHibb9Mq',//    'email', 'test.my'//    )//);/*    {        "redirect_uri":"http://test.my/?code=3H95cXeHorfdgnaonDgolttnLaUL9R52"    } */// 获取token//print_r(//    token(//    'QJD043ipB43NdWsqrbkLiV4WWgkLaAt7', 'GAxHK4g1JxM6IsGPIbsQqxjCPyVbwP3U',//    '64UKJL1Ypk29Gtxrgg1laX9biHibb9Mq', '3H95cXeHorfdgnaonDgolttnLaUL9R52', 'test.my'//    )//);/*    {        "refresh_token":"cG5zAnffPSaj9DvsnOFy7n3fNTMSe6ZA",        "token_type":"bearer",        "access_token":"ZXddR46nj4M4ByO4wtLbxOb0B3sEbS6z",        "expires_in":7200    } */// 验证token//print_r(//    get_api(//    'ZXddR46nj4M4ByO4wtLbxOb0B3sEbS6z', 'test.my'//    )//);// 刷新token//print_r(//    refresh_token(//        'cG5zAnffPSaj9DvsnOFy7n3fNTMSe6ZA', 'QJD043ipB43NdWsqrbkLiV4WWgkLaAt7',//        'GAxHK4g1JxM6IsGPIbsQqxjCPyVbwP3U', 'test.my'//    )//);/*    {        "refresh_token":"25M8vwezfE0HbgEFPBv90WA64HHBXtC8",        "token_type":"bearer",        "access_token":"KRLlDFPM76spsNOchuxxpq97lkURwu34",        "expires_in":7200    } *//* * ===================================================================================================================== */function add_api(){    // 添加API    $api_data = ['name' => 'test-api', 'hosts' => 'test.my', 'upstream_url' => 'http://test.my'];    $curl = curl_init('http://localhost:8001/apis/');    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");    curl_setopt($curl, CURLOPT_POSTFIELDS, $api_data);    $output = curl_exec($curl);    curl_close($curl);    return $output;}function relate_oauth(){    // 关联oauth    $api_data = [        'name' => 'oauth2',        'config.enable_authorization_code' => 'true',        'config.scopes' => 'email,sms',        'config.mandatory_scope' => 'true'    ];    $curl = curl_init('http://localhost:8001/apis/test-api/plugins');    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");    curl_setopt($curl, CURLOPT_POSTFIELDS, $api_data);    $output = curl_exec($curl);    curl_close($curl);    return $output;}function add_consumer(){    // 添加消费者    $api_data = ['username' => 'test'];    $curl = curl_init('http://localhost:8001/consumers/');    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");    curl_setopt($curl, CURLOPT_POSTFIELDS, $api_data);    $output = curl_exec($curl);    curl_close($curl);    return $output;}function create_app(){    // 创建应用    $api_data = ['name' => 'test', 'redirect_uri' => 'http://test.my/'];    $curl = curl_init('http://localhost:8001/consumers/test/oauth2');    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");    curl_setopt($curl, CURLOPT_POSTFIELDS, $api_data);    $output = curl_exec($curl);    curl_close($curl);    return $output;}function code($client_id, $provision_key, $scope, $host){    // 获取code    $post_data = [        'client_id' => $client_id,        'response_type' => 'code',        'provision_key' => $provision_key,        'authenticated_userid' => 0,        'scope' => $scope    ];    $curl = curl_init('https://localhost:8443/oauth2/authorize');    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);    curl_setopt($curl,CURLOPT_HTTPHEADER,[sprintf("Host:%s",$host)]);    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);    curl_setopt($curl,CURLOPT_POST,true);    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);    $output = curl_exec($curl);    curl_close($curl);    return $output;}function token($client_id, $client_secret, $provision_key, $code, $host){    // 获取Token    $post_data = [        'client_id' => $client_id,        'client_secret' => $client_secret,        'provision_key' => $provision_key,        'code' => $code,        'grant_type' => 'authorization_code'    ];    $curl = curl_init('https://localhost:8443/oauth2/token');    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);    curl_setopt($curl,CURLOPT_HTTPHEADER,[sprintf("Host:%s",$host)]);    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);    curl_setopt($curl,CURLOPT_POST,true);    curl_setopt($curl,CURLOPT_POSTFIELDS, $post_data);    $output = curl_exec($curl);    curl_close($curl);    return $output;}function get_api($token, $host){    // 获取API    $post_data = [        'access_token' => $token,    ];    $curl = curl_init('https://localhost:8443');    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);    curl_setopt($curl,CURLOPT_HTTPHEADER,[sprintf("Host:%s",$host)]);    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);    curl_setopt($curl,CURLOPT_POST,true);    curl_setopt($curl,CURLOPT_POSTFIELDS, $post_data);    $output = curl_exec($curl);    curl_close($curl);    return $output;}function refresh_token($refresh_token, $client_id, $client_secret, $host){    //刷新Token    $post_data = [        'refresh_token' => $refresh_token,        'client_id' => $client_id,        'client_secret' => $client_secret,        'grant_type' => 'refresh_token'    ];    $curl = curl_init('https://localhost:8443/oauth2/token');    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);    curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);    curl_setopt($curl,CURLOPT_HTTPHEADER,[sprintf("Host:%s",$host)]);    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);    curl_setopt($curl,CURLOPT_POST,true);    curl_setopt($curl,CURLOPT_POSTFIELDS, $post_data);    $output = curl_exec($curl);    curl_close($curl);    return $output;}

文章来源:http://blog.csdn.net/qq_26656329/article/details/78400704

原创粉丝点击