亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

首頁 微信小程式 微信開發(fā) php微信高級(jí)介面群發(fā) 多客服

php微信高級(jí)介面群發(fā) 多客服

Feb 20, 2017 pm 02:51 PM

這篇文章主要為大家詳細(xì)介紹了php微信高階介面群發(fā)、多客服的相關(guān)資料,有興趣的朋友們可以參考一下

本文實(shí)例為大家分享了php微信高階介面群發(fā)、多客服源碼,供大家參考,具體內(nèi)容如下

/**
 * 微信接口調(diào)用
 * 依賴
 * 全局變量
 * global $uid 公眾號(hào)用戶id, $wid 公眾號(hào)id, $wechatid 粉絲唯一id;
 * 參數(shù)
 * $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
 * $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
 * 緩存類 自定義
 * Cache:set
 * Cache:get
 * 具體業(yè)務(wù)修改
 * 1.上傳圖文信息至微信素材庫
 * function uploadArticlesToWeiXinServer()
 * 2.關(guān)鍵字匹配圖文回復(fù)
 * function getArticleData()
 * 
 * usage:
 *  $options = array(
 *  'token'=>'tokenaccesskey', //填寫你設(shè)定的key
 *  'appid'=>'wxdk1234567890', //填寫高級(jí)調(diào)用功能的app id
 *  'appsecret'=>'xxxxxxxxxxxxxxxxxxx', //填寫高級(jí)調(diào)用功能的密鑰
 *  );
 */
class WeiXinTool {

  private $appid;
  private $appsecret;
  private $access_token;
  private $mediaType = array('image' => array("jpg"), 'voice' => array('amr', 'MP3'), 'video' => array('mp4'), 'thumb' => array("jpg"));
  private $mediaMaxSize = array('image' => 131072, 'voice' => 262144, 'video' => 1048576, 'thumb' => 65536);
  private $tem_file_path = "";

//  授權(quán)地址
  const AUTH_URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
//  素材上傳
  const UPLOAD_MEDIA_URL = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s";
  const GET_MEDIA_URL = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s";
  const UPLOAD_NEWS_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=%s";

  public function __construct($options) {
    $this->appid = isset($options['appid']) ? $options['appid'] : '';
    $this->appsecret = isset($options['appsecret']) ? $options['appsecret'] : '';
    //需修改
    //上傳圖片臨時(shí)文件目錄自定義
    $this->tem_file_path = YYUC_FRAME_PATH . YYUC_PUB . '/' . Session::get('upath');
  }

  /**
   * 需修改
   * 具體業(yè)務(wù)需求,圖文信息上傳至素材庫
   * 微站文章上傳至微信素材
   * @param type $wid 
   * @param type $aid
   * @return int
   */
  public function uploadArticlesToWeiXinServer($wid, $aid) {

    //具體圖文組裝過程,需修改
    $m = new Model('website_article');
    $m_pubs = new Model('pubs');
    $m_pubs->find(array("id" => $wid));
    $m->find(array("wid" => $wid, 'id' => $aid));
    $res = array();
    if ($m->has_id() && $m_pubs->has_id()) {
      $res[] = $m->get_model_array();
//      var_dump($res);
      $m->votetouser = json_decode($m->votetouser, TRUE);
      $articles = $m->votetouser[0];
      $m_article = new Model('website_article');
      $ress = $m_article->where(array('wid' => $wid, 'id' => $articles))->list_all_array();
      $res = array_merge($res, $ress);
    } else {
      $errarr = array();
      $errarr['errcode'] = 44003;
      $errarr['errmsg'] = self::$errorno[$errarr['errcode']];
      return $errarr;
    }
    $items = array();
    foreach ($res as $k => $v) {
      $mediaid = $this->uploadMedia($v['picurl']);
      if ($mediaid['media_id']) {
        $thumb_media_id = $mediaid['media_id'];
      } else {
        return $mediaid;
      }
      $item = array(
        "thumb_media_id" => $thumb_media_id,
        "author" => $m_pubs->pubun,
        "title" => $v['title'],
        "content_source_url" => WeiXinTool::complateUrl(WeiSite::parseArticleLinkData($v)),
        "content" => $v['reply_content'], //內(nèi)容 富文本
        "digest" => $v['description']//描述
      );
      $items[] = $item;
    }
    //以上具體圖文組裝過程,需修改

    $postData['articles'] = $items;
    $error = $this->uploadNews($postData);
    return $error;
  }

