Permalink
Browse files

Added requests library

  • Loading branch information...
captn3m0 committed Aug 16, 2012
1 parent ff49718 commit 533c03272fbe48a33f43daf7156cbe3e521580ef
Showing with 4,188 additions and 0 deletions.
  1. +560 −0 requests/Requests.php
  2. +33 −0 requests/Requests/Auth.php
  3. +88 −0 requests/Requests/Auth/Basic.php
  4. +62 −0 requests/Requests/Exception.php
  5. +67 −0 requests/Requests/Exception/HTTP.php
  6. +27 −0 requests/Requests/Exception/HTTP/400.php
  7. +27 −0 requests/Requests/Exception/HTTP/401.php
  8. +27 −0 requests/Requests/Exception/HTTP/402.php
  9. +27 −0 requests/Requests/Exception/HTTP/403.php
  10. +27 −0 requests/Requests/Exception/HTTP/404.php
  11. +27 −0 requests/Requests/Exception/HTTP/405.php
  12. +27 −0 requests/Requests/Exception/HTTP/406.php
  13. +27 −0 requests/Requests/Exception/HTTP/407.php
  14. +27 −0 requests/Requests/Exception/HTTP/408.php
  15. +27 −0 requests/Requests/Exception/HTTP/409.php
  16. +27 −0 requests/Requests/Exception/HTTP/410.php
  17. +27 −0 requests/Requests/Exception/HTTP/411.php
  18. +27 −0 requests/Requests/Exception/HTTP/412.php
  19. +27 −0 requests/Requests/Exception/HTTP/413.php
  20. +27 −0 requests/Requests/Exception/HTTP/414.php
  21. +27 −0 requests/Requests/Exception/HTTP/415.php
  22. +27 −0 requests/Requests/Exception/HTTP/416.php
  23. +27 −0 requests/Requests/Exception/HTTP/417.php
  24. +29 −0 requests/Requests/Exception/HTTP/418.php
  25. +29 −0 requests/Requests/Exception/HTTP/428.php
  26. +29 −0 requests/Requests/Exception/HTTP/429.php
  27. +29 −0 requests/Requests/Exception/HTTP/431.php
  28. +27 −0 requests/Requests/Exception/HTTP/500.php
  29. +27 −0 requests/Requests/Exception/HTTP/501.php
  30. +27 −0 requests/Requests/Exception/HTTP/502.php
  31. +27 −0 requests/Requests/Exception/HTTP/503.php
  32. +27 −0 requests/Requests/Exception/HTTP/504.php
  33. +27 −0 requests/Requests/Exception/HTTP/505.php
  34. +29 −0 requests/Requests/Exception/HTTP/511.php
  35. +44 −0 requests/Requests/Exception/HTTP/Unknown.php
  36. +61 −0 requests/Requests/Hooks.php
  37. +390 −0 requests/Requests/IDNAEncoder.php
  38. +221 −0 requests/Requests/IPv6.php
  39. +1,218 −0 requests/Requests/IRI.php
  40. +90 −0 requests/Requests/Response.php
  41. +85 −0 requests/Requests/Response/Headers.php
  42. +32 −0 requests/Requests/Transport.php
  43. +218 −0 requests/Requests/Transport/cURL.php
  44. +226 −0 requests/Requests/Transport/fsockopen.php

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,33 @@
<?php
/**
* Authentication provider interface
*
* @package Requests
* @subpackage Authentication
*/
/**
* Authentication provider interface
*
* Implement this interface to act as an authentication provider.
*
* Parameters should be passed via the constructor where possible, as this
* makes it much easier for users to use your provider.
*
* @see Requests_Hooks
* @package Requests
* @subpackage Authentication
*/
interface Requests_Auth {
/**
* Register hooks as needed
*
* This method is called in {@see Requests::request} when the user has set
* an instance as the 'auth' option. Use this callback to register all the
* hooks you'll need.
*
* @see Requests_Hooks::register
* @param Requests_Hooks $hooks Hook system
*/
public function register(Requests_Hooks &$hooks);
}
@@ -0,0 +1,88 @@
<?php
/**
* Basic Authentication provider
*
* @package Requests
* @subpackage Authentication
*/
/**
* Basic Authentication provider
*
* Provides a handler for Basic HTTP authentication via the Authorization
* header.
*
* @package Requests
* @subpackage Authentication
*/
class Requests_Auth_Basic implements Requests_Auth {
/**
* Username
*
* @var string
*/
public $user;
/**
* Password
*
* @var string
*/
public $pass;
/**
* Constructor
*
* @throws Requests_Exception On incorrect number of arguments (`authbasicbadargs`)
* @param array|null $args Array of user and password. Must have exactly two elements
*/
public function __construct($args = null) {
if (is_array($args)) {
if (count($args) !== 2) {
throw new Requests_Exception('Invalid number of arguments', 'authbasicbadargs');
}
list($this->user, $this->pass) = $args;
}
}
/**
* Register the necessary callbacks
*
* @see curl_before_send
* @see fsockopen_header
* @param Requests_Hooks $hooks Hook system
*/
public function register(Requests_Hooks &$hooks) {
$hooks->register('curl.before_send', array(&$this, 'curl_before_send'));
$hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header'));
}
/**
* Set cURL parameters before the data is sent
*
* @param resource $handle cURL resource
*/
public function curl_before_send(&$handle) {
curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($handle, CURLOPT_USERPWD, $this->getAuthString());
}
/**
* Add extra headers to the request before sending
*
* @param string $out HTTP header string
*/
public function fsockopen_header(&$out) {
$out .= "Authorization: Basic " . base64_encode($this->getAuthString()) . "\r\n";
}
/**
* Get the authentication string (user:pass)
*
* @return string
*/
public function getAuthString() {
return $this->user . ':' . $this->pass;
}
}
@@ -0,0 +1,62 @@
<?php
/**
* Exception for HTTP requests
*
* @package Requests
*/
/**
* Exception for HTTP requests
*
* @package Requests
*/
class Requests_Exception extends Exception {
/**
* Type of exception
*
* @var string
*/
protected $type;
/**
* Data associated with the exception
*
* @var mixed
*/
protected $data;
/**
* Create a new exception
*
* @param string $message Exception message
* @param string $type Exception type
* @param mixed $data Associated data
* @param integer $code Exception numerical code, if applicable
*/
public function __construct($message, $type, $data = null, $code = 0) {
parent::__construct($message, $code);
$this->type = $type;
$this->data = $data;
}
/**
* Like {@see getCode()}, but a string code.
*
* @codeCoverageIgnore
* @return string
*/
public function getType() {
return $this->type;
}
/**
* Gives any relevant data
*
* @codeCoverageIgnore
* @return mixed
*/
public function getData() {
return $this->data;
}
}
@@ -0,0 +1,67 @@
<?php
/**
* Exception based on HTTP response
*
* @package Requests
*/
/**
* Exception based on HTTP response
*
* @package Requests
*/
class Requests_Exception_HTTP extends Requests_Exception {
/**
* HTTP status code
*
* @var integer
*/
protected $code = 0;
/**
* Reason phrase
*
* @var string
*/
protected $reason = 'Unknown';
/**
* Create a new exception
*
* There is no mechanism to pass in the status code, as this is set by the
* subclass used. Reason phrases can vary, however.
*
* @param string $reason Reason phrase
* @param mixed $data Associated data
*/
public function __construct($reason = null, $data = null) {
if ($reason !== null) {
$this->reason = $reason;
}
$message = sprintf('%d %s', $this->code, $this->reason);
parent::__construct($message, 'httpresponse', $data, $this->code);
}
/**
* Get the status message
*/
public function get_reason() {
return $this->reason;
}
/**
* Get the correct exception class for a given error code
*
* @param int $code HTTP status code
* @return string Exception class name to use
*/
public static function get_class($code) {
$class = sprintf('Requests_Exception_HTTP_%d', $code);
if (class_exists($class)) {
return $class;
}
return 'Requests_Exception_HTTP_Unknown';
}
}
@@ -0,0 +1,27 @@
<?php
/**
* Exception for 400 Bad Request responses
*
* @package Requests
*/
/**
* Exception for 400 Bad Request responses
*
* @package Requests
*/
class Requests_Exception_HTTP_400 extends Requests_Exception_HTTP {
/**
* HTTP status code
*
* @var integer
*/
protected $code = 400;
/**
* Reason phrase
*
* @var string
*/
protected $reason = 'Bad Request';
}
@@ -0,0 +1,27 @@
<?php
/**
* Exception for 401 Unauthorized responses
*
* @package Requests
*/
/**
* Exception for 401 Unauthorized responses
*
* @package Requests
*/
class Requests_Exception_HTTP_401 extends Requests_Exception_HTTP {
/**
* HTTP status code
*
* @var integer
*/
protected $code = 401;
/**
* Reason phrase
*
* @var string
*/
protected $reason = 'Unauthorized';
}
@@ -0,0 +1,27 @@
<?php
/**
* Exception for 402 Payment Required responses
*
* @package Requests
*/
/**
* Exception for 402 Payment Required responses
*
* @package Requests
*/
class Requests_Exception_HTTP_402 extends Requests_Exception_HTTP {
/**
* HTTP status code
*
* @var integer
*/
protected $code = 402;
/**
* Reason phrase
*
* @var string
*/
protected $reason = 'Payment Required';
}
@@ -0,0 +1,27 @@
<?php
/**
* Exception for 403 Forbidden responses
*
* @package Requests
*/
/**
* Exception for 403 Forbidden responses
*
* @package Requests
*/
class Requests_Exception_HTTP_403 extends Requests_Exception_HTTP {
/**
* HTTP status code
*
* @var integer
*/
protected $code = 403;
/**
* Reason phrase
*
* @var string
*/
protected $reason = 'Forbidden';
}
Oops, something went wrong.

0 comments on commit 533c032

Please sign in to comment.