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 }