  /**
   * 需修改
   * 微站文章關(guān)鍵字匹配數(shù)據(jù)解析
   */
  public static function getArticleData($keyword) {
    global $wid;
    $m = new Model('website_article');
    $m->find(array('wid' => $wid, 'keyword@~' => " " . $keyword . " "));
    $res = array();
    if ($m->has_id()) {
      $res[] = array("tit" => $m->title, "pic" => $m->picurl, "dec" => $m->description, "url" => WeiSite::parseArticleLinkData($m->get_model_array()));
      $m->votetouser = json_decode($m->votetouser, TRUE);
      $articles = $m->votetouser[0];
      if (!empty($articles)) {
        foreach ($articles as $v) {
          $m_article = new Model('website_article');
          $m_article->find(array('wid' => $wid, 'id' => $v));
          $res[] = array("tit" => $m_article->title, "pic" => $m_article->picurl, "dec" => $m_article->description, "url" => WeiSite::parseArticleLinkData($m_article->get_model_array()));
        }
      }
      return $res;
    }
  }

  /**
   * 獲取accesstoken
   * @param type $flag 強(qiáng)制刷新accesstoken 開關(guān)
   * @return type
   */
  public function getAccessToken($flag = FALSE) {
    $url = sprintf(self::AUTH_URL, $this->appid, $this->appsecret);
    $result = Cache::get(md5($url));
    if ($flag || empty($result)) {
      $result = $this->http_get($url);
      $result = json_decode($result, TRUE);
      if ($result['errcode']) {
        return $result['errcode'];
      }
      Cache::set(md5($url), $result, 1);
    }
    $this->access_token = $result['access_token'];
    return true;
  }

  /**
   * 上傳媒體
   * @param type $file 媒體文件 $url或者物理路徑地址
   * @param type $type 類型 
   * @return int
   * return 
   array (size=3)
   'type' => string 'image' (length=5)
   'media_id' => string '-0Lr3rX9mDYBB7i5bDydvwFHHm3zW2Uxt0OoDFBPmGRfYiwckiALqHH_DlP9jCm_' (length=64)
   'created_at' => int 1400477181
   */
  public function uploadMedia($file, $type = "image") {
    $file = self::complateUrl($file);
    $urlarr = parse_url($file);
    $filetype = explode(".", $urlarr['path']);
    $filetype = strtolower($filetype[count($filetype) - 1]);
    $resizeSize = 100; //圖片處理后另存質(zhì)量
    if (!key_exists($type, $this->mediaType) || !in_array($filetype, $this->mediaType[$type])) {
//      return 40005; //格式錯(cuò)誤
      $errarr = array();
      $errarr['errcode'] = 40005;
      $errarr['errmsg'] = self::$errorno[$errarr['errcode']];
      return $errarr;
    }

    $temp_file = $this->tem_file_path . 'uploadMedia.' . $filetype;
    $temp_file_resize = $this->tem_file_path . 'uploadMediaResize.' . $filetype;
    file_put_contents($temp_file, self::http_get($file));
    $handle = fopen($temp_file, "r");
    $fstat = fstat($handle);

    if ($fstat['size'] > $this->mediaMaxSize[$type]) {
      $resizeSize = intval($this->mediaMaxSize[$type] / $fstat['size'] * 100);
      ImageTool::resizeImage($temp_file_resize, $temp_file, 400, 400, $resizeSize); //圖片太大再處理壓縮
      $temp_file = $temp_file_resize;
//      return 40006; //大小錯(cuò)誤
    }
    $filePath = realpath($temp_file);
    $uploadUrl = sprintf(self::UPLOAD_MEDIA_URL, $this->access_token, $type);
    $postData = array("r" => time(), 'media' => "@{$filePath}");
    $result = self::http_post($uploadUrl, $postData);
    $result = json_decode($result, TRUE);
    return $result;
  }

