You are here

public function ServicesClientConnectionCurlRequest::call in Services Client 7.2

Same name and namespace in other branches
  1. 7 services_client_connection/plugins/ServicesClientConnectionCurlRequest.inc \ServicesClientConnectionCurlRequest::call()

Process request and call remote API

Parameters

ServicesClientConnectionHttpRequest $request :

Overrides ServicesClientConnectionRequest::call

File

services_client_connection/plugins/ServicesClientConnectionCurlRequest.inc, line 216
Make requests via cURL

Class

ServicesClientConnectionCurlRequest
@file Make requests via cURL

Code

public function call(ServicesClientConnectionHttpRequest &$request) {
  parent::call($request);
  $opts = $this
    ->getCurlOptions($request);
  if ($request->cookie) {
    if (isset($opts['CURLOPT_COOKIE'])) {
      $opts[CURLOPT_COOKIE] .= '; ' . implode('; ', $request->cookie);
    }
    else {
      $opts[CURLOPT_COOKIE] = implode('; ', $request->cookie);
    }
  }
  $ch = curl_init();
  if ($opts) {
    curl_setopt_array($ch, $opts);
  }
  $ret = new ServicesClientConnectionResponse();
  $ret->raw_response = curl_exec($ch);

  // execute and get response
  if (!empty($ret->raw_response)) {
    $ret->original_response = $ret->raw_response;
    $pos = strrpos($ret->raw_response, "\r\n\r\n");
    if ($pos !== FALSE) {
      $ret->raw_headers = trim(substr($ret->raw_response, 0, $pos));
      $ret->raw_response = trim(substr($ret->raw_response, $pos + 4));
    }
  }
  $ret->error_code = curl_errno($ch);
  $ret->error_message = curl_error($ch);
  $ret->info = curl_getinfo($ch);
  $ret->response_code = $ret->info['http_code'];

  // Try to process HTTP headers
  if (isset($ret->raw_headers)) {
    $ret->response_headers = explode("\r\n", $ret->raw_headers);
  }

  // Services are usually returning specific error message after
  // HTTP code in format:
  //   HTTP/1.0 404 Not found: Site not found on master
  // Try to parse string after :
  if (!empty($ret->response_headers) && !empty($ret->response_code)) {
    $http_response = $ret->response_headers[0];
    if (preg_match('~^[^:]+:(.+)$~i', $http_response, $matches)) {
      $ret->services_error = trim($matches[1]);
    }
  }
  curl_close($ch);
  return $ret;
}