/** * 发起一个HTTP/HTTPS的请求 * * @param string $url 接口的URL * @param string $method 请求类型 GET | POST * @param array $params 接口参数 * @param array $options 其它选项 * * @return array | boolean */ public function request($url, $method = self::GET, $params = array(), $options = array()) { if ($this->token) { $url .= (stripos($url, '?') ? '&' : '?') . 'access_token=' . $this->token; } $method = strtoupper($method); if ($this->json) { $options['json'] = true; } $response = parent::request($url, $method, $params, $options); $this->json = false; if (empty($response['data'])) { throw new Exception('服务器无响应'); } $contents = JSON::decode($response['data'], true); // while the response is an invalid JSON structure, returned the source data if (JSON_ERROR_NONE !== json_last_error()) { return $response['data']; } if (isset($contents['errcode']) && 0 !== $contents['errcode']) { if (empty($contents['errmsg'])) { $contents['errmsg'] = 'Unknown'; } throw new Exception("[{$contents['errcode']}] " . $contents['errmsg'], $contents['errcode']); } if ($contents === array('errcode' => '0', 'errmsg' => 'ok')) { return true; } return $contents; }
/** * 获取JSSDK的配置数组 * * @param array $APIs * @param bool $debug * @param bool $json * * @return string|array */ public function config(array $APIs, $debug = false, $beta = false, $json = true) { $signPackage = $this->getSignaturePackage(); $base = array('debug' => $debug, 'beta' => $beta); $config = array_merge($base, $signPackage, array('jsApiList' => $APIs)); return $json ? JSON::encode($config) : $config; }
/** * 上传视频 * * 有点不一样。。。 * * @param string $path * @param string $title * @param string $description * * @return string */ public function video($path, $title, $description) { $params = array('description' => JSON::encode(array('title' => $title, 'introduction' => $description))); return $this->upload('video', $path, $params); }
/** * 生成 js添加到卡包 需要的 card_list 项 * * @param string $cardId * @param array $extension * * @return string */ public function attachExtension($cardId, array $extension = array()) { $timestamp = time(); $ext = array('code' => Arr::get($extension, 'code'), 'openid' => Arr::get($extension, 'openid', Arr::get($extension, 'open_id')), 'timestamp' => $timestamp, 'outer_id' => Arr::get($extension, 'outer_id'), 'balance' => Arr::get($extension, 'balance')); $ext['signature'] = $this->getSignature($this->getTicket(), $timestamp, $cardId, $ext['code'], $ext['openid'], $ext['balance']); return array('card_id' => $cardId, 'card_ext' => JSON::encode($ext)); }
/** * 获取配置文件(用于 Jssdk chooseWXPay 方式) * * @param bool|true $asJson * * @return array|string */ public function getConfigJssdk($asJson = true) { $config = $this->generateConfig(); $params = array('timestamp' => $config['timeStamp'], 'nonceStr' => $config['nonceStr'], 'package' => $config['package'], 'signType' => $config['signType'], 'paySign' => $config['paySign']); return $asJson ? JSON::encode($params) : $params; }
/** * 返回json. * * @return string */ public function toJson() { return JSON::encode($this->all()); }
/** * Make a HTTP request * * @param string $url * @param string $method * @param array $params * @param array $options * * @return array */ protected function request($url, $method = self::GET, $params = array(), $options = array()) { if ($method === self::GET || $method === self::DELETE) { $url .= (stripos($url, '?') ? '&' : '?') . http_build_query($params); $params = array(); } curl_setopt($this->curl, CURLOPT_HEADER, 1); curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($this->curl, CURLOPT_URL, $url); curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); // Check for files if (isset($options['files']) && count($options['files'])) { foreach ($options['files'] as $index => $file) { $params[$index] = $this->createCurlFile($file); } version_compare(PHP_VERSION, '5.5', '<') || curl_setopt($this->curl, CURLOPT_SAFE_UPLOAD, false); curl_setopt($this->curl, CURLOPT_POST, 1); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params); } else { if (isset($options['json'])) { $params = JSON::encode($params); $options['headers'][] = 'content-type:application/json'; } curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params); } // Check for custom headers if (isset($options['headers']) && count($options['headers'])) { curl_setopt($this->curl, CURLOPT_HTTPHEADER, $options['headers']); } // Check for basic auth if (isset($options['auth']['type']) && 'basic' === $options['auth']['type']) { curl_setopt($this->curl, CURLOPT_USERPWD, $options['auth']['username'] . ':' . $options['auth']['password']); } $response = $this->doCurl(); // Separate headers and body $headerSize = $response['curl_info']['header_size']; $header = substr($response['response'], 0, $headerSize); $body = substr($response['response'], $headerSize); $results = array('curl_info' => $response['curl_info'], 'content_type' => $response['curl_info']['content_type'], 'status' => $response['curl_info']['http_code'], 'headers' => $this->splitHeaders($header), 'data' => $body); return $results; }