MediaWiki REL1_34
HttpRequestFactory.php
Go to the documentation of this file.
1<?php
21
24use Http;
28use Profiler;
29use RuntimeException;
30use Status;
31
69 public function create( $url, array $options = [], $caller = __METHOD__ ) {
70 if ( !Http::$httpEngine ) {
71 Http::$httpEngine = 'guzzle';
72 }
73
74 if ( !isset( $options['logger'] ) ) {
75 $options['logger'] = LoggerFactory::getInstance( 'http' );
76 }
77
78 switch ( Http::$httpEngine ) {
79 case 'guzzle':
80 return new GuzzleHttpRequest( $url, $options, $caller, Profiler::instance() );
81 case 'curl':
82 return new CurlHttpRequest( $url, $options, $caller, Profiler::instance() );
83 case 'php':
84 return new PhpHttpRequest( $url, $options, $caller, Profiler::instance() );
85 default:
86 throw new RuntimeException( __METHOD__ . ': The requested engine is not valid.' );
87 }
88 }
89
95 public function canMakeRequests() {
96 return function_exists( 'curl_init' ) || wfIniGetBool( 'allow_url_fopen' );
97 }
98
110 public function request( $method, $url, array $options = [], $caller = __METHOD__ ) {
111 $logger = LoggerFactory::getInstance( 'http' );
112 $logger->debug( "$method: $url" );
113
114 $options['method'] = strtoupper( $method );
115
116 if ( !isset( $options['timeout'] ) ) {
117 $options['timeout'] = 'default';
118 }
119 if ( !isset( $options['connectTimeout'] ) ) {
120 $options['connectTimeout'] = 'default';
121 }
122
123 $req = $this->create( $url, $options, $caller );
124 $status = $req->execute();
125
126 if ( $status->isOK() ) {
127 return $req->getContent();
128 } else {
129 $errors = $status->getErrorsByType( 'error' );
130 $logger->warning( Status::wrap( $status )->getWikiText( false, false, 'en' ),
131 [ 'error' => $errors, 'caller' => $caller, 'content' => $req->getContent() ] );
132 return null;
133 }
134 }
135
145 public function get( $url, array $options = [], $caller = __METHOD__ ) {
146 return $this->request( 'GET', $url, $options, $caller );
147 }
148
158 public function post( $url, array $options = [], $caller = __METHOD__ ) {
159 return $this->request( 'POST', $url, $options, $caller );
160 }
161
165 public function getUserAgent() {
166 global $wgVersion;
167
168 return "MediaWiki/$wgVersion";
169 }
170}
$wgVersion
MediaWiki version number.
wfIniGetBool( $setting)
Safety wrapper around ini_get() for boolean settings.
MWHttpRequest implemented using internal curl compiled into PHP.
MWHttpRequest implemented using the Guzzle library.
Various HTTP related functions.
Definition Http.php:29
static $httpEngine
Definition Http.php:31
This wrapper class will call out to curl (if available) or fallback to regular PHP if necessary for h...
Factory creating MWHttpRequest objects.
post( $url, array $options=[], $caller=__METHOD__)
Simple wrapper for request( 'POST' ), parameters have same meaning as for request()
create( $url, array $options=[], $caller=__METHOD__)
Generate a new MWHttpRequest object.
canMakeRequests()
Simple function to test if we can make any sort of requests at all, using cURL or fopen()
request( $method, $url, array $options=[], $caller=__METHOD__)
Perform an HTTP request.
PSR-3 logger instance factory.
Profiler base class that defines the interface and some trivial functionality.
Definition Profiler.php:33
static instance()
Singleton.
Definition Profiler.php:63
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:40