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); 
    }

  • 相关阅读:
    linux系统/var/log目录下的信息详解
    SQL 显示表名显示列名
    P2P平台介绍
    outlook署名最后一行没换行
    CSS3下的渐变文字效果实现
    SSH(poderosa)を使って、さくらのMySQLサーバーに接続する方法
    内网IP外网IP的关联及访问互联网原理
    自己吃的哑巴亏,怎么也要吞下去
    解决Ehcache缓存警告问题
    管理的艺术
  • 原文地址:https://www.cnblogs.com/mark545/p/4615615.html
Copyright © 2011-2022 走看看