详解使用php调用微信接口上传永久素材

时间:2017-04-26

说明:该方法为调用微信上传永久素材接口方法

/**
 * 上传永久图文素材(认证后的订阅号可用)
 * 新增的永久素材也可以在公众平台官网素材管理模块中看到
 * @param array $data 消息结构{"articles":[{...}]}
 * @return boolean|array
 */
public function uploadForeverArticles($access_token,$data){
 error_log('post data is=======> '.self::json_encode($data));
 $url = self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPLOAD_URL.'access_token='.$access_token;
 $result = HTTP::request($url, 'POST', self::json_encode($data));
 error_log('weixin return result is =====>'.$result);
 if ($result)
 {
  $json = json_decode($result,true);
  if (!$json || !empty($json['errcode'])) {
   $this->errCode = $json['errcode'];
   $this->errMsg = $json['errmsg'];
   return false;
  }
  return $json;
 }
 return false;
}

upload_meterial方法

说明:该方法为调用微信上传永久素材接口方法

  /**
   * 请注意该方法必须保证php的版本在5.6以下,否则会爆40015错误
   */
  function upload_meterial($file_info,$access_token){
   $url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$access_token}&type=image";
   $ch1 = curl_init ();
   $timeout = 5;
   $real_path="{$file_info['filename']}";
   //$real_path=str_replace("/", "\\", $real_path);
   $data= array("media"=>"@{$real_path}",'form-data'=>$file_info);
   curl_setopt ( $ch1, CURLOPT_URL, $url );
   curl_setopt ( $ch1, CURLOPT_POST, 1 );
   curl_setopt ( $ch1, CURLOPT_RETURNTRANSFER, 1 );
   curl_setopt ( $ch1, CURLOPT_CONNECTTIMEOUT, $timeout );
   curl_setopt ( $ch1, CURLOPT_SSL_VERIFYPEER, FALSE );
   curl_setopt ( $ch1, CURLOPT_SSL_VERIFYHOST, false );
   curl_setopt ( $ch1, CURLOPT_POSTFIELDS, $data );
   $result = curl_exec ( $ch1 );
   echo '<br/>';
   echo 'reulst is ==========>'.$result;
   curl_close ( $ch1 );
   if(curl_errno()==0){
    $result=json_decode($result,true);
    //var_dump($result);
    return $result['media_id'];
   }else {
    return false;
   }
  }

http_post方法

说明:该方法为调http post请求方法

/**
 * POST 请求
 * @param string $url
 * @param array $param
 * @param boolean $post_file 是否文件上传
 * @return string content
 */
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);
  curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
 }
 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;
 }
}

遇到的问题

  • 共3页:
  • 上一页
  • 2/3
  • 下一页
  • 上一篇:php动态读取数据清除最右边距的方法 下一篇:php array_reverse 以相反的顺序返回数组实例代码

    相关文章

    最新文章