zoukankan      html  css  js  c++  java
  • php5.6+上传公众号素材,由于php版本不同,导致curl也得有变化

    php5.5上下的php版本的CURL有差异

    php5.5版本(包含)以下:

     1 public function upload_temporary_material($type, $file)
     2     {
     3         if (PHP_OS == "Linux"){        //Linux
     4             $data = array("media"  => "@".dirname(__FILE__).'/'.$file);
     5         }else{                        //WINNT
     6             $data = array("media"  => "@".dirname(__FILE__).'\'.$file);
     7         }
     8         $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->access_token."&type=".$type;
     9         $res = $this->http_request($url, $data);
    10         return json_decode($res, true);
    11     }
    12 protected function http_request($url, $data = null)
    13     {
    14         $curl = curl_init();
    15         curl_setopt($curl, CURLOPT_URL, $url);
    16         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    17         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    18         if (!empty($data)){
    19             curl_setopt($curl, CURLOPT_POST, 1);
    20             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    21         }
    22         curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    23         $output = curl_exec($curl);
    24         curl_close($curl);
    25         return $output;
    26     }

    5.5的会报一个让你用CURLFile()来代替,但是不影响使用

    php5.5版本以上:

     1     public function upload_temporary_m($type, $file)
     2     {
     3         $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->access_token."&type=".$type;
     4         $res = $this->http_request1($url, $file);
     5         return json_decode($res, true);
     6     }
     7     
     8     protected function http_request1($url,$path){
     9         $curl = curl_init();
    10  
    11         curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
    12         $data = array('file' => new CURLFile(realpath($path)));//
    13         curl_setopt($curl, CURLOPT_URL, $url); 
    14         curl_setopt($curl, CURLOPT_POST, 1 ); 
    15         curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
    16         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    17         curl_setopt($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']); 
    18         $result = curl_exec($curl);
    19         return $result;
    20     }
  • 相关阅读:
    Linq技术四:动态Linq技术 -- Linq.Expressions
    74HC595的中文资料
    MessageDigest简单介绍
    gpu显存(全局内存)在使用时数据对齐的问题
    走进windows编程的世界-----windows进程
    Matlab画图-非常具体,非常全面
    php实现 合并表记录(需求是最好的老师)
    php实现求一个数的质数因子
    php课程 4-16 数组自定义函数(php数组->桶)
    jquery-3 jquery选择器
  • 原文地址:https://www.cnblogs.com/wutianfei/p/10929470.html
Copyright © 2011-2022 走看看