zoukankan      html  css  js  c++  java
  • 七牛整合php上传从微信下载接口下载下来的文件

    因为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 .= " "; 
    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); 
    }

  • 相关阅读:
    ASP.NET常见安全缺陷集锦[转]
    林黛玉出家:给予中国给予我们的警示![摘]
    关于管理 关于经营 还有很多东西都无从做好
    ASP.NET 2.0的新增服务、控件与功能
    将可视的DataGrid、DataList等的规范表数据导出并写入xls或doc格式保存
    2006全球最具影响力品牌
    .NET环境下水晶报表使用总结[转]
    Asp.net(C#)分层——基础类
    Asp.Net传参方式小结
    MSN签名都成了广告位
  • 原文地址:https://www.cnblogs.com/mark545/p/4615615.html
Copyright © 2011-2022 走看看