<?php
define('ENABLE_HTTP_PROXY', FALSE);
define('HTTP_PROXY_IP', '127.0.0.1');
define('HTTP_PROXY_PORT', '8888');
/**
* 阿里云信通接口
* 2018年3月9日
* @author saso <415944661@qq.com>
*/
class dysms{
public static $connectTimeout = 30;//30 second
public static $readTimeout = 80;//80 second
public $requestUrl = 'http://dysmsapi.aliyuncs.com/';
public $method = 'POST';
public $accessKeySecret = '';
public $AccessKeyId = '';
public $queryParameters = array();
function __construct()
{
$this->queryParameters["Format"] = 'JSON';
$this->queryParameters["SignatureVersion"] = '1.0';
$this->queryParameters["RegionId"] = 'cn-hangzhou';
$this->queryParameters["SignatureMethod"] = 'HMAC-SHA1';
$this->queryParameters["Version"] = '2017-05-25';
}
public function setAccessKeyId($AccessKeyId) {
$this->AccessKeyId = $AccessKeyId;
}
public function setaccessKeySecret($accessKeySecret) {
$this->accessKeySecret = $accessKeySecret;
}
private function computeSignature($parameters, $accessKeySecret)
{
ksort($parameters);
$canonicalizedQueryString = '';
foreach($parameters as $key => $value)
{
$canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);
}
$stringToSign = $this->method.'&%2F&' . $this->percentEncode(substr($canonicalizedQueryString, 1));
$signature = $this->signString($stringToSign, $this->accessKeySecret."&");
return $signature;
}
public function setPhoneNumber($phoneNumber) {
$this->queryParameters["PhoneNumber"]=$phoneNumber;
}
public function setPhoneNumbers($templateCode) {
$this->queryParameters["PhoneNumbers"] = $templateCode;
}
public function setSignName($signName) {
$this->queryParameters["SignName"] = $signName;
}
public function setTemplateCode($templateCode) {
$this->queryParameters["TemplateCode"] = $templateCode;
}
public function setTemplateParam($templateParam) {
$this->queryParameters["TemplateParam"] = $templateParam;
}
public function setSendDate($sendDate) {
$this->queryParameters["SendDate"]=$sendDate;
}
public function setPageSize($pageSize) {
$this->queryParameters["PageSize"]=$pageSize;
}
public function setCurrentPage($currentPage) {
$this->queryParameters["CurrentPage"]=$currentPage;
}
public function setActionName($actionName)
{
$this->queryParameters['Action'] = $actionName;
}
public function getDomainParameter(){
$this->queryParameters["AccessKeyId"] = $this->AccessKeyId;
$this->queryParameters["SignatureNonce"] = uniqid(mt_rand(0,0xffff), true);
$this->queryParameters["Timestamp"] = gmdate('Y-m-d\TH:i:s\Z');
$this->queryParameters["Signature"] = $this->computeSignature($this->queryParameters, $this->accessKeySecret);
return $this->queryParameters;
}
public function getHeaders(){
return array("x-sdk-client"=>"php/2.0.0");
}
public function setPhoneNumberJson($phoneNumberJson) {
$this->queryParameters["PhoneNumberJson"]=$phoneNumberJson;
}
public function setSignNameJson($SignNameJson) {
$this->queryParameters["SignNameJson"]=$SignNameJson;
}
public function setTemplateParamJson($templateParamJson) {
$this->queryParameters["TemplateParamJson"]=$templateParamJson;
}
public function signString($source, $accessSecret)
{
return base64_encode(hash_hmac('sha1', $source, $accessSecret, true));
}
protected function percentEncode($str)
{
$res = urlencode($str);
$res = preg_replace('/\+/', '%20', $res);
$res = preg_replace('/\*/', '%2A', $res);
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
public function curl($url, $httpMethod = "GET", $postFields = null, $headers = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
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);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($postFields) ? $this->getPostHttpBody($postFields) : $postFields);
if (self::$readTimeout) {
curl_setopt($ch, CURLOPT_TIMEOUT, self::$readTimeout);
}
if (self::$connectTimeout) {
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
}
//https request
if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
if (is_array($headers) && 0 < count($headers))
{
$httpHeaders = $this->getHttpHearders($headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
}
$httpResponse = new HttpResponse();
$httpResponse->setBody(curl_exec($ch));
$httpResponse->setStatus(curl_getinfo($ch, CURLINFO_HTTP_CODE));
if (curl_errno($ch))
{
throw new ClientException("Server unreachable: Errno: " . curl_errno($ch) . " " . curl_error($ch), "SDK.ServerUnreachable");
}
curl_close($ch);
return $httpResponse;
}
static function getPostHttpBody($postFildes){
$content = "";
foreach ($postFildes as $apiParamKey => $apiParamValue)
{
$content .= "$apiParamKey=" . urlencode($apiParamValue) . "&";
}
return substr($content, 0, -1);
}
static function getHttpHearders($headers)
{
$httpHeader = array();
foreach ($headers as $key => $value)
{
array_push($httpHeader, $key.":".$value);
}
return $httpHeader;
}
private function parseAcsResponse($body, $format)
{
if ("JSON" == $format)
{
$respObject = json_decode($body);
}
else if("XML" == $format)
{
$respObject = @simplexml_load_string($body);
}
else if("RAW" == $format)
{
$respObject = $body;
}
return $respObject;
}
}
class HttpResponse
{
private $body;
private $status;
public function getBody()
{
return $this->body;
}
public function setBody($body)
{
$this->body = $body;
}
public function getStatus()
{
return $this->status;
}
public function setStatus($status)
{
$this->status = $status;
}
public function isSuccess()
{
if(200 <= $this->status && 300 > $this->status)
{
return true;
}
return false;
}
}
阿里云信通-短信接口-php版
需积分: 9 9 浏览量
2018-03-09
14:07:41
上传
评论
收藏 4KB ZIP 举报
月月鸟三寿
- 粉丝: 7
- 资源: 12