阿里雲簽名方法

<?php
date_default_timezone_set("GMT");

//綁定 ip 到域名
Ali::Obj()->UpdateDomainRecord();

class Ali
{
    private $accessKeyId  = "OJR9w5xfGaz1jVDg";
    private $accessSecrec = "xOKRdKk0EI7F0hdtnEnIAVLVxAe4LI";
    private static $obj  = null;
    public static function Obj ()
    {
        if(is_null(self::$obj))
        {
            self::$obj = new self();
        }
        return self::$obj;
    }

    public function DescribeDomainRecords()
    {
        $requestParams = array(
            "Action"    =>  "DescribeDomainRecords",
            "DomainName"    =>  "hankangkang.com"
        );
        $val =  $this->requestAli($requestParams);
        $this->outPut($val);
    }

    /**
     * 更新 ip
     */
    public function UpdateDomainRecord()
    {
        $ip = $this->ip();
        $requestParams = array(
            "Action"        =>  "UpdateDomainRecord",
            "RecordId"      =>  "71927383",
            "RR"            =>  "@",
            "Type"          =>  "A",
            "Value"         =>  $ip,
        );
        $val =  $this->requestAli($requestParams);
        $this->outPut($val."  ".$ip);
    }

    private function requestAli($requestParams)
    {
        $publicParams = array(
            "Format"        =>  "JSON",
            "Version"       =>  "2015-01-09",
            "AccessKeyId"   =>  $this->accessKeyId,
            "Timestamp"     =>  date("Y-m-d\TH:i:s\Z"),
            "SignatureMethod"   =>  "HMAC-SHA1",
            "SignatureVersion"  =>  "1.0",
            "SignatureNonce"    =>  substr(md5(rand(1,99999999)),rand(1,9),14),
        );

        $params = array_merge($publicParams, $requestParams);
        $params['Signature'] =  $this->sign($params, $this->accessSecrec);
        $uri = http_build_query($params);
        $url = 'http://alidns.aliyuncs.com/?'.$uri;
        return $this->curl($url);
    }


    private function ip()
    {
        $ip = $this->curl("http://httpbin.org/ip");
        $ip = json_decode($ip,true);
        return $ip['origin'];
    }

    private function sign($params, $accessSecrec, $method="GET")
    {
        ksort($params);
        $stringToSign = strtoupper($method).'&'.$this->percentEncode('/').'&';

        $tmp = "";
        foreach($params as $key=>$val){
            $tmp .= '&'.$this->percentEncode($key).'='.$this->percentEncode($val);
        }
        $tmp = trim($tmp, '&');
        $stringToSign = $stringToSign.$this->percentEncode($tmp);

        $key  = $accessSecrec.'&';
        $hmac = hash_hmac("sha1", $stringToSign, $key, true);

        return base64_encode($hmac);
    }


    private function percentEncode($value=null)
    {
        $en = urlencode($value);
        $en = str_replace("+", "%20", $en);
        $en = str_replace("*", "%2A", $en);
        $en = str_replace("%7E", "~", $en);
        return $en;
    }

    private function curl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        $result=curl_exec ($ch);
        return $result;
    }

    private function outPut($msg)
    {
        echo date("Y-m-d H:i:s")."  ".$msg.PHP_EOL;
    }
}

https://bbs.aliyun.com/read/542070.html?spm=5176.10695662.1996646101.searchclickresult.55d87513SSWtLl

https://bbs.aliyun.com/read/299251.html

<?php
namespace app\aliyun\controller;
use think\Controller;
use think\Cache;
use think\Cookie;
header("Content-Type: text/html;charset=utf-8");
class Signali extends Controller
{	
	private $accessKeyId  = "uuu4563554";
    private $accessSecrec = "dfgdfhdgfjhgkjjkljhlhjljh";
    private static $obj  = null;
    public static function Obj ()
    {
        if(is_null(self::$obj))
        {
            self::$obj = new self();
        }
        return self::$obj;
    }

    public function DescribeDomainRecords()
    {
        $requestParams = array(
            "Action"    =>  "DescribeDomainRecords",
            "DomainName"    =>  "hankangkang.com"
        );
        $val =  $this->requestAli($requestParams);
        $this->outPut($val);
    }

