2 // Functions that relate to HTTP stuff
5 * Perform a GET request, return response object
7 * Notable object properties: body, headers, status_code
10 * @see yourls_http_request
11 * @return object Response
13 function yourls_http_get( $url, $headers = array(), $data = array(), $options = array() ) {
14 return yourls_http_request( 'GET', $url, $headers, $data, $options );
18 * Perform a GET request, return body
21 * @see yourls_http_request
24 function yourls_http_get_body( $url, $headers = array(), $data = array(), $options = array() ) {
25 $return = yourls_http_get( $url, $headers, $data, $options );
30 * Perform a POST request, return response object
32 * Notable object properties: body, headers, status_code
35 * @see yourls_http_request
36 * @return object Response
38 function yourls_http_post( $url, $headers = array(), $data = array(), $options = array() ) {
39 return yourls_http_request( 'POST', $url, $headers, $data, $options );
43 * Perform a POST request, return body
45 * Wrapper for yourls_http_request()
48 * @see yourls_http_request
51 function yourls_http_post_body( $url, $headers = array(), $data = array(), $options = array() ) {
52 $return = yourls_http_post( $url, $headers, $data, $options );
57 * Default HTTP requests options for YOURLS
59 * For a list of all available options, see function request() in Requests/Requests.php
62 * @return array Options
64 function yourls_http_default_options() {
67 'useragent' => yourls_http_user_agent(),
68 'follow_redirects' => true,
72 return yourls_apply_filter( 'http_default_options', $options );
76 * Perform a HTTP request, return response object
79 * @param string $var Stuff
80 * @return string Result
82 function yourls_http_request( $type, $url, $headers, $data, $options ) {
83 yourls_http_load_library();
85 $options = array_merge( yourls_http_default_options(), $options );
87 return Requests::request( $url, $headers, $data, $type, $options );
91 * Check if Requests class is defined, include Requests library if need be
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.
98 function yourls_http_load_library() {
99 if ( !class_exists( 'Requests', false ) ) {
100 require_once dirname( __FILE__ ) . '/Requests/Requests.php';
101 Requests::register_autoloader();
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
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 );
116 * Return funky user agent string
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.')' );