  /**
   * 群發(fā)圖文信息
   * @param type $touser 粉絲數(shù)組/粉絲組id
   * @param type $media_id
   * @return type
   */
  public function sendArticles($touser, $media_id) {
    $errarr = array();
    $postData = array();
    $postData['mpnews'] = array("media_id" => $media_id);
    $postData['msgtype'] = "mpnews";
    if (is_array($touser)) {
      //用戶列表群發(fā)
      $postData['touser'] = $touser;
      $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=%s';
    } else {
      $group = intval($touser);
      $groups = $this->getGroups(true);
      if (key_exists($group, $groups['list'])) {
        $postData['filter'] = array("group_id" => $group);
      } else {
        $errarr['errcode'] = 40050; //無效分組id
        $errarr['errmsg'] = self::$errorno[$errarr['errcode']];
        return $errarr;
      }
      $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=%s';
    }
    $url = sprintf($url, $this->access_token);
    $result = self::http_post($url, self::json_encode($postData));
    $result = json_decode($result, TRUE);
    return $result;
  }

  /**
   * 刪除群發(fā)信息
   * @param type $msgid
   * @return type
   */
  public function delSend($msgid) {
    $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token=%s';
    $url = sprintf($url, $this->access_token);
    $postData = array('msgid' => $msgid, 'r' => time());
    $result = self::http_post($url, self::json_encode($postData));
    $result = json_decode($result, TRUE);
    return $result;
  }

  /**
   * 群發(fā)圖文素材上傳
   $postData = array('articles' => array($item, $item,...));
   $item = array(
   "thumb_media_id" => "WMQubqCECMQwAjqh8CI500LfhyoG0vmTTlKKJM5oP-of0uLML1_2s26j8XeIorDL",
   "author" => "xxx",
   "title" => "Happy Day",
   "content_source_url" => "www.qq.com",
   "content" => "content",
   "digest" => "digest"
   );
   * return 
   array (size=3)
   'type' => string 'news' (length=4)
   'media_id' => string 'OuXqv2dgZzxAmK4z-tvStgr6InG18oIllWkD6Tj1qJZVRg-2f64FDU2D3J7dptHs' (length=64)
   'created_at' => int 1400477183
   */
  public function uploadNews($postData) {
    $uploadUrl = sprintf(self::UPLOAD_NEWS_URL, $this->access_token);
    $result = self::http_post($uploadUrl, self::json_encode($postData));
    $result = json_decode($result, TRUE);
    return $result;
  }

  /**
   * 獲取粉絲列表
   * @param type $nextOpenId
   * @return type
   */
  public function getAllConnects($nextOpenId = "") {
    $url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s';
    $url = sprintf($url, $this->access_token, $nextOpenId);
    $result = self::http_get($url);
    $result = json_decode($result, TRUE);
    $count = count($result['data']['openid']);
    $list = $result['data']['openid'];
    if ($result['data']['openid'][$count - 1] == $result['next_openid']) {
      return $result['data']['openid'];
    } else {
      $templist = $this->getAllConnects($result['next_openid']);
      $list = array_merge($list, $templist);
      return $list;
    }
  }

  /**
   * 根據(jù)粉絲唯一id獲取微信信息
   * @param type $openid
   * @return type
   */
  public function getFansInfo($openid) {
    $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN';
    $url = sprintf($url, $this->access_token, $openid);
    $result = self::http_get($url);
    $result = json_decode($result, TRUE);
    return $result;
  }

  /**
   * 更新粉絲組信息
   * @param type $openid
   * @param type $groupid
   * @return type
   */
  public function updateFansGroups($openid, $groupid) {
    $url = 'https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=%s';
    $url = sprintf($url, $this->access_token);
    $postData = array("to_groupid" => $groupid, 'openid' => $openid);
    $result = self::http_post($url, self::json_encode($postData));
    $result = json_decode($result, TRUE);
    $this->getGroups(true);
    return $result;
  }

  /**
   * 獲取粉絲組信息
   * @param type $openid
   * @return type
   */
  public function getFansGroupInfo($openid) {
    $url = 'https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=%s';
    $url = sprintf($url, $this->access_token);
    $postData = array("r" => time(), 'openid' => $openid);
    $result = self::http_post($url, self::json_encode($postData));
    $result = json_decode($result, TRUE);
    return $result['groupid'];
  }

  /**
   * 獲取唯一key
   * @param type $key
   * @return type
   */
  public function getKey($key) {
    return md5($this->appid . $this->appsecret . $key);
  }

  /**
   * 獲取媒體圖片到本地
   * @param type $mediaid
   * @return string
   */
  public function getMedia($mediaid) {
    $url = sprintf(self::GET_MEDIA_URL, $this->access_token, $mediaid);
    $result = self::http_get($url);
    $temp_file = $this->tem_file_path . 'getMedia.jpg';
    file_put_contents($temp_file, $result);
    return $temp_file;
  }

