Modules

Remote
extends Kohana_Remote

Provides remote server communications options using curl.

package
Kohana
category
Helpers
author
Kohana Team
copyright
© 2008-2010 Kohana Team
license
http://kohanaframework.org/license

Class declared in SYSPATH/classes/remote.php on line 3.

Constants

  • None

Properties

Methods

Properties

public static array $default_options

default cURL options

array(4) (
    10018 => string(66) "Mozilla/5.0 (compatible; Kohana v3.0 +http://kohanaframework.org/)"
    78 => integer 5
    13 => integer 5
    19913 => bool TRUE
)

Methods

public static get( string $url [, array $options = NULL ] ) (defined in Kohana_Remote)

Returns the output of a remote URL. Any curl option may be used.

// Do a simple GET request
$data = Remote::get($url);

// Do a POST request
$data = Remote::get($url, array(
    CURLOPT_POST       => TRUE,
    CURLOPT_POSTFIELDS => http_build_query($array),
));

Parameters

  • string $url required - Remote URL
  • array $options = NULL - Curl options

Tags

Return Values

  • string

Source Code

public static function get($url, array $options = NULL)
{
	if ($options === NULL)
	{
		// Use default options
		$options = Remote::$default_options;
	}
	else
	{
		// Add default options
		$options = $options + Remote::$default_options;
	}

	// Open a new remote connection
	$remote = curl_init($url);

	// Set connection options
	if ( ! curl_setopt_array($remote, $options))
	{
		throw new Kohana_Exception('Failed to set CURL options, check CURL documentation: :url',
			array(':url' => 'http://php.net/curl_setopt_array'));
	}

	// Get the response
	$response = curl_exec($remote);

	// Get the response information
	$code = curl_getinfo($remote, CURLINFO_HTTP_CODE);

	if ($code AND $code < 200 OR $code > 299)
	{
		$error = $response;
	}
	elseif ($response === FALSE)
	{
		$error = curl_error($remote);
	}

	// Close the connection
	curl_close($remote);

	if (isset($error))
	{
		throw new Kohana_Exception('Error fetching remote :url [ status :code ] :error', array(
				':url' => $url,
				':code' => $code,
				':error' => $error,
			), $code);
	}

	return $response;
}

public static status( string $url ) (defined in Kohana_Remote)

Returns the status code (200, 500, etc) for a URL.

$status = Remote::status($url);

Parameters

  • string $url required - URL to check

Return Values

  • integer

Source Code

public static function status($url)
{
	// Get the hostname and path
	$url = parse_url($url);

	if (empty($url['path']))
	{
		// Request the root document
		$url['path'] = '/';
	}

	// Open a remote connection
	$port = isset($url['port']) ? $url['port'] : 80;
	$remote = fsockopen($url['host'], $port, $errno, $errstr, 5);

	if ( ! is_resource($remote))
		return FALSE;

	// Set CRLF
	$line_feed = "\r\n";

	// Send request
	fwrite($remote, 'HEAD '.$url['path'].' HTTP/1.0'.$line_feed);
	fwrite($remote, 'Host: '.$url['host'].$line_feed);
	fwrite($remote, 'Connection: close'.$line_feed);
	fwrite($remote, 'User-Agent: Kohana Framework (+http://kohanaframework.org/)'.$line_feed);

	// Send one more CRLF to terminate the headers
	fwrite($remote, $line_feed);

	// Remote is offline
	$response = FALSE;

	while ( ! feof($remote))
	{
		// Get the line
		$line = trim(fgets($remote, 512));

		if ($line !== '' AND preg_match('#^HTTP/1\.[01] (\d{3})#', $line, $matches))
		{
			// Response code found
			$response = (int) $matches[1];
			break;
		}
	}

	// Close the connection
	fclose($remote);

	return $response;
}