Example #1
0
 /**
  * @param ApiCall $client
  * @param IPub\OAuth\Api\Response $response
  */
 public function __construct(ApiCall $client, IPub\OAuth\Api\Response $response)
 {
     $this->client = $client;
     $this->httpClient = $client->getHttpClient();
     $resource = $response->toArray();
     $params = $response->request->getParameters();
     $this->perPage = isset($params['count']) ? (int) $params['count'] : count($resource);
     $this->responses[$this->firstPage] = $response;
     $this->resources[$this->firstPage] = $resource;
 }
Example #2
0
 /**
  * Makes an HTTP request. This method can be overridden by subclasses if
  * developers want to do fancier things or use something other than curl to
  * make the request.
  *
  * @param Request $request
  * @param string $signatureMethodName
  *
  * @return Response
  *
  * @throws Exceptions\ApiException
  * @throws Exceptions\InvalidArgumentException
  */
 public function makeRequest(Request $request, $signatureMethodName = 'PLAINTEXT')
 {
     if (!($signatureMethod = $this->getSignatureMethod($signatureMethodName))) {
         throw new Exceptions\InvalidArgumentException("Signature method '{$signatureMethodName}' was not found. Please provide valid signature method name");
     }
     if (isset($this->memoryCache[$cacheKey = md5(serialize($request))])) {
         return $this->memoryCache[$cacheKey];
     }
     // Sign request with selected method
     $request->signRequest($signatureMethod);
     $ch = $this->buildCurlResource($request);
     $result = curl_exec($ch);
     // provide certificate if needed
     if (curl_errno($ch) == CURLE_SSL_CACERT || curl_errno($ch) === CURLE_SSL_CACERT_BADFILE) {
         Debugger::log('Invalid or no certificate authority found, using bundled information', 'oauth');
         $this->curlOptions[CURLOPT_CAINFO] = CurlCaBundle\CertificateHelper::getCaInfoFile();
         curl_setopt($ch, CURLOPT_CAINFO, CurlCaBundle\CertificateHelper::getCaInfoFile());
         $result = curl_exec($ch);
     }
     // With dual stacked DNS responses, it's possible for a server to
     // have IPv6 enabled but not have IPv6 connectivity.  If this is
     // the case, curl will try IPv4 first and if that fails, then it will
     // fall back to IPv6 and the error EHOSTUNREACH is returned by the operating system.
     if ($result === FALSE && (!isset($this->curlOptions[CURLOPT_IPRESOLVE]) || empty($this->curlOptions[CURLOPT_IPRESOLVE]))) {
         $matches = [];
         if (preg_match('/Failed to connect to ([^:].*): Network is unreachable/', curl_error($ch), $matches)) {
             if (strlen(@inet_pton($matches[1])) === 16) {
                 Debugger::log('Invalid IPv6 configuration on server, Please disable or get native IPv6 on your server.', 'oauth');
                 $this->curlOptions[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4;
                 curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
                 $result = curl_exec($ch);
             }
         }
     }
     $info = $this->getRequestInfo($ch, $request, $result);
     if (isset($info['request_header'])) {
         $request->setHeaders($info['request_header']);
     }
     $response = new Response($request, substr($result, $info['header_size']), $info['http_code'], end($info['headers']), $info);
     if (!$response->isOk()) {
         $e = $response->toException();
         curl_close($ch);
         $this->onError($e, $response);
         throw $e;
     }
     $this->onSuccess($response);
     curl_close($ch);
     return $this->memoryCache[$cacheKey] = $response;
 }
Example #3
0
 /**
  * @param Api\Response $response
  * @param \Exception $exception
  */
 private function processEvent(Api\Response $response, \Exception $exception = NULL)
 {
     if (!isset($this->calls[$oid = spl_object_hash($response->getRequest())])) {
         return;
     }
     $debugInfo = $response->debugInfo;
     $current = $this->calls[$oid];
     $this->totalTime += $current->time = $debugInfo['total_time'];
     unset($debugInfo['total_time']);
     $current->info = $debugInfo;
     $current->info['method'] = $response->getRequest()->getMethod();
     if ($exception) {
         $current->exception = $exception;
     } else {
         $current->result = $response->toArray() ?: $response->getContent();
     }
 }