Example #1
0
 /**
  * Make HTTP Request via cURL
  *
  * @param string $url
  *   Request full URL in format 'http://www.example.com/endpoint'
  * @param string $method
  *   HTTP method name
  * @param array $query
  *   URL Query data.
  * @param array $data
  *   HTTP Request data.
  * @param array $headers
  *   Optionally additional headers.
  *   Headers can be in format
  *     array(
  *      'X-Custom-Header' => 'Value',
  *     )
  * @param array $files
  *   Local files list that should be sent with request
  *
  * @return Response
  */
 public function call($url, $method = 'GET', $query = array(), $data = array(), $headers = array(), $files = array())
 {
     // Normalize HTTP method name
     $method = $this->normalizeHttpMethod($method);
     // Zuora API talks in JSON
     $headers += array('Accept' => 'application/json');
     // If sending files, app should be not json
     if (empty($files)) {
         $headers['Content-Type'] = 'application/json';
     } else {
         $files = array_map(function ($item) {
             return '@' . $item;
         }, $files);
     }
     // URL can contain port, make sure its processed correctly.
     list($url, $port) = $this->normalizeUrl($url, $query);
     $opts = $this->getCurlOptions($url, $port, $method, $data, $headers, $files);
     // Prepare curl channel.
     $ch = curl_init();
     curl_setopt_array($ch, $opts);
     $raw = curl_exec($ch);
     // Parse response
     $response = Response::fromString($raw);
     if ($data = $response->getData()) {
         $response->setData(json_decode($data, true));
     }
     // Add curl errors
     if (!$response->getCode()) {
         $response->setErrorCode(curl_errno($ch));
         $response->setErrorMessage(curl_error($ch));
     }
     // Close channel
     curl_close($ch);
     return $response;
 }