public static function send($method, $url, $body = null, $headers = array(), $username = null, $password = null)
{
self::$handle = curl_init();
if ($method !== Method::GET) {
if ($method === Method::POST) {
curl_setopt(self::$handle, CURLOPT_POST, true);
} else {
curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, $method);
}
curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
} elseif (is_array($body)) {
if (strpos($url, '?') !== false) {
$url .= '&';
} else {
$url .= '?';
}
$url .= urldecode(http_build_query(self::buildHTTPCurlQuery($body)));
}
$curl_base_options = [CURLOPT_URL => self::encodeUrl($url), CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 10, CURLOPT_HTTPHEADER => self::getFormattedHeaders($headers), CURLOPT_HEADER => true, CURLOPT_SSL_VERIFYPEER => self::$verifyPeer, CURLOPT_SSL_VERIFYHOST => self::$verifyHost === false ? 0 : 2, CURLOPT_ENCODING => ''];
curl_setopt_array(self::$handle, self::mergeCurlOptions($curl_base_options, self::$curlOpts));
if (self::$socketTimeout !== null) {
curl_setopt(self::$handle, CURLOPT_TIMEOUT, self::$socketTimeout);
}
if (self::$cookie) {
curl_setopt(self::$handle, CURLOPT_COOKIE, self::$cookie);
}
if (self::$cookieFile) {
curl_setopt(self::$handle, CURLOPT_COOKIEFILE, self::$cookieFile);
curl_setopt(self::$handle, CURLOPT_COOKIEJAR, self::$cookieFile);
}
// supporting deprecated http auth method
if (!empty($username)) {
curl_setopt_array(self::$handle, array(CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => $username . ':' . $password));
}
if (!empty(self::$auth['user'])) {
curl_setopt_array(self::$handle, array(CURLOPT_HTTPAUTH => self::$auth['method'], CURLOPT_USERPWD => self::$auth['user'] . ':' . self::$auth['pass']));
}
if (self::$proxy['address'] !== false) {
curl_setopt_array(self::$handle, array(CURLOPT_PROXYTYPE => self::$proxy['type'], CURLOPT_PROXY => self::$proxy['address'], CURLOPT_PROXYPORT => self::$proxy['port'], CURLOPT_HTTPPROXYTUNNEL => self::$proxy['tunnel'], CURLOPT_PROXYAUTH => self::$proxy['auth']['method'], CURLOPT_PROXYUSERPWD => self::$proxy['auth']['user'] . ':' . self::$proxy['auth']['pass']));
}
$response = curl_exec(self::$handle);
$error = curl_error(self::$handle);
$info = self::getInfo();
if ($error) {
throw new Exception($error);
}
// Split the full response in its headers and body
$header_size = $info['header_size'];
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$httpCode = $info['http_code'];
return new Response($httpCode, $body, $header, self::$jsonOpts);
}