七牛整合php上传从微信下载接口下载下来的文件

时间:2021-03-04 04:11:08
因为ios系统直接读取不了MP3格式的文件,所以从微信接口下载下来的MP3格式音频上传到七牛后要转码。
 
Sample code:
 

public function doMobileUploadToQiniu() {
global $_GPC,$_W;
$hd=$_GPC['hd'];
$weid=$_W['weid'];
$from_user = $_GPC['from_user'];
$media_id = $_GPC['voiceServerId'];
$access_key = 'xxxxxxxxxxxxxxxxxxxxx'; 
$secret_key = 'xxxxxxxxxxxxxxxxxxxxx';

$access_token_json = $_W['account']['access_token']['token'];

$downloadurl = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token='.$access_token_json.'&media_id='.$media_id;
$fetch = $this->urlsafe_base64_encode($downloadurl); 
$target = $media_id.".amr";

$to = $this->urlsafe_base64_encode($target); 
$url = 'http://iovip.qbox.me/fetch/'. $fetch .'/to/' . $to;

$access_token = $this->generate_access_token($access_key, $secret_key, $url); 
$header[] = 'Content-Type: application/json'; 
$header[] = 'Authorization: QBox '. $access_token;

$con = $this->send('iovip.qbox.me/fetch/'.$fetch.'/to/'.$to, $header); 
$id = $this -> dataTransfer($access_key, $secret_key, $media_id);
return $id;
}

/**

*将音频格式转码成amr格式

*/

public function dataTransfer($access_key, $secret_key, $media_id) {
$auth = new Auth($access_key, $secret_key);

$bucket = 'chuangyi-file-01';
$key = "".$media_id.".amr";
$pfop = New PersistentFop($auth, $bucket);

$fops='avthumb/mp3/ab/128k/ar/44100/acodec/libmp3lame';
list($id, $err, $r) = $pfop->execute($key, $fops);

if ($err != null) {
message("系统错误,请稍后再试");
} else {
return $id;
}
}

public function send($url, $header = '') { 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_HEADER,1); 
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
curl_setopt($curl, CURLOPT_POST, 1);

$con = curl_exec($curl); 
if ($con === false) {

return $error;
} else { 
return $con; 

}

/** 
* generate_access_token 

* @desc 签名运算 
* @param string $access_key 
* @param string $secret_key 
* @param string $url 
* @param array $params 
* @return string 
*/ 
public function generate_access_token($access_key, $secret_key, $url, $params = ''){ 
$parsed_url = parse_url($url); 
$path = $parsed_url['path']; 
$access = $path; 
if (isset($parsed_url['query'])) { 
$access .= "?" . $parsed_url['query']; 

$access .= "\n"; 
if($params){ 
if (is_array($params)){ 
$params = http_build_query($params); 

$access .= $params; 

$digest = hash_hmac('sha1', $access, $secret_key, true); 
return $access_key.':'.$this->urlsafe_base64_encode($digest); 
}