/** * Execute a request * * @param string $url Request URL * @param mixed $payload The payload to send * @param string $method The HTTP method to send * @param array $headers Array of HTTP headers to send in array( 'header: value', 'header: value', ... ) format * @param array $curlOptions Array of options to pass to CURL * * @throws AuthenticationException * @return array */ protected function _makeRequest($url, array $payload = array(), $method = self::Get, array $headers = array(), array $curlOptions = array()) { static $_defaultCurlOptions = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => 0); // Start clean... $this->_resetRequest(); // Add in any user-supplied CURL options $_curlOptions = array_merge($_defaultCurlOptions, $curlOptions); // Add certificate info for SSL if (null !== ($_certificateFile = $this->getConfig('certificate_file'))) { $_curlOptions[CURLOPT_SSL_VERIFYPEER] = true; $_curlOptions[CURLOPT_SSL_VERIFYHOST] = 2; $_curlOptions[CURLOPT_CAINFO] = $_certificateFile; } // And finally our headers if (null !== ($_agent = $this->getConfig('user_agent'))) { $headers[] = 'User-Agent: ' . $_agent; } $_curlOptions[CURLOPT_HTTPHEADER] = $headers; // Convert payload to query string for a GET if (static::Get == $method && !empty($payload)) { $url .= (false === strpos($url, '?') ? '?' : '&') . http_build_query($payload); $payload = array(); } // And finally make the request if (false === ($_result = Curl::request($method, $url, $this->_translatePayload($payload, false), $_curlOptions))) { throw new AuthenticationException(Curl::getErrorAsString()); } // Save off response $this->_lastResponseCode = $_code = Curl::getLastHttpCode(); // Shift result from array... if (is_array($_result) && isset($_result[0]) && sizeof($_result) == 1 && $_result[0] instanceof \stdClass) { $_result = $_result[0]; } $_contentType = Curl::getInfo('content_type'); if (DataFormatTypes::JSON == $this->_responseFormat && false !== stripos($_contentType, 'application/json', 0)) { $_result = $this->_translatePayload($_result); } return $this->_lastResponse = array('result' => $_result, 'code' => $_code, 'content_type' => $_contentType); }
/** * Makes a service request * * @param string $url * @param array|mixed $payload * @param array $curlOptions * @param string $method * * @return string */ public function request($url, $payload = array(), $curlOptions = array(), $method = Curl::Get) { $_response = null; if ($this->_hostPort) { Curl::setHostPort($this->_hostPort); } if ($this->_userName) { Curl::setUserName($this->_userName); if ($this->_password) { Curl::setPassword($this->_password); } } return Curl::request($method, $url, $payload, $curlOptions); }