public function sendRequest()

in templates/php/src/FacebookAds/Http/Adapter/CurlAdapter.php [139:200]


  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;
  }