/** * 获取IP列表 * @return array 对应ip地址数组(请求失败返回false) * @throws ApiException */ public function get() { $httpClient = new HttpClient($this->url); $httpClient->get(); $data = $httpClient->jsonToArray(); if (isset($data['ip_list'])) { return $data['ip_list']; } else { throw ApiException::throws(ApiException::ERROR_JSON_ERROR_CODE, 'response: ' . $httpClient->getResponse()); } //never return false; }
/** * 上传一个素材 * @param string $mediaFile 完整(绝对路径)文件路径 * @param string $type 可选,上传媒体文件的类型(image、voice、video、thumb),不填则根据后缀名判断 * @return string mediaId媒体id(请求失败返回false) * @throws ApiException */ public function upload($mediaFile, $type = null) { if (!file_exists($mediaFile)) { throw ApiException::throws(ApiException::FILE_NOT_EXISTS_ERROR_CODE, "file: {$mediaFile}"); return false; } if ($type == null) { $type = $this->parseMediaType($mediaFile); } $url = $this->url . '/upload?access_token=' . $this->accessToken . '&type=' . $type; $httpClient = new HttpClient($url); $httpClient->upload(array('media' => $mediaFile)); $result = $httpClient->jsonToArray(); if (isset($result['media_id'])) { return $result['media_id']; } else { throw ApiException::throws(ApiException::ERROR_JSON_ERROR_CODE, 'response: ' . $httpClient->getResponse()); } //never return false; }
private function reloadTicket() { $fp = fopen($this->file, "w"); if (flock($fp, LOCK_EX)) { Log::i('Lock', 'JsapiTicket'); try { $url = $this->url . "?access_token={$this->accessToken}&type=wx_card"; $httpClient = new HttpClient($url); $httpClient->get(); $stream = $httpClient->jsonToArray(); if (isset($stream['ticket'])) { //提取参数 $jsapi_ticket = $stream['ticket']; $expires_in = $stream['expires_in']; $expires_time = intval(time()) + intval($expires_in) - 60; //60s超时缓冲 $file_stream = json_encode(array('expires_time' => $expires_time, 'jsapi_ticket' => $jsapi_ticket)); fwrite($fp, "<?php exit; ?>\n"); fwrite($fp, $file_stream); Log::i('Unlock', 'JsapiTicket'); flock($fp, LOCK_UN); fclose($fp); @chmod($this->file, DEFAULT_PERMISSION); return $jsapi_ticket; } else { throw ApiException::throws(ApiException::ERROR_JSON_ERROR_CODE, 'response: ' . $httpClient->getResponse()); } } catch (ApiException $e) { Log::i('Unlock', 'JsapiTicket'); flock($fp, LOCK_UN); fclose($fp); throw $e; return false; } } else { fclose($fp); throw ApiException::throws(ApiException::FILE_LOCK_ERROR_CODE); return false; } }
/** * 设置用户备注名 * @param string $openId 用户openid * @param string $remark 备注名,小于30字符 * @return boolean 设置成功返回true * @throws ApiException */ public function setRemark($openId, $remark) { $url = $this->url . '/info/updateremark?access_token=' . $this->accessToken; $json = array('openid' => $openId, 'remark' => urlencode($remark)); $httpClient = new HttpClient($url); $httpClient->setBody(urldecode(json_encode($json))); $httpClient->post(); $result = $httpClient->jsonToArray(); if (isset($result['errcode']) && $result['errcode'] == 0) { return true; } else { throw ApiException::throws(ApiException::ERROR_JSON_ERROR_CODE, 'response: ' . $httpClient->getResponse()); } }
/** * 移动用户分组 * @param string $openidList 用户openid * @param string $toGroupId 目标分组id * @return boolean true表示成功 * @throws ApiException */ public function moveUser($openidList, $toGroupId) { $json = array('openid' => $openidList, 'to_groupid' => intval($toGroupId)); $httpClient = new HttpClient($this->url . '/members/update?access_token=' . $this->accessToken); $httpClient->setBody(json_encode($json)); $httpClient->post(); $result = $httpClient->jsonToArray(); if (isset($result['errcode']) && $result['errcode'] == 0) { //errcode!=0已在checkErrcode检验 return true; } else { throw ApiException::throws(ApiException::ERROR_JSON_ERROR_CODE, 'response: ' . $httpClient->getResponse()); } return false; }
/** * 测试个性化菜单 * @param string $userId 用户openId或微信号 * @param boolean $assocArray 可选,false则直接返回API的结果(默认true返回解析后的数组) * @return string/array 查询后的结果 */ public function test($userId, $assocArray = true) { $url = $this->url . '/trymatch?access_token=' . $this->accessToken; $httpClient = new HttpClient($url); $httpClient->setBody(json_encode(array('user_id' => $userId))); $httpClient->post(); if ($httpClient->getStatus() != 200 || $httpClient->getResponse() == '') { throw ApiException::throws(ApiException::HTTP_ERROR_CODE, 'status code: ' . $httpClient->getStatus()); return false; } if (!$assocArray) { //直接返回API接口的结果 return $httpClient->getResponse(); } $result = $httpClient->jsonToArray(); return $result; }
private function sendToTarget($jsonArr) { $url = $this->url . $this->accessToken; $httpClient = new HttpClient($url); $httpClient->setBody(urldecode(json_encode($jsonArr))); $httpClient->post(); $result = $httpClient->jsonToArray(); if (isset($result['errcode']) && $result['errcode'] == 0) { return true; } else { throw ApiException::throws(ApiException::ERROR_JSON_ERROR_CODE, 'response: ' . $httpClient->getResponse()); } //never return false; }
/** * 获取在线客服接待信息 * @return array 客服接待信息集合(失败时返回false) * @throws ApiException */ public function getOnlineList() { $url = 'https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token=' . $this->accessToken; $httpClient = new HttpClient($url); $httpClient->get(); $result = $httpClient->jsonToArray(); if (isset($result['kf_online_list'])) { return $result['kf_online_list']; } else { throw ApiException::throws(ApiException::ERROR_JSON_ERROR_CODE, 'response: ' . $httpClient->getResponse()); } //never return false; }