Ejemplo n.º 1
0
 /**
  * @param string $level
  * @param RequestInterface $request
  * @param array $context
  */
 public function logRequest($level, RequestInterface $request, array $context = array())
 {
     $msg = "[FacebookAPI] [Response] \n";
     $msg = trim($request->getMethod() . ' ' . $request->getUrl());
     foreach ($request->getHeaders() as $name => $value) {
         $msg .= "\r\n{$name}: " . $value;
     }
     $message = "{$msg}\r\n\r\n" . implode("\n", $request->getBodyParams()->export());
     $this->logger->log($level, $message, $context);
 }
 /**
  * @param int $num_pages
  * @param RequestInterface|null $prev
  * @return Mock|ResponseInterface
  */
 protected function createResponseChainMock($num_pages, RequestInterface $prev = null)
 {
     $query_params = $prev ? clone $prev->getQueryParams() : new Parameters();
     $sample_content = $this->createSampleResponseContent();
     $request = $this->createRequestMock();
     $request->method('getMethod')->willReturn(RequestInterface::METHOD_GET);
     $request->method('getQueryParams')->willReturn($query_params);
     $response = $this->createResponseMock();
     $response->method('getRequest')->willReturn($request);
     $request->method('execute')->willReturn($response);
     $callback = function () use($num_pages, $sample_content) {
         return $num_pages > 0 ? $sample_content : $this->createEmptyResponseContent();
     };
     $clone_callback = function () use($num_pages, $request) {
         return $this->createResponseChainMock($num_pages - 1, $request)->getRequest();
     };
     $url_callback = function () use($query_params) {
         return $this->createUnparameterizedUrl() . '?' . http_build_query($query_params->getArrayCopy());
     };
     $response->method('getContent')->willReturnCallback($callback);
     $request->method('createClone')->willReturnCallback($clone_callback);
     $request->method('getUrl')->willReturnCallback($url_callback);
     return $response;
 }
 /**
  * @param string $level
  * @param RequestInterface $request
  * @param array $context
  */
 public function logRequest($level, RequestInterface $request, array $context = array())
 {
     $new_line = ' \\' . PHP_EOL . '  ';
     $method_flag = static::getMethodFlag($request->getMethod());
     $param_flag = static::getParamFlag($request->getMethod());
     $params = array_merge($this->processParams($request->getQueryParams(), $param_flag, false), $this->processParams($request->getBodyParams(), $param_flag, false), $this->processParams($request->getFileParams(), $param_flag, true));
     $buffer = 'curl' . ($method_flag ? ' -' . $method_flag : '');
     foreach ($params as $param) {
         $buffer .= $new_line . $param;
     }
     $buffer .= $new_line . $this->processUrl($request);
     $this->flush($buffer);
 }
 /**
  * @param RequestInterface $request
  * @return ResponseInterface
  * @throws Exception
  */
 public function sendRequest(RequestInterface $request)
 {
     $response = $this->getClient()->createResponse();
     $this->getCurl()->reset();
     $curlopts = array(CURLOPT_URL => $request->getUrl());
     $method = $request->getMethod();
     if ($method !== RequestInterface::METHOD_GET && $method !== RequestInterface::METHOD_POST) {
         $curlopts[CURLOPT_CUSTOMREQUEST] = $method;
     }
     $curlopts = $this->getOpts()->getArrayCopy() + $curlopts;
     if ($request->getHeaders()->count()) {
         $headers = array();
         foreach ($request->getHeaders() as $header => $value) {
             $headers[] = "{$header}: {$value}";
         }
         $curlopts[CURLOPT_HTTPHEADER] = $headers;
     }
     $postfields = array();
     if ($method === RequestInterface::METHOD_POST && $request->getFileParams()->count()) {
         $postfields = array_merge($postfields, array_map(array($this->getCurl(), 'preparePostFileField'), $request->getFileParams()->getArrayCopy()));
     }
     if ($method !== RequestInterface::METHOD_GET && $request->getBodyParams()->count()) {
         $postfields = array_merge($postfields, $request->getBodyParams()->export());
     }
     if (!empty($postfields)) {
         $curlopts[CURLOPT_POSTFIELDS] = $postfields;
     }
     $this->getCurl()->setoptArray($curlopts);
     $raw_response = $this->getCurl()->exec();
     $status_code = $this->getCurl()->getInfo(CURLINFO_HTTP_CODE);
     $curl_errno = $this->getCurl()->errno();
     $curl_error = $curl_errno ? $this->getCurl()->error() : null;
     $response_parts = $this->extractResponseHeadersAndBody($raw_response);
     $response->setStatusCode($status_code);
     $this->parseHeaders($response->getHeaders(), $response_parts[0]);
     $response->setBody($response_parts[1]);
     if ($curl_errno) {
         throw new Exception($curl_error, $curl_errno);
     }
     return $response;
 }