最近干的杂活(附PHP微信素材模拟上传源码)

        离上次写博客又有几天时间了,来抱怨一下。


  1. 弄了一个在线文字转语音的网站,网址是: www.hoovip.com ,使用的是科大讯飞的SDK包,前台与后台通过队列通信。弄了几天今天上线,哎,终于完成任务。
  2. 前几天www.aidemofang.com一直打不开,50VZ坑死我了,看来买vps还是得备案买国内的,不然被坑死去。
  3. 下一步准备重新整一下www.xiuxiandou.com电影网,最后再专家整淘宝客....
  4. 还是上码吧
  5.  public function uploadFile($file_url,$wei_id='idemofang',$is_img=true)
        {
            //先下载
            $_filename    =   download_image($file_url);
            if(!$_filename){
                echo("DOWN ERROR");
    
                return false;
            }
            $file_name      =   $_filename;//basename($file_url);
            $file_ext       =   $is_img?$this->_getImageExt($file_url):"audio/wav";
            $target_url     =   "https://mp.weixin.qq.com/cgi-bin/filetransfer?action=upload_material&f=json&scene=1&writetype=doublewrite&groupid=1&ticket_id={$wei_id}&ticket=".$this->getTicket()."&svr_time=".time()."&token=".$this->getToken()."&lang=zh_CN&seq=1";
    
    
            $eol            =   "\r\n"; //default line-break for mime type
            $BOUNDARY       =   md5(time()); //random boundaryid, is a separator for each param on my post curl function
            $BODY           =   ""; //init my curl body
            $BODY          .=  '--'.$BOUNDARY. $eol; //start param header
            $BODY          .=   'Content-Disposition: form-data; name="id"' . $eol . $eol; // last Content with 2 $eol, in this case is only 1 content.
            $BODY          .=   "WU_FILE_0" . $eol;//param data in this case is a simple post data and 1 $eol for the end of the data
            $BODY          .=   '--'.$BOUNDARY. $eol; //start param header
            $BODY          .=   'Content-Disposition: form-data; name="name"' . $eol . $eol; // last Content with 2 $eol, in this case is only 1 content.
            $BODY          .=   $file_name . $eol;//param data in this case is a simple post data and 1 $eol for the end of the data
            $BODY          .=   '--'.$BOUNDARY. $eol; //start param header
            $BODY          .=   'Content-Disposition: form-data; name="type"' . $eol . $eol; // last Content with 2 $eol, in this case is only 1 content.
            $BODY          .=   $file_ext . $eol;//param data in this case is a simple post data and 1 $eol for the end of the data
            $BODY          .=   '--'.$BOUNDARY. $eol; //start param header
            $BODY          .=   'Content-Disposition: form-data; name="lastModifiedDate"' . $eol . $eol; // last Content with 2 $eol, in this case is only 1 content.
            $BODY          .=   gmdate("D M d Y H:i:s")." GMT+0800 (CST)" . $eol;//param data in this case is a simple post data and 1 $eol for the end of the data
            $BODY          .=   '--'.$BOUNDARY. $eol; //start param header
            $BODY          .=   'Content-Disposition: form-data; name="size"' . $eol . $eol; // last Content with 2 $eol, in this case is only 1 content.
            $BODY          .=   filesize(MY_UPLOAD_PATH.$_filename) . $eol;//param data in this case is a simple post data and 1 $eol for the end of the data
            $BODY          .=   '--'.$BOUNDARY. $eol; // start 2nd param,
            $BODY          .=   'Content-Disposition: form-data; name="file"; filename="'.$file_name.'"'. $eol ; //first Content data for post file, remember you only put 1 when you are going to add more Contents, and 2 on the last, to close the Content Instance
            $BODY          .=   'Content-Type:'.$file_ext . $eol; //Same before row  WAV:'Content-Type: application/octet-stream' . $eol; //Same before row
            $BODY          .=   'Content-Transfer-Encoding: base64' . $eol . $eol; // we put the last Content and 2 $eol,
            //在这里被搞死了,原来只要file_get_contetns就可以了
            $BODY          .=   file_get_contents($file_url) . $eol; // we write the Base64 File Content and the $eol to finish the data,
            $BODY          .=   '--'.$BOUNDARY .'--' . $eol. $eol; // we close the param and the post width "--" and 2 $eol at the end of our boundary header.
            $ch             =   curl_init(); //init curl
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'X_PARAM_TOKEN : 71e2cb8b-42b7-4bf0-b2e8-53fbd2f578f9' //custom header for my api validation you can get it from $_SERVER["HTTP_X_PARAM_TOKEN"] variable
            ,"Content-Type: multipart/form-data; boundary=".$BOUNDARY) //setting our mime type for make it work on $_FILE variable
            );
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/1.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0'); //setting our user agent
            curl_setopt($ch, CURLOPT_URL, $target_url); //setting our api post url
            curl_setopt( $ch, CURLOPT_COOKIEJAR,  $this->_getCookieFile());
            curl_setopt( $ch, CURLOPT_COOKIEFILE,  $this->_getCookieFile());
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // call return content
            curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); //navigate the endpoint
            curl_setopt($ch, CURLOPT_POST, true); //set as post
            curl_setopt($ch, CURLOPT_POSTFIELDS, $BODY); // set our $BODY
            $response = curl_exec($ch); // start curl navigation
            curl_close($ch);
            return $response;
        }



您有什么不同的意见或看法? 欢迎留言共同学习,谢谢。

本文链接:http://www.hihubs.com/article/278

关键字:最近干的杂活(附PHP微信素材模拟上传源码)

若无特别注明,文章皆为Hubs'm原创,转载请注明出处...O(∩_∩)O