10 * Various HTTP related functions
16 * Simple wrapper for Http::request( 'GET' )
17 * @see Http::request()
19 public static function get( $url, $timeout = 'default', $opts = array() ) {
20 return Http
::request( "GET", $url, $timeout, $opts );
24 * Simple wrapper for Http::request( 'POST' )
25 * @see Http::request()
27 public static function post( $url, $timeout = 'default', $opts = array() ) {
28 return Http
::request( "POST", $url, $timeout, $opts );
32 * Get the contents of a file by HTTP
33 * @param $method string HTTP method. Usually GET/POST
34 * @param $url string Full URL to act on
35 * @param $timeout int Seconds to timeout. 'default' falls to $wgHTTPTimeout
36 * @param $curlOptions array Optional array of extra params to pass
39 public static function request( $method, $url, $timeout = 'default', $curlOptions = array() ) {
40 global $wgHTTPTimeout, $wgHTTPProxy, $wgTitle;
42 // Go ahead and set the timeout if not otherwise specified
43 if ( $timeout == 'default' ) {
44 $timeout = $wgHTTPTimeout;
47 wfDebug( __METHOD__
. ": $method $url\n" );
48 # Use curl if available
49 if ( function_exists( 'curl_init' ) ) {
50 $c = curl_init( $url );
51 if ( self
::isLocalURL( $url ) ) {
52 curl_setopt( $c, CURLOPT_PROXY
, 'localhost:80' );
53 } else if ($wgHTTPProxy) {
54 curl_setopt($c, CURLOPT_PROXY
, $wgHTTPProxy);
57 curl_setopt( $c, CURLOPT_TIMEOUT
, $timeout );
58 curl_setopt( $c, CURLOPT_USERAGENT
, self
:: userAgent() );
59 if ( $method == 'POST' ) {
60 curl_setopt( $c, CURLOPT_POST
, true );
61 curl_setopt( $c, CURLOPT_POSTFIELDS
, '' );
64 curl_setopt( $c, CURLOPT_CUSTOMREQUEST
, $method );
66 # Set the referer to $wgTitle, even in command-line mode
67 # This is useful for interwiki transclusion, where the foreign
68 # server wants to know what the referring page is.
69 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
71 if ( is_object( $wgTitle ) ) {
72 curl_setopt( $c, CURLOPT_REFERER
, $wgTitle->getFullURL() );
75 if ( is_array( $curlOptions ) ) {
76 foreach( $curlOptions as $option => $value ) {
77 curl_setopt( $c, $option, $value );
83 $text = ob_get_contents();
86 # Don't return the text of error messages, return false on error
87 $retcode = curl_getinfo( $c, CURLINFO_HTTP_CODE
);
88 if ( $retcode != 200 ) {
89 wfDebug( __METHOD__
. ": HTTP return code $retcode\n" );
92 # Don't return truncated output
93 $errno = curl_errno( $c );
94 if ( $errno != CURLE_OK
) {
95 $errstr = curl_error( $c );
96 wfDebug( __METHOD__
. ": CURL error code $errno: $errstr\n" );
101 # Otherwise use file_get_contents...
102 # This doesn't have local fetch capabilities...
104 $headers = array( "User-Agent: " . self
:: userAgent() );
105 if( strcasecmp( $method, 'post' ) == 0 ) {
106 // Required for HTTP 1.0 POSTs
107 $headers[] = "Content-Length: 0";
112 'header' => implode( "\r\n", $headers ),
113 'timeout' => $timeout ) );
114 $ctx = stream_context_create($opts);
116 $text = file_get_contents( $url, false, $ctx );
122 * Check if the URL can be served by localhost
123 * @param $url string Full url to check
126 public static function isLocalURL( $url ) {
127 global $wgCommandLineMode, $wgConf;
128 if ( $wgCommandLineMode ) {
134 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
137 $domainParts = explode( '.', $host );
138 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
139 $domainParts = array_reverse( $domainParts );
140 for ( $i = 0; $i < count( $domainParts ); $i++
) {
141 $domainPart = $domainParts[$i];
143 $domain = $domainPart;
145 $domain = $domainPart . '.' . $domain;
147 if ( $wgConf->isLocalVHost( $domain ) ) {
156 * Return a standard user-agent we can use for external requests.
158 public static function userAgent() {
160 return "MediaWiki/$wgVersion";