]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-http.php
More consistent HTTP functions.
[Github/YOURLS.git] / includes / functions-http.php
1 <?php
2 /**
3  * Functions that relate to HTTP stuff, using library Requests
4  *
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
12  *
13  * TODO: global $ydb->debug_log should store the error message if any
14  *
15  * @since 1.7
16  */
17
18 /**
19  * Perform a GET request, return response object or error string message
20  *
21  * Notable object properties: body, headers, status_code
22  *
23  * @since 1.7
24  * @see yourls_http_request
25  * @return mixed Response object, or error string
26  */
27 function yourls_http_get( $url, $headers = array(), $data = array(), $options = array() ) {
28         return yourls_http_request( 'GET', $url, $headers, $data, $options );
29 }
30
31 /**
32  * Perform a GET request, return body or null if there was an error
33  *
34  * @since 1.7
35  * @see yourls_http_request
36  * @return mixed String (page body) or null if error
37  */
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;
41 }
42
43 /**
44  * Perform a POST request, return response object
45  *
46  * Notable object properties: body, headers, status_code
47  *
48  * @since 1.7
49  * @see yourls_http_request
50  * @return mixed Response object, or error string
51  */
52 function yourls_http_post( $url, $headers = array(), $data = array(), $options = array() ) {
53         return yourls_http_request( 'POST', $url, $headers, $data, $options );
54 }
55
56 /**
57  * Perform a POST request, return body
58  *
59  * Wrapper for yourls_http_request()
60  *
61  * @since 1.7
62  * @see yourls_http_request
63  * @return mixed String (page body) or null if error
64  */
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;
68 }
69
70 /**
71  * Default HTTP requests options for YOURLS
72  *
73  * For a list of all available options, see function request() in /includes/Requests/Requests.php
74  *
75  * @since 1.7
76  * @return array Options
77  */
78 function yourls_http_default_options() {
79         $options = array(
80                 'timeout'          => yourls_apply_filter( 'http_default_options_timeout', 3 ),
81                 'useragent'        => yourls_http_user_agent(),
82                 'follow_redirects' => true,
83                 'redirects'        => 3,
84         );
85
86         return yourls_apply_filter( 'http_default_options', $options ); 
87 }
88
89 /**
90  * Perform a HTTP request, return response object
91  *
92  * @since 1.7
93  * @param string $var Stuff
94  * @return string Result
95  */
96 function yourls_http_request( $type, $url, $headers, $data, $options ) {
97         yourls_http_load_library();
98         
99         $options = array_merge( yourls_http_default_options(), $options );
100         
101         try {
102                 $result = Requests::request( $url, $headers, $data, $type, $options );
103         } catch( Requests_Exception $e ) {
104                 $result = $e->getMessage();
105         };
106         
107         return $result;
108 }
109
110 /**
111  * Check if Requests class is defined, include Requests library if need be
112  *
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.
115  *
116  * @since 1.7
117  */
118 function yourls_http_load_library() {
119         if ( !class_exists( 'Requests', false ) ) {
120                 require_once dirname( __FILE__ ) . '/Requests/Requests.php';
121                 Requests::register_autoloader();
122         }
123 }
124
125 /**
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
128  *
129  */
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 );
133 }
134
135 /**
136  * Return funky user agent string
137  *
138  */
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.')' );
141 }