  /**
   * 用戶組
   * @param type $flag 強(qiáng)制刷新用戶組
   * @return type
   */
  public function getGroups($flag = false) {
    $key = $this->appid . 'gasdfev' . $this->appsecret;
    $result = Cache::get($key);
    if (empty($result) || $flag) {
      $url = 'https://api.weixin.qq.com/cgi-bin/groups/get?access_token=%s';
      $url = sprintf($url, $this->access_token);
      $result = self::http_get($url);
      $result = json_decode($result, TRUE);
      $temg = array();
      $temlist = array();
      foreach ($result['groups'] as $k => $v) {
        $temg[$v['id']] = $v;
        $temlist[$v['id']] = $v['name'];
      }
      $result['map'] = $temg;
      $result['list'] = $temlist;
      Cache::set($key, $result);
    }
    return $result;
  }

  /**
   * 新增用戶組
   * @param type $name
   * @return type
   */
  public function createGroup($name) {
    $url = 'https://api.weixin.qq.com/cgi-bin/groups/create?access_token=%s';
    $url = sprintf($url, $this->access_token);
    $result = self::http_post($url, self::json_encode(array('group' => array('name' => $name))));
    $result = json_decode($result, TRUE);
    return $result;
  }

  /**
   * 修改用戶組
   * @param type $id
   * @param type $name
   * @return type
   */
  public function modifyGroup($id, $name) {
    $url = 'https://api.weixin.qq.com/cgi-bin/groups/update?access_token=%s';
    $url = sprintf($url, $this->access_token);
    $result = self::http_post($url, self::json_encode(array('group' => array('id' => $id, 'name' => $name))));
    $result = json_decode($result, TRUE);
    return $result;
  }

