您的位置:首页 > 编程语言 > PHP开发

阿里云短信服务SDK踩坑(php)

2017-07-25 14:27 645 查看
我用的php,下载demo和sdk导入项目后,写了一个driver,来驱动这个sdk

部分代码如下

require_once 'api_sdk/vendor/autoload.php';

use Aliyun\Api\Sms\Request\V20170525\QuerySendDetailsRequest;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\Regions\EndpointConfig;

//include './api_sdk/vendor/autoload.php';

class MessageDriver{

function __construct($app_key,$secret_key)
{
// 短信API产品名
$product = "Dysmsapi";

// 短信API产品域名
$domain = "dysmsapi.aliyuncs.com";

// 暂时不支持多Region
$region = "cn-hangzhou";

// 服务结点
$endPointName = "cn-hangzhou";

// 初始化用户Profile实例
$profile = DefaultProfile::getProfile($region, $app_key, $secret_key);

// 手动加载endpoint
EndpointConfig::load();
// 增加服务结点
DefaultProfile::addEndpoint($endPointName, $region, $product, $domain);

// 初始化AcsClient用于发起请求
$this->acsClient = new DefaultAcsClient($profile);
}


执行后总是报错

Can not find endpoint to access.

慢慢debug发现是在 EndpointerProvider里面,没有setEndpoints就直接在demo里面使用了getEndpointers,所以返回为空信息

找一下源代码,发现setEndpoints操作是在EndpointConf中调用的,加载xml中的endpoints

上面贴的代码中已经加入了该行代码

EndpointConfig::load();


然后另一个坑出现了

Use of undefined constant ENABLE_HTTP_PROXY - assumed 'ENABLE_HTTP_PROXY'

讲道理,阿里云做这个么久了,就不能做个好用点的SDK么,真是操碎了心

看看日志,是代理的问题,在HttpHelper中注释掉下面的代码即可

if(ENABLE_HTTP_PROXY) {
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_PROXY, HTTP_PROXY_IP);
curl_setopt($ch, CURLOPT_PROXYPORT, HTTP_PROXY_PORT);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  短信 php 阿里云 api aliyun