3 * Functions that relate to HTTP stuff, using library Requests
5 * The goal here is to provide convenient wrapper functions to the Requests library. There are
6 * 2 types of functions for each METHOD, where METHOD is 'get' or 'post' (implement more as needed)
7 * - yourls_http_METHOD() :
8 * Return a complete Response object (with ->body, ->headers, ->status_code, etc...) or
9 * a simple string (error message)
10 * - yourls_http_METHOD_body() :
11 * Return a string (response body) or null if there was an error
13 * TODO: global $ydb->debug_log should store the error message if any
19 * Perform a GET request, return response object or error string message
21 * Notable object properties: body, headers, status_code
24 * @see yourls_http_request
25 * @return mixed Response object, or error string
27 function yourls_http_get( $url, $headers = array(), $data = array(), $options = array() ) {
28 return yourls_http_request( 'GET', $url, $headers, $data, $options );
32 * Perform a GET request, return body or null if there was an error
35 * @see yourls_http_request
36 * @return mixed String (page body) or null if error
38 function yourls_http_get_body( $url, $headers = array(), $data = array(), $options = array() ) {
39 $return = yourls_http_get( $url, $headers, $data, $options );
40 return isset( $return->body ) ? $return->body : null;
44 * Perform a POST request, return response object
46 * Notable object properties: body, headers, status_code
49 * @see yourls_http_request
50 * @return mixed Response object, or error string
52 function yourls_http_post( $url, $headers = array(), $data = array(), $options = array() ) {
53 return yourls_http_request( 'POST', $url, $headers, $data, $options );
57 * Perform a POST request, return body
59 * Wrapper for yourls_http_request()
62 * @see yourls_http_request
63 * @return mixed String (page body) or null if error
65 function yourls_http_post_body( $url, $headers = array(), $data = array(), $options = array() ) {
66 $return = yourls_http_post( $url, $headers, $data, $options );
67 return isset( $return->body ) ? $return->body : null;
71 * Default HTTP requests options for YOURLS
73 * For a list of all available options, see function request() in /includes/Requests/Requests.php
76 * @return array Options
78 function yourls_http_default_options() {
80 'timeout' => yourls_apply_filter( 'http_default_options_timeout', 3 ),
81 'useragent' => yourls_http_user_agent(),
82 'follow_redirects' => true,
86 return yourls_apply_filter( 'http_default_options', $options );
90 * Perform a HTTP request, return response object
93 * @param string $var Stuff
94 * @return string Result
96 function yourls_http_request( $type, $url, $headers, $data, $options ) {
97 yourls_http_load_library();
99 $options = array_merge( yourls_http_default_options(), $options );
102 $result = Requests::request( $url, $headers, $data, $type, $options );
103 } catch( Requests_Exception $e ) {
104 $result = $e->getMessage();
111 * Check if Requests class is defined, include Requests library if need be
113 * All HTTP functions should perform that check prior to any operation. This is to avoid
114 * include()-ing all the Requests files on every YOURLS instance disregarding whether needed or not.
118 function yourls_http_load_library() {
119 if ( !class_exists( 'Requests', false ) ) {
120 require_once dirname( __FILE__ ) . '/Requests/Requests.php';
121 Requests::register_autoloader();
126 * Deprecated. Get remote content via a GET request using best transport available
127 * Returns $content (might be an error message) or false if no transport available
130 function yourls_get_remote_content( $url, $maxlen = 4096, $timeout = 5 ) {
131 yourls_deprecated_function( __FUNCTION__, '1.7', 'yourls_http_get' );
132 return yourls_http_get_body( $url );
136 * Return funky user agent string
139 function yourls_http_user_agent() {
140 return yourls_apply_filter( 'http_user_agent', 'YOURLS v'.YOURLS_VERSION.' +http://yourls.org/ (running on '.YOURLS_SITE.')' );