2008-12-10 03:10:02 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Provides remote server communications options using [curl][ref-curl].
|
|
|
|
*
|
|
|
|
* [ref-curl]: http://php.net/curl
|
|
|
|
*
|
2009-03-21 00:01:47 +02:00
|
|
|
* @package Kohana
|
2008-12-10 03:10:02 +02:00
|
|
|
* @author Kohana Team
|
|
|
|
* @copyright (c) 2008-2009 Kohana Team
|
|
|
|
* @license http://kohanaphp.com/license.html
|
|
|
|
*/
|
|
|
|
class remote_Core {
|
|
|
|
|
|
|
|
// Default curl options
|
2009-03-09 11:56:07 +02:00
|
|
|
public static $default_options = array
|
2008-12-10 03:10:02 +02:00
|
|
|
(
|
2009-03-09 11:56:07 +02:00
|
|
|
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; Kohana v3.0 +http://kohanaphp.com/)',
|
2008-12-10 03:10:02 +02:00
|
|
|
CURLOPT_CONNECTTIMEOUT => 5,
|
|
|
|
CURLOPT_TIMEOUT => 5,
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2009-04-18 00:45:09 +03:00
|
|
|
* Returns the output of a remote URL.
|
|
|
|
*
|
|
|
|
* @throws Kohana_Exception
|
2008-12-10 03:10:02 +02:00
|
|
|
* @param string remote URL
|
|
|
|
* @param array curl options
|
|
|
|
* @return array
|
|
|
|
*/
|
2009-03-09 11:56:07 +02:00
|
|
|
public static function get($url, array $options = NULL)
|
2008-12-10 03:10:02 +02:00
|
|
|
{
|
|
|
|
// Add default options
|
2009-03-09 11:56:07 +02:00
|
|
|
$options = array_merge((array) $options, remote::$default_options);
|
2008-12-10 03:10:02 +02:00
|
|
|
|
|
|
|
// The transfer must always be returned
|
|
|
|
$options[CURLOPT_RETURNTRANSFER] = TRUE;
|
|
|
|
|
|
|
|
// Open a new remote connection
|
|
|
|
$remote = curl_init($url);
|
|
|
|
|
|
|
|
// Set connection options
|
|
|
|
curl_setopt_array($remote, $options);
|
|
|
|
|
2009-04-18 00:45:09 +03:00
|
|
|
// Get the response
|
|
|
|
$response = curl_exec($remote);
|
2008-12-10 03:10:02 +02:00
|
|
|
|
2009-04-18 00:45:09 +03:00
|
|
|
// Get the response information
|
|
|
|
$code = curl_getinfo($remote, CURLINFO_HTTP_CODE);
|
|
|
|
|
|
|
|
if ($response === FALSE OR $code !== 200)
|
2008-12-10 03:10:02 +02:00
|
|
|
{
|
2009-04-18 00:45:09 +03:00
|
|
|
throw new Kohana_Exception('Error fetching remote :url [ status :code ] :error',
|
|
|
|
array(':url' => $url, ':code' => $code, ':error' => curl_error($remote)));
|
2008-12-10 03:10:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close the connection
|
|
|
|
curl_close($remote);
|
|
|
|
|
2009-04-18 00:45:09 +03:00
|
|
|
return $response;
|
2008-12-10 03:10:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End remote
|