PHP code example of maxsky / aliyun-api-gateway-sdk

1. Go to this page and download the library: Download maxsky/aliyun-api-gateway-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

maxsky / aliyun-api-gateway-sdk example snippets


use Aliyun\ApiGateway\Http;

try {
    $result = HttpClient::setKey('App Key', 'App Secret')
        ->execute('POST', 'https://demo.market.alicloudapi.com/gateway_api', [
            'headers' => [
                // 额外请求头键值对,“X-Ca-”开头的请求头会自动加入签名计算
                'x-ca-header1' => 'value1',
                'x-ca-header2' => 'value2'
            ],
            'query' => [
                // query 参数无需拼接在 URL 上,写在此处即可
                'param1' => 'value1',
                'param2' => 'value2'
            ],
            'body' => [
                // 三选一,自动设置 Content-Type
                'form' => [
                    // application/x-www-form-urlencoded
                    'formParam1' => 'value 1',
                    'formParam2' => 'value 2'
                ],
                // application/json
                'json' => [
                    'name' => 'Max Sky',
                    'gender' => 'male'
                ],
                // application/text
                'text' => 'contents'
            ]
        ]);
} catch (GuzzleException|ClientException|BadResponseException $e) {
    if ($e instanceof ClientException) {
        // 请求失败时显示来自阿里云的错误消息
        var_dump($e->getResponse()->getHeader('X-Ca-Error-Message'));
        die;
    }

    if ($e instanceof BadResponseException) {
        // 请求成功但参数错误之类的响应可通过捕获该异常得到
        var_dump(json_decode($e->getResponse()->getBody(), true));
        die;
    }

    var_dump($e->getMessage());
    die;
}

// 请求成功获取响应结果
var_dump(json_decode($result, true));