阿里雲簡訊服務SDK踩坑(php)
阿新 • • 發佈:2019-01-10
我用的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); }