private function http_request($http_method, $uri, $data = array())
{
$data = empty($data) ? '' : $data;
$data = !empty($data) && is_array($data) ? http_build_query($data) : '';
$http_method = strtoupper($http_method);
if ($http_method == 'GET' && !empty($this->params) && is_array($this->params)) {
throw new RestAgentException("You may not use param() when issuing an HTTP GET. Use data() instead!");
}
$this->header('Content-Length', strlen($data));
if (!empty($data)) {
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
}
// $this->headers is an associative array, to allow for overrides in set(), but
// curl_setopt() takes indexed array, so we need to convert.
$idxed_headers = array();
foreach ($this->headers as $name => $value) {
$idxed_headers[] = "{$name}: {$value}";
}
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $idxed_headers);
$full_url = $this->get_full_url($uri);
// Sometimes you want to use query params with non-HTTP GET methods
if ($http_method != 'GET') {
$params = is_array($this->params) ? http_build_query($this->params) : null;
if (!empty($params)) {
$full_url .= "?{$params}";
}
}
curl_setopt($this->curl, CURLOPT_URL, $full_url);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $http_method);
$response = curl_exec($this->curl);
//reset defaults to allow clean re-use of the request object
$this->data = array();
$this->headers = array();
$this->method = '';
//$this->check_status($response, $full_url);
$header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$content = substr($response, $header_size);
if (function_exists('http_parse_headers')) {
$headers = http_parse_headers($headers);
} else {
$headers = $this->_http_parse_headers($headers);
}
return array('code' => curl_getinfo($this->curl, CURLINFO_HTTP_CODE), 'meta' => curl_getinfo($this->curl), 'headers' => $headers, 'data' => $content);
}