/** * 获取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; }
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 $mediaId 媒体id,通过上传获得 * @param string $toFile 可选,下载到文件的绝对路径,不填则默认路径为RES_ROOT/$mediaId * @return integer 下载到的文件大小(失败时返回false) * @throws ApiException */ public function download($mediaId, $toFile = null) { //使用http协议 // $this->url = str_replace('https', 'http', $this->url); $url = $this->url . '/get?access_token=' . $this->accessToken . '&media_id=' . $mediaId; $httpClient = new HttpClient($url); $httpClient->get(30); if ($httpClient->getStatus() != 200 || $httpClient->getResponse() == '') { throw ApiException::throws(ApiException::HTTP_ERROR_CODE, 'status code: ' . $httpClient->getStatus()); return false; } $header = $httpClient->getHeader(); $contentType = $header['Content-Type'][0]; //Content-Type为text/时表示带有errcode错误信息 if (!isset($header['Content-Type']) || stripos($contentType, 'text') !== false) { $result = json_decode($httpClient->getResponse(), true); if (!$result) { throw ApiException::throws(ApiException::JSON_DECODE_ERROR_CODE, 'response: ' . $httpClient->getResponse()); } if (isset($result['errcode']) && $result['errcode'] != 0) { throw new ApiException($result['errmsg'], $result['errcode']); //非0状态码 } return false; } //写入文件 if (empty($toFile)) { if (!is_dir(RES_ROOT . '/media')) { mkdir(RES_ROOT . '/media'); chmod(RES_ROOT . '/media', 0775); } $toFile = RES_ROOT . "/media/{$mediaId}" . $this->parseExtension($contentType); } file_put_contents($toFile, $httpClient->getResponse()); return filesize($toFile); }
/** * 设置用户备注名 * @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; }