/** * Perform an HTTP request * @param $method string HTTP method. Usually GET/POST * @param $url string Full URL to act on * @param $options options to pass to HttpRequest object * Possible keys for the array: * timeout Timeout length in seconds * postData An array of key-value pairs or a url-encoded form data * proxy The proxy to use. Will use $wgHTTPProxy (if set) otherwise. * noProxy Override $wgHTTPProxy (if set) and don't use any proxy at all. * sslVerifyHost (curl only) Verify the SSL certificate * caInfo (curl only) Provide CA information * maxRedirects Maximum number of redirects to follow (defaults to 5) * followRedirects Whether to follow redirects (defaults to true) * @returns mixed (bool)false on failure or a string on success */ public static function request($method, $url, $options = array()) { wfDebug("HTTP: {$method}: {$url}"); $options['method'] = strtoupper($method); if (!isset($options['timeout'])) { $options['timeout'] = 'default'; } $req = HttpRequest::factory($url, $options); $status = $req->execute(); if ($status->isOK()) { return $req->getContent(); } else { return false; } }
/** * Retrieve a feed. * @param $key String: * @param $headers Array: headers to send along with the request * @return Status object */ protected function fetchRemote($key, array $headers = array()) { global $wgRSSFetchTimeout, $wgRSSUserAgent, $wgRSSProxy; if ($this->etag) { wfDebugLog('RSS', 'Used etag: ' . $this->etag); $headers['If-None-Match'] = $this->etag; } if ($this->lastModified) { $lm = gmdate('r', $this->lastModified); wfDebugLog('RSS', "Used last modified: {$lm}"); $headers['If-Modified-Since'] = $lm; } $client = HttpRequest::factory($this->url, array('timeout' => $wgRSSFetchTimeout, 'proxy' => $wgRSSProxy)); $client->setUserAgent($wgRSSUserAgent); foreach ($headers as $header => $value) { $client->setHeader($header, $value); } $fetch = $client->execute(); $this->client = $client; if (!$fetch->isGood()) { wfDebug('RSS', 'Request Failed: ' . $fetch->getWikiText()); return $fetch; } $ret = $this->responseToXML($key); return $ret; }
function runCookieRequests() { $r = HttpRequest::factory("http://www.php.net/manual"); $r->execute(); $jar = $r->getCookieJar(); $this->assertThat($jar, $this->isInstanceOf('CookieJar')); if (is_a($r, 'PhpHttpRequest') && version_compare('5.1.7', phpversion(), '>')) { $this->markTestSkipped('Redirection fails or crashes PHP on 5.1.6 and prior'); } $serialized = $jar->serializeToHttpRequest("/search?q=test", "www.php.net"); $this->assertRegExp('/\\bCOUNTRY=[^=;]+/', $serialized); $this->assertRegExp('/\\bLAST_LANG=[^=;]+/', $serialized); $this->assertEquals('', $jar->serializeToHttpRequest("/search?q=test", "www.php.com")); }
protected function getMicrosoftSuggestion($serviceName, $config) { global $wgMemc; $this->mustHaveDefinition(); self::checkTranslationServiceFailure($serviceName); $code = $this->handle->getCode(); $definition = trim(strval($this->getDefinition())); $definition = self::wrapUntranslatable($definition); $memckey = wfMemckey('translate-tmsug-badcodes-' . $serviceName); $unsupported = $wgMemc->get($memckey); if (isset($unsupported[$code])) { return null; } $options = array(); $options['timeout'] = $config['timeout']; $params = array('text' => $definition, 'to' => $code); if (isset($config['key'])) { $params['appId'] = $config['key']; } else { return null; } $url = $config['url'] . '?' . wfArrayToCgi($params); $url = wfExpandUrl($url); $options['method'] = 'GET'; if (class_exists('MWHttpRequest')) { $req = MWHttpRequest::factory($url, $options); } else { $req = HttpRequest::factory($url, $options); } $status = $req->execute(); if (!$status->isOK()) { $error = $req->getContent(); if (strpos($error, 'must be a valid language') !== false) { $unsupported[$code] = true; $wgMemc->set($memckey, $unsupported, 60 * 60 * 8); return null; } if ($error) { error_log(__METHOD__ . ': Http::get failed:' . $error); } else { error_log(__METHOD__ . ': Unknown error, grr'); } // Most likely a timeout or other general error self::reportTranslationServiceFailure($serviceName); } $ret = $req->getContent(); $text = preg_replace('~<string.*>(.*)</string>~', '\\1', $ret); $text = Sanitizer::decodeCharReferences($text); $text = self::unwrapUntranslatable($text); $text = $this->suggestionField($text); return Html::rawElement('div', null, self::legend($serviceName) . $text . self::clear()); }
/** * @depends testApiGotCookie */ function testApiListPages(CookieJar $cj) { $this->markTestIncomplete("Not done with this yet"); if ($wgServerName == "localhost" || $wgServer == "http://localhost") { $this->markTestIncomplete('This test needs $wgServerName and $wgServer to ' . 'be set in LocalSettings.php'); } $req = HttpRequest::factory(self::$apiUrl . "?action=query&format=xml&prop=revisions&" . "titles=Main%20Page&rvprop=timestamp|user|comment|content"); $req->setCookieJar($cj); $req->execute(); libxml_use_internal_errors(true); $sxe = simplexml_load_string($req->getContent()); $this->assertNotType("bool", $sxe); $this->assertThat($sxe, $this->isInstanceOf("SimpleXMLElement")); $a = $sxe->query[0]->pages[0]->page[0]->attributes(); }
function sendRequest($scriptUrl, $titleText) { $url = $scriptUrl . '?' . wfArrayToCGI(array('action' => 'render', 'title' => $titleText)); $req = HttpRequest::factory($url); $status = $req->execute(); if (!$status->isOK()) { return $status; } return Status::newGood(array('text' => $req->getContent())); }
protected function getUrlContent($url) { global $wgIgnApiConfig; $options = array('timeout' => 'default'); echo "Creating request\n"; $req = HttpRequest::factory($url, $options); $req->setHeader('X-App-Id', $wgIgnApiConfig['AppId']); $req->setHeader('X-App-Key', $wgIgnApiConfig['AppKey']); echo "Executing\n"; $status = $req->execute(); if ($status->isOK()) { echo "Got content\n"; $ret = $req->getContent(); } else { $errMsg = "Requested URL was: " . $req->getFinalUrl(); $errMsg .= " (err: " . json_encode($req->status->errors) . ')'; Wikia::log(__METHOD__, 'error', $errMsg); echo "No content\n" . $errMsg; $ret = false; } return $ret; }
/** * Returns the foreign wiki options array from a valid wiki page * * @return Array */ private static function getOptionsFromForeignWiki( $pageName ) { global $wgMemc; wfProfileIn( __METHOD__ ); $key = null; $rev = null; if ( $pageName ) { $memcKey = wfMemcKey( 'zero-rated-mobile-access-foreign-options-', md5( $pageName ) ); $foreignOptions = $wgMemc->get( $memcKey ); if ( !$foreignOptions ) { $options = array(); $options['method'] = 'GET'; $url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&&rvlimit=1&rvprop=content&format=json&titles=MediaWiki:' . $pageName; $req = ( class_exists( 'MWHttpRequest' ) ) ? MWHttpRequest::factory( $url, $options ) : HttpRequest::factory( $url, $options ); $status = $req->execute(); if ( !$status->isOK() ) { $error = $req->getContent(); wfProfileOut( __METHOD__ ); return array( $key, $rev ); } $ret = $req->getContent(); $jsonData = FormatJson::decode( $ret, true ); if ( isset( $jsonData['query']['pages'] ) ) { $key = key( $jsonData['query']['pages'] ); if ( !is_int( $key ) ) { $key = null; } foreach ( $jsonData['query']['pages'] as $pages ) { if ( isset( $pages['revisions'][0]['*'] ) ) { $rev = $pages['revisions'][0]['*']; } } } if ( $key && $rev ) { $wgMemc->set( $memcKey, array( $key, $rev ), self::getMaxAge() ); } } else { list ( $key, $rev ) = $foreignOptions; } } wfProfileOut( __METHOD__ ); return array( $key, $rev ); }