3 * Various HTTP related functions
10 * Simple wrapper for Http::request( 'GET' )
11 * @see Http::request()
13 public static function get( $url, $timeout = 'default', $opts = array() ) {
14 return Http
::request( "GET", $url, $timeout, $opts );
18 * Simple wrapper for Http::request( 'POST' )
19 * @see Http::request()
21 public static function post( $url, $timeout = 'default', $opts = array() ) {
22 return Http
::request( "POST", $url, $timeout, $opts );
26 * Get the contents of a file by HTTP
27 * @param $method string HTTP method. Usually GET/POST
28 * @param $url string Full URL to act on
29 * @param $timeout int Seconds to timeout. 'default' falls to $wgHTTPTimeout
30 * @param $curlOptions array Optional array of extra params to pass
33 public static function request( $method, $url, $timeout = 'default', $curlOptions = array() ) {
34 global $wgHTTPTimeout, $wgHTTPProxy, $wgTitle;
36 // Go ahead and set the timeout if not otherwise specified
37 if ( $timeout == 'default' ) {
38 $timeout = $wgHTTPTimeout;
41 wfDebug( __METHOD__
. ": $method $url\n" );
42 # Use curl if available
43 if ( function_exists( 'curl_init' ) ) {
44 $c = curl_init( $url );
45 if ( self
::isLocalURL( $url ) ) {
46 curl_setopt( $c, CURLOPT_PROXY
, 'localhost:80' );
47 } else if ($wgHTTPProxy) {
48 curl_setopt($c, CURLOPT_PROXY
, $wgHTTPProxy);
51 curl_setopt( $c, CURLOPT_TIMEOUT
, $timeout );
52 curl_setopt( $c, CURLOPT_USERAGENT
, self
:: userAgent() );
53 if ( $method == 'POST' ) {
54 curl_setopt( $c, CURLOPT_POST
, true );
55 curl_setopt( $c, CURLOPT_POSTFIELDS
, '' );
58 curl_setopt( $c, CURLOPT_CUSTOMREQUEST
, $method );
60 # Set the referer to $wgTitle, even in command-line mode
61 # This is useful for interwiki transclusion, where the foreign
62 # server wants to know what the referring page is.
63 # $_SERVER['REQUEST_URI'] gives a less reliable indication of the
65 if ( is_object( $wgTitle ) ) {
66 curl_setopt( $c, CURLOPT_REFERER
, $wgTitle->getFullURL() );
69 if ( is_array( $curlOptions ) ) {
70 foreach( $curlOptions as $option => $value ) {
71 curl_setopt( $c, $option, $value );
77 $text = ob_get_contents();
80 # Don't return the text of error messages, return false on error
81 $retcode = curl_getinfo( $c, CURLINFO_HTTP_CODE
);
82 if ( $retcode != 200 ) {
83 wfDebug( __METHOD__
. ": HTTP return code $retcode\n" );
86 # Don't return truncated output
87 $errno = curl_errno( $c );
88 if ( $errno != CURLE_OK
) {
89 $errstr = curl_error( $c );
90 wfDebug( __METHOD__
. ": CURL error code $errno: $errstr\n" );
95 # Otherwise use file_get_contents...
96 # This doesn't have local fetch capabilities...
98 $headers = array( "User-Agent: " . self
:: userAgent() );
99 if( strcasecmp( $method, 'post' ) == 0 ) {
100 // Required for HTTP 1.0 POSTs
101 $headers[] = "Content-Length: 0";
106 'header' => implode( "\r\n", $headers ),
107 'timeout' => $timeout ) );
108 $ctx = stream_context_create($opts);
110 $text = file_get_contents( $url, false, $ctx );
116 * Check if the URL can be served by localhost
117 * @param $url string Full url to check
120 public static function isLocalURL( $url ) {
121 global $wgCommandLineMode, $wgConf;
122 if ( $wgCommandLineMode ) {
128 if ( preg_match( '!^http://([\w.-]+)[/:].*$!', $url, $matches ) ) {
131 $domainParts = explode( '.', $host );
132 // Check if this domain or any superdomain is listed in $wgConf as a local virtual host
133 $domainParts = array_reverse( $domainParts );
134 for ( $i = 0; $i < count( $domainParts ); $i++
) {
135 $domainPart = $domainParts[$i];
137 $domain = $domainPart;
139 $domain = $domainPart . '.' . $domain;
141 if ( $wgConf->isLocalVHost( $domain ) ) {
150 * Return a standard user-agent we can use for external requests.
152 public static function userAgent() {
154 return "MediaWiki/$wgVersion";