又拍云云处理php类

又拍云提供了云处理的文档,其中包括视频音频剪辑、远程下载、在线压缩解压等,在国内云服务算是比较领先的了。

工作需要,恰好按照文档整理出一个php类,只有在线压缩解压和异步文件拉取(远程下载)。其它音频剪辑等没需求,就没去做。

代码还有一些优化空间,比如tasks和提交parameter格式,也许也可以单独从每个函数中拆封出来,但是不太清楚又拍云的每一个请求是不是格式都一样,看了下视频就有一些不一样,所以没有拆分。代码直接复制保存就可以用,其中的函数作用也备注出来了,小白估计都能理解。

少说废话,上代码:

<?php
	class upyun{
		
		public $operator_name;
		public $bucket_name;
		public $operator_password_md5;
		public $notify_url;
		
		
		public function __construct($bucket_name, $operator_name, $operator_password_md5, $notify_url){
			$this->operator_name			= $operator_name;
			$this->operator_password_md5	= $operator_password_md5;
			$this->notify_url				= $notify_url;
			$this->bucket_name				= $bucket_name;
		}
		
		
		/* 异步文件拉取 */
		public function spider($url, $save_as, $random=false, $overwrite=true){
			# tasks格式
			$tasks[] = array(
				'url'			=> $url,
				'save_as'		=> $save_as,
				'random'		=> $random,
				'overwrite'		=> $overwrite
			);
			$tasks	= base64_encode(json_encode($tasks));

			# 构成参数并签名
			$parameter = array(
				'bucket_name'		=> $this->bucket_name,
				'notify_url'		=> $this->notify_url,
				'tasks'				=> $tasks,
				'app_name'			=> 'spiderman',
			);
			
			# 构建Header
			$header		= array();
			$header[]	= $this->getAuthorization($this->operator_name, $this->operator_password_md5, $parameter);
			$return		= $this->request_by_curl('http://p0.api.upyun.com/pretreatment/', $parameter, $header);
			$return		= json_decode($return, true);
			return $return[0] != '' ? $return[0] : false;
		}
		
		
		/* 在线解压 */
		public function zip($sources, $save_as, $home_dir=''){
			$tasks[] = array(
				'sources'			=> $sources,
				'save_as'			=> $save_as,
				'home_dir'			=> $home_dir
			);
			$tasks	= base64_encode(json_encode($tasks));
			$parameter = array(
				'bucket_name'		=> $this->bucket_name,
				'notify_url'		=> $this->notify_url,
				'tasks'				=> $tasks,
				'app_name'			=> 'compress',
			);
			$header		= array();
			$header[]	= $this->getAuthorization($this->operator_name, $this->operator_password_md5, $parameter);
			$return		= $this->request_by_curl('http://p0.api.upyun.com/pretreatment/', $parameter, $header);
			$return		= json_decode($return, true);
			return $return[0] != '' ? $return[0] : false;
			
		}
		
		
		/* 在线压缩 */
		public function unzip($sources, $save_as){
			$tasks[] = array(
				'sources'			=> $sources,
				'save_as'			=> $save_as
			);
			$tasks	= base64_encode(json_encode($tasks));
			$parameter = array(
				'bucket_name'		=> $this->bucket_name,
				'notify_url'		=> $this->notify_url,
				'tasks'				=> $tasks,
				'app_name'			=> 'depress',
			);
			$header		= array();
			$header[]	= $this->getAuthorization($this->operator_name, $this->operator_password_md5, $parameter);
			$return		= $this->request_by_curl('http://p0.api.upyun.com/pretreatment/', $parameter, $header);
			$return		= json_decode($return, true);
			return $return[0] != '' ? $return[0] : false;
		}
		
		
		
		
		/**		Private		**/
		
		/* 授权认证返回 */
		private function getAuthorization($operator_name,$operator_password_md5, $parameter){
			$parameter	= $this->SortedDictionary($parameter);
			$str		= '';
			foreach ($parameter as $k=>$v){
				$str .= $k.$v;
			}
			$signature = md5($operator_name.$str.$operator_password_md5);
			#Authorization: UPYUN <operator>:<signature>
			$str = 'Authorization: UPYUN '. $operator_name .':'.$signature;
			return $str;
		}
		
		/* 字典排序 */
		private function SortedDictionary($array){
			ksort($array);
			return $array;
		}
		
		/* 提交参数拼接 */
		private function UrlParameter($array){
			$arr = array();
			foreach($array as $k=>$v){
				$arr[] = $k.'='.$v;
			}
			$arr = implode('&',$arr);
			return $arr;
		}
		
		/* post请求 */
		private function request_by_curl($url,$parameter,$header){
			$post_string = $this->UrlParameter($parameter);
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL,$url);
			curl_setopt($ch, CURLOPT_POSTFIELDS,$post_string);
			curl_setopt($ch, CURLOPT_HEADER, true);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // https请求 不验证证书和hosts
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
			curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
			$data		= curl_exec($ch);
			$headerSize	= curl_getinfo($ch, CURLINFO_HEADER_SIZE);
			$content 	= substr($data, $headerSize);
			curl_close($ch);
			return $content;
		}
		
	}
	
?>
发表评论
评论
大雄回复
2016-11-16 22:36Windows 10ChromeIP:广东
撸过,就是完全看不懂-
山野愚人居回复
2016-11-18 12:13Windows 7FirefoxIP:香港
没用过又拍云的路过……
小俊回复
2016-11-18 21:58Windows 10ChromeIP:广东
楼上都看不懂 那么我这个没学过PHP的人怎么办
Sinkey回复
2016-12-08 15:49Windows 7ChromeIP:浙江
大神,你有没有听过php是世界上最好的语言。。。
老张回复
2016-11-26 23:52Windows 7ChromeIP:北京
同意1楼
钛客志回复
2016-12-05 01:40Android 4.1.1AndroidIP:广东
只用过七牛,没用过又拍云
Sinkey回复
2016-12-08 15:49Windows 7ChromeIP:浙江
七牛我也用过,qshell很强大, 又拍有些云处理api确实还不如七牛~