]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-http.php
Implement use of Requests - first pass.
[Github/YOURLS.git] / includes / functions-http.php
1 <?php
2 // Functions that relate to HTTP stuff
3
4 /**
5  * Perform a GET request, return response object
6  *
7  * Notable object properties: body, headers, status_code
8  *
9  * @since 1.7
10  * @see yourls_http_request
11  * @return object Response
12  */
13 function yourls_http_get( $url, $headers = array(), $data = array(), $options = array() ) {
14         return yourls_http_request( 'GET', $url, $headers, $data, $options );
15 }
16
17 /**
18  * Perform a GET request, return body
19  *
20  * @since 1.7
21  * @see yourls_http_request
22  * @return string body
23  */
24 function yourls_http_get_body( $url, $headers = array(), $data = array(), $options = array() ) {
25         $return = yourls_http_get( $url, $headers, $data, $options );
26         return $return->body;
27 }
28
29 /**
30  * Perform a POST request, return response object
31  *
32  * Notable object properties: body, headers, status_code
33  *
34  * @since 1.7
35  * @see yourls_http_request
36  * @return object Response
37  */
38 function yourls_http_post( $url, $headers = array(), $data = array(), $options = array() ) {
39         return yourls_http_request( 'POST', $url, $headers, $data, $options );
40 }
41
42 /**
43  * Perform a POST request, return body
44  *
45  * Wrapper for yourls_http_request()
46  *
47  * @since 1.7
48  * @see yourls_http_request
49  * @return string body
50  */
51 function yourls_http_post_body( $url, $headers = array(), $data = array(), $options = array() ) {
52         $return = yourls_http_post( $url, $headers, $data, $options );
53         return $return->body;
54 }
55
56 /**
57  * Default HTTP requests options for YOURLS
58  *
59  * For a list of all available options, see function request() in Requests/Requests.php
60  *
61  * @since 1.7
62  * @return array Options
63  */
64 function yourls_http_default_options() {
65         $options = array(
66                 'timeout'          => '5',
67                 'useragent'        => yourls_http_user_agent(),
68                 'follow_redirects' => true,
69                 'redirects'        => 3,
70         );
71
72         return yourls_apply_filter( 'http_default_options', $options ); 
73 }
74
75 /**
76  * Perform a HTTP request, return response object
77  *
78  * @since 1.7
79  * @param string $var Stuff
80  * @return string Result
81  */
82 function yourls_http_request( $type, $url, $headers, $data, $options ) {
83         yourls_http_load_library();
84         
85         $options = array_merge( yourls_http_default_options(), $options );
86
87         return Requests::request( $url, $headers, $data, $type, $options );
88 }
89
90 /**
91  * Check if Requests class is defined, include Requests library if need be
92  *
93  * All HTTP functions should perform that check prior to any operation. This is to avoid
94  * include()-ing all the Requests files on every YOURLS instance disregarding whether needed or not.
95  *
96  * @since 1.7
97  */
98 function yourls_http_load_library() {
99         if ( !class_exists( 'Requests', false ) ) {
100                 require_once dirname( __FILE__ ) . '/Requests/Requests.php';
101                 Requests::register_autoloader();
102         }
103 }
104
105 /**
106  * Deprecated. Get remote content via a GET request using best transport available
107  * Returns $content (might be an error message) or false if no transport available
108  *
109  */
110 function yourls_get_remote_content( $url,  $maxlen = 4096, $timeout = 5 ) {
111         yourls_deprecated_function( __FUNCTION__, '1.7', 'yourls_http_get' );
112         return yourls_http_get_body( $url );
113 }
114
115 /**
116  * Return funky user agent string
117  *
118  */
119 function yourls_http_user_agent() {
120         return yourls_apply_filter( 'http_user_agent', 'YOURLS v'.YOURLS_VERSION.' +http://yourls.org/ (running on '.YOURLS_SITE.')' );
121 }