MediaWiki  master
HttpRequestFactory.php
Go to the documentation of this file.
1 <?php
20 namespace MediaWiki\Http;
21 
22 use CurlHttpRequest;
24 use Http;
26 use MWHttpRequest;
27 use PhpHttpRequest;
28 use Profiler;
30 use 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 }
create( $url, array $options=[], $caller=__METHOD__)
Generate a new MWHttpRequest object.
$wgVersion
MediaWiki version number.
static instance()
Singleton.
Definition: Profiler.php:63
static getInstance( $channel)
Get a named logger instance from the currently configured logger factory.
post( $url, array $options=[], $caller=__METHOD__)
Simple wrapper for request( &#39;POST&#39; ), parameters have same meaning as for request() ...
static $httpEngine
Definition: Http.php:31
Various HTTP related functions.
Definition: Http.php:29
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:58
Factory creating MWHttpRequest objects.
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.
wfIniGetBool( $setting)
Safety wrapper around ini_get() for boolean settings.