/ 7评 /

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

工作需要,恰好按照文档整理出一个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;
		}
		
	}
	
?>

7条回应:“又拍云云处理php类”

  1. 大雄说道:

    撸过,就是完全看不懂-

  2. 山野愚人居说道:

    没用过又拍云的路过……

  3. 小俊说道:

    楼上都看不懂 那么我这个没学过PHP的人怎么办

  4. 钛客志说道:

    只用过七牛,没用过又拍云

发表评论

电子邮件地址不会被公开。 必填项已用*标注