  /**
   * 多客服接入
   * @global type $wid
   * @param type $postObj
   */
  public static function responseService($postObj) {
    global $wid;
    $fromUsername = $postObj->FromUserName;
    $toUsername = $postObj->ToUserName;
    $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[transfer_customer_service]]></MsgType>
</xml>";
    $resstr = sprintf($textTpl, $fromUsername, $toUsername, time());
    echo $resstr;
  }

  /**
   * 微信回復(fù)多圖文
   * @global type $wid
   * @param type $res
   * array(array(),array()
   * );
   * @param type $rid
   * @param type $postObj
   */
  public static function response_morearts($res, $postObj) {
    global $wid;
    $fromUsername = $postObj->FromUserName;
    $toUsername = $postObj->ToUserName;
    $textTpl = "<xml>
 <ToUserName><![CDATA[%s]]></ToUserName>
 <FromUserName><![CDATA[%s]]></FromUserName>
 <CreateTime>%s</CreateTime>
 <MsgType><![CDATA[%s]]></MsgType>
 <ArticleCount>%s</ArticleCount>
 <Articles>
 ITEM
 </Articles>
 </xml>";
    $resstr = sprintf($textTpl, $fromUsername, $toUsername, time(), "news", count($res));
    $item = &#39;&#39;;
    $subitem = "<item>
 <Title><![CDATA[%s]]></Title>
 <Description><![CDATA[%s]]></Description>
 <PicUrl><![CDATA[%s]]></PicUrl>
 <Url><![CDATA[%s]]></Url>
 </item>";
    foreach ($res as $r) {
      $r[&#39;url&#39;] = self::parseUrl($r[&#39;url&#39;]);
      $item .= sprintf($subitem, $r[&#39;tit&#39;], $r[&#39;des&#39;], self::complateUrl($r[&#39;pic&#39;]), self::replaceToWXUrl(self::complateUrl($r[&#39;url&#39;]), $postObj));
    }
    $resstr = str_replace(&#39;ITEM&#39;, $item, $resstr);
    echo $resstr;
  }

  /**
   * url解析
   * @param type $url
   * @return string
   */
  public static function complateUrl($url) {
    if (false === stristr($url, "http://")) {//查找http:// 如果不存在
      if (0 === strpos($url, &#39;/&#39;)) {//查找首字母 如果存在
        $url = substr($url, 1); //去除/
      }
      $url = "http://" . $_SERVER[&#39;HTTP_HOST&#39;] . &#39;/&#39; . $url; //拼接完整路徑
    }
    return $url;
  }

  /**
   * 微信url固定參數(shù)替換
   * @param type $url
   * @param type $postObj
   * @return type
   */
  public static function replaceToWXUrl($url, $postObj) {
    global $wechatid;
    return str_ireplace(&#39;fromUsername&#39;, $wechatid, $url);
  }

  /**
   * 完成以下行為
   * parseUrlParam
   * setUrlPublicParam
   * buildUrlParam
   * @param type $url
   * @param type $other 附加參數(shù) 基礎(chǔ)參數(shù)wid wxid wechatid
   */
  public static function parseUrl($url, $other = array()) {
    $url = self::parseUrlParam($url);
    $url = self::setUrlPublicParam($url);
    if (is_array($other) && !empty($other)) {
      $url[&#39;param&#39;] = array_merge($url[&#39;param&#39;], $other);
    }
    $url = self::buildUrlParam($url);
    return $url;
  }

  /**
   * 分析url
   * @param type $url
   * @return type
   */
  public static function parseUrlParam($url) {
    $temp = explode("?", $url);
    $url0 = $temp[0];
    $url1 = $temp[1];
    $p = explode("&", $url1);
    $param = array();
    foreach ($p as $v) {
      $tempp = explode("=", $v);
      $param[$tempp[0]] = $tempp[1];
    }
    return array("url" => $url0, "param" => $param);
  }

  /**
   * 組裝url
   * @param type $arr
   * @return type
   */
  public static function buildUrlParam($arr) {
    $url = $arr[&#39;url&#39;];
    $param = $arr[&#39;param&#39;];
    $param_arr = array();
    foreach ($param as $k => $v) {
      if ($k == "") continue;
      $param_arr[] = $k . "=" . $v;
    }
    return $url . "?" . implode("&", $param_arr) . "#mp.weixin.qq.com";
  }

  /**
   * 設(shè)置微信保留參數(shù)信息
   * @global type $uid
   * @global type $wid
   * @global type $wechatid
   * @param type $url
   * @return type
   */
  public static function setUrlPublicParam($url) {
    global $uid, $wid, $wechatid;
    $url[&#39;param&#39;][&#39;wid&#39;] = $wid;
    $url[&#39;param&#39;][&#39;wxid&#39;] = $wechatid;
    $url[&#39;param&#39;][&#39;wechatid&#39;] = $wechatid;
    return $url;
  }

  public static $errorno = array(
    &#39;-1&#39; => &#39;系統(tǒng)繁忙&#39;,
    &#39;0&#39; => &#39;請(qǐng)求成功&#39;,
    &#39;40001&#39; => &#39;獲取access_token時(shí)AppSecret錯(cuò)誤,或者access_token無效&#39;,
    &#39;40002&#39; => &#39;不合法的憑證類型&#39;,
    &#39;40003&#39; => &#39;不合法的OpenID&#39;,
    &#39;40004&#39; => &#39;不合法的媒體文件類型&#39;,
    &#39;40005&#39; => &#39;不合法的文件類型&#39;,
    &#39;40006&#39; => &#39;不合法的文件大小&#39;,
    &#39;40007&#39; => &#39;不合法的媒體文件id&#39;,
    &#39;40008&#39; => &#39;不合法的消息類型&#39;,
    &#39;40009&#39; => &#39;不合法的圖片文件大小&#39;,
    &#39;40010&#39; => &#39;不合法的語音文件大小&#39;,
    &#39;40011&#39; => &#39;不合法的視頻文件大小&#39;,
    &#39;40012&#39; => &#39;不合法的縮略圖文件大小&#39;,
    &#39;40013&#39; => &#39;不合法的APPID&#39;,
    &#39;40014&#39; => &#39;不合法的access_token&#39;,
    &#39;40015&#39; => &#39;不合法的菜單類型&#39;,
    &#39;40016&#39; => &#39;不合法的按鈕個(gè)數(shù)&#39;,
    &#39;40017&#39; => &#39;不合法的按鈕個(gè)數(shù)&#39;,
    &#39;40018&#39; => &#39;不合法的按鈕名字長(zhǎng)度&#39;,
    &#39;40019&#39; => &#39;不合法的按鈕KEY長(zhǎng)度&#39;,
    &#39;40020&#39; => &#39;不合法的按鈕URL長(zhǎng)度&#39;,
    &#39;40021&#39; => &#39;不合法的菜單版本號(hào)&#39;,
    &#39;40022&#39; => &#39;不合法的子菜單級(jí)數(shù)&#39;,
    &#39;40023&#39; => &#39;不合法的子菜單按鈕個(gè)數(shù)&#39;,
    &#39;40024&#39; => &#39;不合法的子菜單按鈕類型&#39;,
    &#39;40025&#39; => &#39;不合法的子菜單按鈕名字長(zhǎng)度&#39;,
    &#39;40026&#39; => &#39;不合法的子菜單按鈕KEY長(zhǎng)度&#39;,
    &#39;40027&#39; => &#39;不合法的子菜單按鈕URL長(zhǎng)度&#39;,
    &#39;40028&#39; => &#39;不合法的自定義菜單使用用戶&#39;,
    &#39;40029&#39; => &#39;不合法的oauth_code&#39;,
    &#39;40030&#39; => &#39;不合法的refresh_token&#39;,
    &#39;40031&#39; => &#39;不合法的openid列表&#39;,
    &#39;40032&#39; => &#39;不合法的openid列表長(zhǎng)度&#39;,
    &#39;40033&#39; => &#39;不合法的請(qǐng)求字符,不能包含\uxxxx格式的字符&#39;,
    &#39;40035&#39; => &#39;不合法的參數(shù)&#39;,
    &#39;40038&#39; => &#39;不合法的請(qǐng)求格式&#39;,
    &#39;40039&#39; => &#39;不合法的URL長(zhǎng)度&#39;,
    &#39;40050&#39; => &#39;不合法的分組id&#39;,
    &#39;40051&#39; => &#39;分組名字不合法&#39;,
    &#39;40059&#39; => &#39;不合法的消息id&#39;,
    &#39;41001&#39; => &#39;缺少access_token參數(shù)&#39;,
    &#39;41002&#39; => &#39;缺少appid參數(shù)&#39;,
    &#39;41003&#39; => &#39;缺少refresh_token參數(shù)&#39;,
    &#39;41004&#39; => &#39;缺少secret參數(shù)&#39;,
    &#39;41005&#39; => &#39;缺少多媒體文件數(shù)據(jù)&#39;,
    &#39;41006&#39; => &#39;缺少media_id參數(shù)&#39;,
    &#39;41007&#39; => &#39;缺少子菜單數(shù)據(jù)&#39;,
    &#39;41008&#39; => &#39;缺少oauth code&#39;,
    &#39;41009&#39; => &#39;缺少openid&#39;,
    &#39;42001&#39; => &#39;access_token超時(shí)&#39;,
    &#39;42002&#39; => &#39;refresh_token超時(shí)&#39;,
    &#39;42003&#39; => &#39;oauth_code超時(shí)&#39;,
    &#39;43001&#39; => &#39;需要GET請(qǐng)求&#39;,
    &#39;43002&#39; => &#39;需要POST請(qǐng)求&#39;,
    &#39;43003&#39; => &#39;需要HTTPS請(qǐng)求&#39;,
    &#39;43004&#39; => &#39;需要接收者關(guān)注&#39;,
    &#39;43005&#39; => &#39;需要好友關(guān)系&#39;,
    &#39;44001&#39; => &#39;多媒體文件為空&#39;,
    &#39;44002&#39; => &#39;POST的數(shù)據(jù)包為空&#39;,
    &#39;44003&#39; => &#39;圖文消息內(nèi)容為空&#39;,
    &#39;44004&#39; => &#39;文本消息內(nèi)容為空&#39;,
    &#39;45001&#39; => &#39;多媒體文件大小超過限制&#39;,
    &#39;45002&#39; => &#39;消息內(nèi)容超過限制&#39;,
    &#39;45003&#39; => &#39;標(biāo)題字段超過限制&#39;,
    &#39;45004&#39; => &#39;描述字段超過限制&#39;,
    &#39;45005&#39; => &#39;鏈接字段超過限制&#39;,
    &#39;45006&#39; => &#39;圖片鏈接字段超過限制&#39;,
    &#39;45007&#39; => &#39;語音播放時(shí)間超過限制&#39;,
    &#39;45008&#39; => &#39;圖文消息超過限制&#39;,
    &#39;45009&#39; => &#39;接口調(diào)用超過限制&#39;,
    &#39;45010&#39; => &#39;創(chuàng)建菜單個(gè)數(shù)超過限制&#39;,
    &#39;45015&#39; => &#39;回復(fù)時(shí)間超過限制&#39;,
    &#39;45016&#39; => &#39;系統(tǒng)分組,不允許修改&#39;,
    &#39;45017&#39; => &#39;分組名字過長(zhǎng)&#39;,
    &#39;45018&#39; => &#39;分組數(shù)量超過上限&#39;,
    &#39;46001&#39; => &#39;不存在媒體數(shù)據(jù)&#39;,
    &#39;46002&#39; => &#39;不存在的菜單版本&#39;,
    &#39;46003&#39; => &#39;不存在的菜單數(shù)據(jù)&#39;,
    &#39;46004&#39; => &#39;不存在的用戶&#39;,
    &#39;47001&#39; => &#39;解析JSON/XML內(nèi)容錯(cuò)誤&#39;,
    &#39;48001&#39; => &#39;api功能未授權(quán)&#39;,
    &#39;50001&#39; => &#39;用戶未授權(quán)該api&#39;
  );

  /**
   * GET 請(qǐng)求
   * @param string $url
   */
  public static function http_get($url) {
    if (!function_exists(&#39;curl_init&#39;)) {
      die(&#39;curl 未開啟&#39;);
    };
    $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_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    if (intval($aStatus["http_code"]) == 200) {
      return $sContent;
    } else {
      return false;
    }
  }

  /**
   * POST 請(qǐng)求
   * @param string $url
   * @param array $param
   * @return string content
   */
  public static function http_post($url, $param, $httpBuildQuery = false) {
    if (!function_exists(&#39;curl_init&#39;)) {
      die(&#39;curl 未開啟&#39;);
    };
    $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_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
//    curl_setopt($oCurl, CURLOPT_HTTPHEADER, $header);//設(shè)置HTTP頭
    curl_setopt($oCurl, CURLOPT_POST, 1);
    if ($httpBuildQuery) {
      $param = http_build_query($param);
    }
    curl_setopt($oCurl, CURLOPT_POSTFIELDS, $param);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    if (intval($aStatus["http_code"]) == 200) {
      return $sContent;
    } else {
      return false;
    }
  }

  /**
   * 微信api不支持中文轉(zhuǎn)義的json結(jié)構(gòu)
   * @param array $arr
   */
  static function json_encode($arr) {
    $parts = array();
    $is_list = false;
    //Find out if the given array is a numerical array
    $keys = array_keys($arr);
    $max_length = count($arr) - 1;
    if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1
      $is_list = true;
      for ($i = 0; $i < count($keys); $i ++) { //See if each key correspondes to its position
        if ($i != $keys [$i]) { //A key fails at position check.
          $is_list = false; //It is an associative array.
          break;
        }
      }
    }
    foreach ($arr as $key => $value) {
      if (is_array($value)) { //Custom handling for arrays
        if ($is_list) $parts [] = self::json_encode($value); /* :RECURSION: */
        else $parts [] = &#39;"&#39; . $key . &#39;":&#39; . self::json_encode($value); /* :RECURSION: */
      } else {
        $str = &#39;&#39;;
        if (!$is_list) $str = &#39;"&#39; . $key . &#39;":&#39;;
        //Custom handling for multiple data types
        if (is_numeric($value) && $value < 2000000000) $str .= $value; //Numbers
        elseif ($value === false) $str .= &#39;false&#39;; //The booleans
        elseif ($value === true) $str .= &#39;true&#39;;
        else $str .= &#39;"&#39; . addslashes($value) . &#39;"&#39;;
//All other things
// :TODO: Is there any more datatype we should be in the lookout for? (Object?)
        $parts [] = $str;
      }
    }
    $json = implode(&#39;,&#39;, $parts);
    if ($is_list) return &#39;[&#39; . $json . &#39;]&#39;; //Return numerical JSON
    return &#39;{&#39; . $json . &#39;}&#39;; //Return associative JSON
  }

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)php程式設(shè)計(jì)有所幫助。

更多php微信高階介面群發(fā) 多客服相關(guān)文章請(qǐng)關(guān)注PHP中文網(wǎng)!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願(yuàn)投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請(qǐng)聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)程式碼編輯軟體(SublimeText3)