浏览代码

修复http_post文件素材上传错误 issue #132

dodgepudding 10 年之前
父节点
当前提交
03295c1be7
共有 4 个文件被更改,包括 66 次插入62 次删除
  1. 4 3
      Thinkphp/Wechat.class.php
  2. 29 28
      Thinkphp/qywechat.class.php
  3. 29 28
      qywechat.class.php
  4. 4 3
      wechat.class.php

+ 4 - 3
Thinkphp/Wechat.class.php

@@ -856,15 +856,16 @@ class Wechat
 	 * POST 请求
 	 * @param string $url
 	 * @param array $param
+	 * @param boolean $post_file 是否文件上传
 	 * @return string content
 	 */
-	private function http_post($url,$param){
+	private function http_post($url,$param,$post_file=false){
 		$oCurl = curl_init();
 		if(stripos($url,"https://")!==FALSE){
 			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
 			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
 		}
-		if (is_string($param)) {
+		if (is_string($param) || $post_file) {
 			$strPOST = $param;
 		} else {
 			$aPOST = array();
@@ -1102,7 +1103,7 @@ class Wechat
 	 */
 	public function uploadMedia($data, $type){
 		if (!$this->access_token && !$this->checkAuth()) return false;
-		$result = $this->http_post(self::UPLOAD_MEDIA_URL.self::MEDIA_UPLOAD.'access_token='.$this->access_token.'&type='.$type,$data);
+		$result = $this->http_post(self::UPLOAD_MEDIA_URL.self::MEDIA_UPLOAD.'access_token='.$this->access_token.'&type='.$type,$data,true);
 		if ($result)
 		{
 			$json = json_decode($result,true);

+ 29 - 28
Thinkphp/qywechat.class.php

@@ -229,35 +229,36 @@ class Wechat
 	 * POST 请求
 	 * @param string $url
 	 * @param array $param
+	 * @param boolean $post_file 是否文件上传
 	 * @return string content
 	 */
-	private function http_post($url,$param){
-	    $oCurl = curl_init();
-	    if(stripos($url,"https://")!==FALSE){
-	        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
-	        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
-	    }
-	    if (is_string($param)) {
-	        $strPOST = $param;
-	    } else {
-	        $aPOST = array();
-	        foreach($param as $key=>$val){
-	            $aPOST[] = $key."=".urlencode($val);
-	        }
-	        $strPOST =  join("&", $aPOST);
-	    }
-	    curl_setopt($oCurl, CURLOPT_URL, $url);
-	    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
-	    curl_setopt($oCurl, CURLOPT_POST,true);
-	    curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
-	    $sContent = curl_exec($oCurl);
-	    $aStatus = curl_getinfo($oCurl);
-	    curl_close($oCurl);
-	    if(intval($aStatus["http_code"])==200){
-	        return $sContent;
-	    }else{
-	        return false;
-	    }
+	private function http_post($url,$param,$post_file=false){
+		$oCurl = curl_init();
+		if(stripos($url,"https://")!==FALSE){
+			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
+			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
+		}
+		if (is_string($param) || $post_file) {
+			$strPOST = $param;
+		} else {
+			$aPOST = array();
+			foreach($param as $key=>$val){
+				$aPOST[] = $key."=".urlencode($val);
+			}
+			$strPOST =  join("&", $aPOST);
+		}
+		curl_setopt($oCurl, CURLOPT_URL, $url);
+		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
+		curl_setopt($oCurl, CURLOPT_POST,true);
+		curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
+		$sContent = curl_exec($oCurl);
+		$aStatus = curl_getinfo($oCurl);
+		curl_close($oCurl);
+		if(intval($aStatus["http_code"])==200){
+			return $sContent;
+		}else{
+			return false;
+		}
 	}
 
 	/**
@@ -991,7 +992,7 @@ class Wechat
 	 */
 	public function uploadMedia($data, $type){
 		if (!$this->access_token && !$this->checkAuth()) return false;
-		$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOAD.'access_token='.$this->access_token.'&type='.$type,$data);
+		$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOAD.'access_token='.$this->access_token.'&type='.$type,$data,true);
 		if ($result)
 		{
 			$json = json_decode($result,true);

+ 29 - 28
qywechat.class.php

@@ -224,35 +224,36 @@ class Wechat
 	 * POST 请求
 	 * @param string $url
 	 * @param array $param
+	 * @param boolean $post_file 是否文件上传
 	 * @return string content
 	 */
-	private function http_post($url,$param){
-	    $oCurl = curl_init();
-	    if(stripos($url,"https://")!==FALSE){
-	        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
-	        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
-	    }
-	    if (is_string($param)) {
-	        $strPOST = $param;
-	    } else {
-	        $aPOST = array();
-	        foreach($param as $key=>$val){
-	            $aPOST[] = $key."=".urlencode($val);
-	        }
-	        $strPOST =  join("&", $aPOST);
-	    }
-	    curl_setopt($oCurl, CURLOPT_URL, $url);
-	    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
-	    curl_setopt($oCurl, CURLOPT_POST,true);
-	    curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
-	    $sContent = curl_exec($oCurl);
-	    $aStatus = curl_getinfo($oCurl);
-	    curl_close($oCurl);
-	    if(intval($aStatus["http_code"])==200){
-	        return $sContent;
-	    }else{
-	        return false;
-	    }
+	private function http_post($url,$param,$post_file=false){
+		$oCurl = curl_init();
+		if(stripos($url,"https://")!==FALSE){
+			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
+			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
+		}
+		if (is_string($param) || $post_file) {
+			$strPOST = $param;
+		} else {
+			$aPOST = array();
+			foreach($param as $key=>$val){
+				$aPOST[] = $key."=".urlencode($val);
+			}
+			$strPOST =  join("&", $aPOST);
+		}
+		curl_setopt($oCurl, CURLOPT_URL, $url);
+		curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
+		curl_setopt($oCurl, CURLOPT_POST,true);
+		curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
+		$sContent = curl_exec($oCurl);
+		$aStatus = curl_getinfo($oCurl);
+		curl_close($oCurl);
+		if(intval($aStatus["http_code"])==200){
+			return $sContent;
+		}else{
+			return false;
+		}
 	}
 
 	/**
@@ -982,7 +983,7 @@ class Wechat
 	 */
 	public function uploadMedia($data, $type){
 		if (!$this->access_token && !$this->checkAuth()) return false;
-		$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOAD.'access_token='.$this->access_token.'&type='.$type,$data);
+		$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOAD.'access_token='.$this->access_token.'&type='.$type,$data,true);
 		if ($result)
 		{
 			$json = json_decode($result,true);

+ 4 - 3
wechat.class.php

@@ -851,15 +851,16 @@ class Wechat
 	 * POST 请求
 	 * @param string $url
 	 * @param array $param
+	 * @param boolean $post_file 是否文件上传
 	 * @return string content
 	 */
-	private function http_post($url,$param){
+	private function http_post($url,$param,$post_file=false){
 		$oCurl = curl_init();
 		if(stripos($url,"https://")!==FALSE){
 			curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
 			curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
 		}
-		if (is_string($param)) {
+		if (is_string($param) || $post_file) {
 			$strPOST = $param;
 		} else {
 			$aPOST = array();
@@ -1092,7 +1093,7 @@ class Wechat
 	 */
 	public function uploadMedia($data, $type){
 		if (!$this->access_token && !$this->checkAuth()) return false;
-		$result = $this->http_post(self::UPLOAD_MEDIA_URL.self::MEDIA_UPLOAD.'access_token='.$this->access_token.'&type='.$type,$data);
+		$result = $this->http_post(self::UPLOAD_MEDIA_URL.self::MEDIA_UPLOAD.'access_token='.$this->access_token.'&type='.$type,$data,true);
 		if ($result)
 		{
 			$json = json_decode($result,true);