public
Created

http-api-wrapper

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
<?php
 
if ( class_exists( 'http_api_wrapper' ) ) {
return false;
}
 
/**
* This is the primary class for our HTTP API wrapper.
*
* It is intended to save us many repeated processes involving verification of requests and caching.
*
* @package WordPress
* @subpackage Plugin
* @since 3.0.0
*/
class http_api_wrapper {
 
/**
* Constant for defining how long a request should take before timing out.
*
* @since 3.0.0
* @var integer
*/
const timeout = 20;
 
/**
* Public variable intended to contain the URL being requested.
*
* @since 3.0.0
* @access public
* @var string
*/
public $url = null;
 
/**
* Public variable intended to contain the group or the requested file to be cached under.
*
* @since 3.0.0
* @access public
* @var string
*/
public $group = null;
 
/**
* Public variable containing the parameters used when making requests.
*
* @since 3.0.0
* @access public
* @var array
*/
public $request_parameters = array(
'timeout' => http_api_wrapper::timeout,
'headers' => array( 'Referer' => NULL )
);
 
private $debug = false;
 
public $request_stack = array();
 
/**
* Sets up object properties. PHP 5 style constructor.
*
* Also adds some additional headers useful for debugging and analytics.
*
* @param string $url The requested file.
* @param string $group The group we'll be caching the file under.
* @since 3.0.0
* @return void
*/
function __construct( $url , $group ) {
global $wp;
$this->url = $url;
$this->group = $group;
$this->request_parameters[ 'headers' ][ 'Referer' ] = site_url() . '/' . $wp->request;
}
 
/**
* Try to fetch a cached object. If it's not cached, return false.
*
* @since 3.0.0
* @return mixed Object on success. False on fail.
*/
function cached() {
return wp_cache_get( $this->url , $this->group );
}
 
/**
* Attempt to get the requested file.
*
* @param boolean $sanitize If true, then whatever is returned from the file request, sanitize using kses.
* @since 3.0.0
* @return mixed Error response array on fail. HTTP Object on success.
*/
function get_file( $sanitize = false ) {
$start = timer_stop();
$response = wp_remote_request( $this->url , $this->request_parameters );
$this->request_stack[] = $this->url;
$stop = timer_stop();
$response_time = $stop - $start;
if( wp_remote_retrieve_response_code( $response ) == 200 ) {
if( $sanitize === true ) {
$response[ 'body' ] = wp_kses_data( $response[ 'body' ] );
}
return $response;
} else {
if( is_wp_error( $response) ) {
$error_message = $response->get_error_message();
$error_code = $response->get_error_message();
} else {
$error_code = wp_remote_retrieve_response_code( $response );
$error_message = wp_remote_retrieve_response_message( $response );
}
$error_array = array( 'code' => $error_code , 'message' => $error_message );
return $error_array;
}
}
 
/**
* Attempt to get the requested file.
*
* @param mixed Anything submitted will try to be cached.
* @since 3.0.0
* @return boolean
*/
function cache_file( $data ) {
return wp_cache_add( $url , $data , $group , 0 );
}
 
}
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.