    /**
     * 更新 ip
     */
    public function GetVideo()
    {
		$timestamp=gmdate("Y-m-d\TH:i:s\Z");
		echo $timestamp;
        //$ip = $this->ip();
        $requestParams = array(
            "Action"        =>  "GetVideoPlayAuth",
			'VideoId'=>'53776bd98b734563544f6e8ed782cad38a1fe1',
           // "RecordId"      =>  "71927383",
          //  "RR"            =>  "@",
           // "Type"          =>  "A",
           // "Value"         =>  $ip,
        );
        //$val =  $this->GetVideoPlayAuth($requestParams);
		//print_r(json_decode($val,true));
			$requestParams = array(
				"Action"        =>  "GetPlayInfo",
				'VideoId'=>'53776bd98b45441554734f6e8ed782cad38a1fe1',
			);

		//$val =  $this->GetPlayInfo($requestParams);

			$requestParams = array(
				"Action"        =>  "DescribeLiveStreamsControlHistory",
				"DomainName"=>"haomaishou.com",
				'EndTime'=>"2018-05-30T07:39:24Z",//$timestamp,
				"StartTime"=>"2018-05-01T07:39:24Z",
				
			);
		$val =  $this->DescribeLiveStreamsOnlineList($requestParams);
print_r(json_decode($val,true));

    }
	//直播
	public function DescribeLiveStreamsOnlineList($requestParams){
			$timestamp=gmdate("Y-m-d\TH:i:s\Z");
				$publicParams = array(
					"Format"        =>  "JSON",
					"Version"       =>  "2016-11-01",
					"AccessKeyId"   =>  $this->accessKeyId,
					"Timestamp"     =>  gmdate("Y-m-d\TH:i:s\Z"),
					"SignatureMethod"   =>  "HMAC-SHA1",
					"SignatureVersion"  =>  "1.0",
					"SignatureNonce"    => uuid(),// substr(md5(rand(1,99999999)),rand(1,9),14),
					
				);

				$params = array_merge($publicParams, $requestParams);
			
				$params['Signature'] =  $this->sign($params, $this->accessSecrec);
				$uri = http_build_query($params);
				$url = 'http://live.aliyuncs.com/?'.$uri;
				return $this->curl($url);
	}
	//點播
	public function GetPlayInfo($requestParams)
    {
		$timestamp=gmdate("Y-m-d\TH:i:s\Z");
        $publicParams = array(
            "Format"        =>  "JSON",
            "Version"       =>  "2017-03-21",
            "AccessKeyId"   =>  $this->accessKeyId,
            "Timestamp"     =>  gmdate("Y-m-d\TH:i:s\Z"),
            "SignatureMethod"   =>  "HMAC-SHA1",
            "SignatureVersion"  =>  "1.0",
            "SignatureNonce"    => uuid(),// substr(md5(rand(1,99999999)),rand(1,9),14),
			
        );

        $params = array_merge($publicParams, $requestParams);
	
        $params['Signature'] =  $this->sign($params, $this->accessSecrec);
        $uri = http_build_query($params);
        $url = 'http://vod.cn-shanghai.aliyuncs.com/?'.$uri;
        return $this->curl($url);
    }




    public function GetVideoPlayAuth($requestParams)
    {
		$timestamp=gmdate("Y-m-d\TH:i:s\Z");
        $publicParams = array(
            "Format"        =>  "JSON",
            "Version"       =>  "2017-03-21",
            "AccessKeyId"   =>  $this->accessKeyId,
            "Timestamp"     =>  gmdate("Y-m-d\TH:i:s\Z"),
            "SignatureMethod"   =>  "HMAC-SHA1",
            "SignatureVersion"  =>  "1.0",
            "SignatureNonce"    => uuid(),// substr(md5(rand(1,99999999)),rand(1,9),14),
			
        );

        $params = array_merge($publicParams, $requestParams);
	
        $params['Signature'] =  $this->sign($params, $this->accessSecrec);
        $uri = http_build_query($params);
        $url = 'http://vod.cn-shanghai.aliyuncs.com/?'.$uri;
        return $this->curl($url);
    }


    public function ip()
    {
        $ip = $this->curl("http://httpbin.org/ip");
        $ip = json_decode($ip,true);
        return $ip['origin'];
    }

    public function sign($params, $accessSecrec, $method="GET")
    {
        ksort($params);
        $stringToSign = strtoupper($method).'&'.$this->percentEncode('/').'&';

        $tmp = "";
        foreach($params as $key=>$val){
            $tmp .= '&'.$this->percentEncode($key).'='.$this->percentEncode($val);
        }
        $tmp = trim($tmp, '&');
        $stringToSign = $stringToSign.$this->percentEncode($tmp);

        $key  = $accessSecrec.'&';
        $hmac = hash_hmac("sha1", $stringToSign, $key, true);

        return base64_encode($hmac);
    }


    public function percentEncode($value=null)
    {
        $en = urlencode($value);
        $en = str_replace("+", "%20", $en);
        $en = str_replace("*", "%2A", $en);
        $en = str_replace("%7E", "~", $en);
        return $en;
    }

    public function curl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        $result=curl_exec ($ch);
        return $result;
    }

    public function outPut($msg)
    {
        echo date("Y-m-d H:i:s")."  ".$msg.PHP_EOL;
    }

public function uuid($prefix = '')  
  {  
    $chars = md5(uniqid(mt_rand(), true));  
    $uuid  = substr($chars,0,8) . '-';  
    $uuid .= substr($chars,8,4) . '-';  
    $uuid .= substr($chars,12,4) . '-';  
    $uuid .= substr($chars,16,4) . '-';  
    $uuid .= substr($chars,20,12);  
    return $prefix . $uuid;  
  }



	public function indexs(){
			$url=compose_url();
echo _juhecurl($url);

	}


	



}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章