deng-php/alisms

dev-master 2020-02-13 12:48 UTC

This package is auto-updated.

Last update: 2024-05-13 21:47:11 UTC


README

php-composer-alisms English | 简体中文

68747470733a2f2f616c6979756e73646b2d70616765732e616c6963646e2e636f6d2f69636f6e732f416c6962616261436c6f75642e737667

Alibaba Cloud Client for PHP

Latest Stable Version Latest Unstable Version composer.lock Total Downloads License
codecov Scrutinizer Code Quality Travis Build Status Appveyor Build Status Code Intelligence Status

Alibaba Cloud Client for PHP is a client tool that helps PHP developers manage credentials and send requests, Alibaba Cloud SDK for PHP dependency on this tool.

一:【ThinkPhp5.1版本】

1. application/common.php文件

/**
 * 发送验证码公共方法
 * @param $mobile
 * @param $code
 * @return array
 * @throws ClientException
 * @author deng    (2019/8/17 10:55)
 */
function sendSms($mobile, $code)
{
    $condition = [
        'accessKeyId' => config('sms.accessKeyId'),
        'accessSecret' => config('sms.accessSecret'),
        'code' => $code,
        'mobile' => $mobile,
        'signName' => config('sms.signName'),
        'templateCode' => config('sms.templateCode'),
    ];
    $sendSms = AliSms::sendSms($condition);

    return $sendSms;
}

2. config/sms.php

// +----------------------------------------------------------------------
// | 短信设置
// +----------------------------------------------------------------------
return [
    'accessKeyId' => 'LTAIepxxx5VaFvt',
    'accessSecret' => 'WHqEyofsxxxxxxxMHT26hmqDRrqQQEn',
    'signName' => '模板名称',
    'templateCode' => '模板号码'
];

68747470733a2f2f7265732e636c6f7564696e6172792e636f6d2f6474666276766b79702f696d6167652f75706c6f61642f76313536363333313337372f6c61726176656c2d6c6f676f6c6f636b75702d636d796b2d7265642e737667

二:【Laravel6】

路径: config/sms.php

// +----------------------------------------------------------------------
// | 短信设置
//  Config::get('sms.accessKeyId')
// +----------------------------------------------------------------------
return [
    'accessKeyId' => 'LdTAIepXM665VaFvt',
    'accessSecret' => 'WdHqEyofsgxTmFCMHT26hmqDRrqQQEn',
    'signName' => '柠檬xx',
    'templateCode' => [
        'other' => 'SMS_1637252967',
        'login' => 'SMS_1637252967',
        'register' => 'SMS_1637252967',
    ]
];

namespace App\Common\Services;

    /**
     * 发送阿里云短信
     *
     * @param $mobile
     * @param $code
     * @param $type
     * @return array
     * @throws ClientException
     * @author:  deng    (2020/2/13 20:08)
     */
    public function smsSend($mobile, $code, $type = '')
    {
        switch ($type) {
            case 'LOGIN':
                $templateCode = Config::get('sms.templateCode')['login'];
                break;
            case 'REGISTER':
                $templateCode = Config::get('sms.templateCode')['register'];
                break;
            default:
                $templateCode = Config::get('sms.templateCode')['other'];
                break;
        }
        $condition = [
            'accessKeyId' => Config::get('sms.accessKeyId'),
            'accessSecret' => Config::get('sms.accessSecret'),
            'signName' => Config::get('sms.signName'),
            'templateCode' => $templateCode,
            'mobile' => $mobile,
            'code' => $code,
        ];

        $sendSms = AliSms::sendSms($condition);

        return $sendSms;
    }


-----------------------------------------

-----------------------------------------

Online Demo

API Explorer provides the ability to call the cloud product OpenAPI online, and dynamically generate SDK Example code and quick retrieval interface, which can significantly reduce the difficulty of using the cloud API.

Prerequisites

Your system will need to meet the Prerequisites, including having PHP >= 5.5. We highly recommend having it compiled with the cURL extension and cURL 7.16.2+.

Installation

If Composer is already installed globally on your system, run the following in the base directory of your project to install Alibaba Cloud Client for PHP as a dependency:

composer require alibabacloud/client

Some users may not be able to install due to network problems, you can try to switch the Composer mirror.

Please see the Installation for more detailed information about installing the Alibaba Cloud Client for PHP through Composer and other means.

Quick Examples

Before you begin, you need to sign up for an Alibaba Cloud account and retrieve your Credentials.

Create Client

<?php

use AlibabaCloud\Client\AlibabaCloud;

AlibabaCloud::accessKeyClient('accessKeyId', 'accessKeySecret')->asDefaultClient();

ROA Request

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

try {
    $result = AlibabaCloud::roa()
                          ->regionId('cn-hangzhou') // Specify the requested regionId, if not specified, use the client regionId, then default regionId
                          ->product('CS') // Specify product
                          ->version('2015-12-15') // Specify product version
                          ->action('DescribeClusterServices') // Specify product interface
                          ->serviceCode('cs') // Set ServiceCode for addressing, optional
                          ->endpointType('openAPI') // Set type, optional
                          ->method('GET') // Set request method
                          ->host('cs.aliyun.com') // Location Service will not be enabled if the host is specified. For example, service with a Certification type-Bearer Token should be specified
                          ->pathPattern('/clusters/[ClusterId]/services') // Specify path rule with ROA-style
                          ->withClusterId('123456') // Assign values to parameters in the path. Method: with + Parameter
                          ->request(); // Make a request and return to result object. The request is to be placed at the end of the setting
                          
    print_r($result->toArray());
    
} catch (ClientException $exception) {
    print_r($exception->getErrorMessage());
} catch (ServerException $exception) {
    print_r($exception->getErrorMessage());
}

RPC Request

<?php

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

try {
    $result = AlibabaCloud::rpc()
                          ->product('Cdn')
                          ->version('2014-11-11')
                          ->action('DescribeCdnService')
                          ->method('POST')
                          ->request();
    
    print_r($result->toArray());
    
} catch (ClientException $exception) {
    print_r($exception->getErrorMessage());
} catch (ServerException $exception) {
    print_r($exception->getErrorMessage());
}

Documentation

Issues

Opening an Issue, Issues not conforming to the guidelines may be closed immediately.

Changelog

Detailed changes for each release are documented in the release notes.

Contribution

Please make sure to read the Contributing Guide before making a pull request.

References

License

Apache-2.0

Copyright 1999-2019 Alibaba Group Holding Ltd.