Getting a start on making the CURLRequest class functional.

This commit is contained in:
Lonnie Ezell 2015-11-13 22:01:49 -06:00
parent 5fa6238e2e
commit 05d536cc75
2 changed files with 151 additions and 8 deletions

View File

@ -1,6 +1,7 @@
<?php namespace App\Config;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\URI;
/**
* Services Configuration file.
@ -91,7 +92,7 @@ class Services {
//--------------------------------------------------------------------
public static function curlrequest($response=null)
public static function curlrequest(array $options=[], $response=null)
{
if (! is_object($response))
{
@ -100,9 +101,9 @@ class Services {
return new \CodeIgniter\HTTP\CURLRequest(
new AppConfig(),
null,
null,
$response
new URI(),
$response,
$options
);
}

View File

@ -14,10 +14,17 @@ use App\Config\AppConfig;
*/
class CURLRequest extends Request
{
protected $baseURI;
protected $timeout = 2.0;
protected $response;
/**
* The first poriton of URI that is prepended
* to all relative requests.
* @var string
*/
protected $base_uri;
//--------------------------------------------------------------------
/**
@ -29,14 +36,23 @@ class CURLRequest extends Request
*
* @param array $options
*/
public function __construct(AppConfig $config, URI $uri = null, $response=null)
public function __construct(AppConfig $config, URI $uri, ResponseInterface $response=null, array $options=[])
{
if (! function_exists('curl_version'))
{
throw new \RuntimeException('CURL must be enabled to use the CURLRequest class.');
}
parent::__construct($config, $uri);
parent::__construct($config);
$this->response = $response;
if (array_key_exists('base_uri', $options))
{
$this->base_uri = $uri->setURI($options['base_uri']);
}
$this->parseOptions($options);
}
//--------------------------------------------------------------------
@ -53,7 +69,13 @@ class CURLRequest extends Request
*/
public function request($method, string $url, array $options = []): Response
{
$this->parseOptions($options);
$url = $this->prepareURL($url);
$this->send($url);
return $this->response;
}
//--------------------------------------------------------------------
@ -163,4 +185,124 @@ class CURLRequest extends Request
//--------------------------------------------------------------------
/**
* Sets the correct settings based on the options array
* passed in.
*
* @param array $options
*/
protected function parseOptions(array $options)
{
foreach ($options as $key => $value)
{
if (isset($this->$key))
{
$this->$key = $value;
}
}
}
//--------------------------------------------------------------------
/**
* If the $url is a relative URL, will attempt to create
* a full URL by prepending $this->base_uri to it.
*
* @param string $url
*
* @return string
*/
protected function prepareURL(string $url): string
{
// If it's a full URI, then we have nothing to do here...
if (strpos($url, '://') !== false)
{
return $url;
}
return (string)$this->base_uri->resolveRelativeURI($url);
}
//--------------------------------------------------------------------
/**
* Fires the actual cURL request.
*
* @param string $url
*/
public function send(string $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HEADER, true);
// Send the request and wait for a response.
$output = curl_exec($ch);
if($output === false)
{
echo "Error Number:".curl_errno($ch)."<br>";
echo "Error String:".curl_error($ch);
}
curl_close($ch);
// Split out our headers and body
$break = strpos($output, "\r\n\r\n");
if ($break !== false)
{
// Our headers
$headers = explode("\n", substr($output, 0, $break));
$this->setResponseHeaders($headers);
// Our body
$body = substr($output, $break+4);
$this->response->setBody($body);
}
return true;
}
//--------------------------------------------------------------------
/**
* Parses the header retrieved from the cURL response into
* our Response object.
*
* @param array $headers
*/
protected function setResponseHeaders(array $headers = [])
{
foreach ($headers as $header)
{
if (($pos = strpos($header, ':')) !== false)
{
$title = substr($header, 0, $pos);
$value = substr($header, $pos+1);
$this->response->setHeader($title, $value);
}
else if (substr($header, 0, 4) == 'HTTP')
{
preg_match('#^HTTP\/(1\.[01]) ([0-9]+) (.+)#', $header, $matches);
if (isset($matches[1]))
{
$this->response->setProtocolVersion($matches[1]);
}
if (isset($matches[2]))
{
$this->response->setStatusCode($matches[2], isset($matches[3]) ? $matches[3] : null);
}
}
}
}
//--------------------------------------------------------------------
}