예제 #1
0
파일: Http.php 프로젝트: ably/ably-php
 /**
  * Executes a cURL request
  * @param string $method HTTP method (GET, POST, PUT, DELETE, ...)
  * @param string $url Absolute URL to make a request on
  * @param array $headers HTTP headers to send
  * @param array|string $params Array of parameters to submit or a JSON string
  * @throws AblyRequestException if the request fails
  * @throws AblyRequestTimeoutException if the request times out
  * @return array with 'headers' and 'body' fields, body is automatically decoded
  */
 public function request($method, $url, $headers = [], $params = [])
 {
     $ch = $this->curl->init($url);
     $this->curl->setOpt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeout);
     $this->curl->setOpt($ch, CURLOPT_TIMEOUT_MS, $this->requestTimeout);
     if (!empty($params)) {
         if (is_array($params)) {
             $paramsQuery = http_build_query($params);
             if ($method == 'GET') {
                 if ($paramsQuery) {
                     $url .= '?' . $paramsQuery;
                 }
                 $this->curl->setOpt($ch, CURLOPT_URL, $url);
             } else {
                 if ($method == 'POST') {
                     $this->curl->setOpt($ch, CURLOPT_POST, true);
                     $this->curl->setOpt($ch, CURLOPT_POSTFIELDS, $paramsQuery);
                 } else {
                     $this->curl->setOpt($ch, CURLOPT_CUSTOMREQUEST, $method);
                     $this->curl->setOpt($ch, CURLOPT_POSTFIELDS, $paramsQuery);
                 }
             }
         } else {
             if (is_string($params)) {
                 // json or msgpack
                 if ($method == 'GET') {
                 } else {
                     if ($method == 'POST') {
                         $this->curl->setOpt($ch, CURLOPT_POST, true);
                     } else {
                         $this->curl->setOpt($ch, CURLOPT_CUSTOMREQUEST, $method);
                     }
                 }
                 $this->curl->setOpt($ch, CURLOPT_POSTFIELDS, $params);
                 if ($this->postDataFormat == 'json') {
                     array_push($headers, 'Content-Type: application/json');
                 }
             } else {
                 throw new AblyRequestException('Unknown $params format');
             }
         }
     }
     if (!empty($headers)) {
         $this->curl->setOpt($ch, CURLOPT_HTTPHEADER, $headers);
     }
     $this->curl->setOpt($ch, CURLOPT_RETURNTRANSFER, true);
     if (Log::getLogLevel() >= Log::VERBOSE) {
         $this->curl->setOpt($ch, CURLOPT_VERBOSE, true);
     }
     $this->curl->setOpt($ch, CURLOPT_HEADER, true);
     // return response headers
     Log::d('cURL command:', $this->curl->getCommand($ch));
     $raw = $this->curl->exec($ch);
     $info = $this->curl->getInfo($ch);
     $err = $this->curl->getErrNo($ch);
     $errmsg = $err ? $this->curl->getError($ch) : '';
     $this->curl->close($ch);
     if ($err) {
         // a connection error has occured (no data received)
         Log::e('cURL error:', $err, $errmsg);
         throw new AblyRequestException('cURL error: ' . $errmsg, 50003, 500);
     }
     $resHeaders = substr($raw, 0, $info['header_size']);
     $body = substr($raw, $info['header_size']);
     $decodedBody = json_decode($body);
     $response = ['headers' => $resHeaders, 'body' => $decodedBody ? $decodedBody : $body];
     Log::v('cURL request response:', $info['http_code'], $response);
     if ($info['http_code'] < 200 || $info['http_code'] >= 300) {
         $ablyCode = empty($decodedBody->error->code) ? $info['http_code'] * 100 : $decodedBody->error->code * 1;
         $errorMessage = empty($decodedBody->error->message) ? 'cURL request failed' : $decodedBody->error->message;
         throw new AblyRequestException($errorMessage, $ablyCode, $info['http_code'], $response);
     }
     return $response;
 }
예제 #2
0
파일: HttpTest.php 프로젝트: ably/ably-php
 public function init($url = null)
 {
     $this->lastParams = [CURLOPT_URL => $url];
     return parent::init($url);
 }