/** * 验证返回的数据是否为对象,根据传入的方式判断返回的状态码 * @param $response * @param $method * @param string $type 'create' * @throws ResponseException * @return boolean */ public function validateResponse($response, $method, $type = '') { $state_code = $type == 'create' ? '201' : '200'; if ($type == 'delete') { $response = new stdClass(); } if (!is_object($response) || $this->client->getDebug()->lastResponseCode != $state_code) { throw new ResponseException($this, $method); } return $type == 'delete' ? 'success' : $response; }
/** * Use the send method to call every endpoint except for oauth/tokens * * @param Client $client * @param string $endPoint * @param array $json * @param string $method * @param string $contentType * * @throws \Exception * * @return mixed */ public static function send(Client $client, $endPoint, $json = array(), $method = 'GET', $contentType = 'application/json') { $url = $client->getApiUrl() . $endPoint; $method = strtoupper($method); $curl = isset(self::$curl) ? self::$curl : new CurlRequest(); $curl->setopt(CURLOPT_URL, $url); if ($method === 'POST') { $curl->setopt(CURLOPT_POST, true); } else { if ($method === 'PUT') { $curl->setopt(CURLOPT_CUSTOMREQUEST, 'PUT'); } else { $st = http_build_query((array) $json); $curl->setopt(CURLOPT_URL, $url . ($st !== array() ? (strpos($url, '?') === false ? '?' : '&') . $st : '')); $curl->setopt(CURLOPT_CUSTOMREQUEST, $method); } } $httpHeader = array('Accept: application/json'); if ($client->getAuthType() == 'oauth_token') { $httpHeader[] = 'Authorization: Bearer ' . $client->getAuthText(); } else { $curl->setopt(CURLOPT_USERPWD, $client->getAuthText()); } /* DO NOT SET CONTENT TYPE IF UPLOADING */ if (!isset($json['uploaded_data'])) { if (isset($json['filename'])) { $filename = $json['filename']; $file = fopen($filename, 'r'); $size = filesize($filename); $fileData = fread($file, $size); $json = $fileData; $curl->setopt(CURLOPT_INFILE, $file); $curl->setopt(CURLOPT_INFILESIZE, $size); } else { if (isset($json['body'])) { $curl->setopt(CURLOPT_INFILESIZE, strlen($json['body'])); $json = $json['body']; } } $httpHeader[] = 'Content-Type: ' . $contentType; } else { $contentType = ''; } if ($contentType === 'application/json') { $json = json_encode($json); } $curl->setopt(CURLOPT_POSTFIELDS, $json); $curl->setopt(CURLOPT_HTTPHEADER, $httpHeader); $curl->setopt(CURLINFO_HEADER_OUT, true); $curl->setopt(CURLOPT_RETURNTRANSFER, true); $curl->setopt(CURLOPT_CONNECTTIMEOUT, 30); $curl->setopt(CURLOPT_TIMEOUT, 30); $curl->setopt(CURLOPT_SSL_VERIFYPEER, false); $curl->setopt(CURLOPT_HEADER, true); $curl->setopt(CURLOPT_VERBOSE, true); $curl->setopt(CURLOPT_FOLLOWLOCATION, true); $curl->setopt(CURLOPT_MAXREDIRS, 3); $response = $curl->exec(); if ($response === false) { throw new \Exception(sprintf('Curl error message: "%s" in %s', $curl->error(), __METHOD__)); } $headerSize = $curl->getinfo(CURLINFO_HEADER_SIZE); $responseBody = substr($response, $headerSize); $responseObject = json_decode($responseBody); $client->setDebug($curl->getinfo(CURLINFO_HEADER_OUT), $curl->getinfo(CURLINFO_HTTP_CODE), substr($response, 0, $headerSize), isset($responseObject->error) ? $responseObject : null); $responseCode = $client->getDebug()->lastResponseCode; if ($responseCode >= 400) { print $client->getDebug(); throw new ResponseException(__METHOD__); } $curl->close(); self::$curl = null; return $responseObject; }