get() public method

Sends a GET request.
public get ( string $uri = null, array $headers = [] ) : Horde_Http_Response_Base
$uri string Request URI.
$headers array Additional request headers.
return Horde_Http_Response_Base
Esempio n. 1
0
 /**
  * Place the component source archive at the specified location.
  *
  * @param string $destination The path to write the archive to.
  * @param array  $options     Options for the operation.
  *
  * @return array An array with at least [0] the path to the resulting
  *               archive, optionally [1] an array of error strings, and [2]
  *               PEAR output.
  */
 public function placeArchive($destination, $options = array())
 {
     $this->createDestination($destination);
     $this->_client->{'request.timeout'} = 60;
     file_put_contents($destination . '/' . basename($this->_getDownloadUri()), $this->_client->get($this->_getDownloadUri())->getStream());
     return array($destination . '/' . basename($this->_getDownloadUri()));
 }
Esempio n. 2
0
 public function testGetBodyAfter404()
 {
     $this->_skipMissingConfig();
     $client = new Horde_Http_Client(array('request' => new Horde_Http_Request_Curl()));
     $response = $client->get('http://' . $this->_server . '/doesntexist');
     $content = $response->getBody();
     $this->assertTrue(!empty($content));
 }
Esempio n. 3
0
 public function testGetBodyAfter404()
 {
     $this->_skipMissingConfig();
     $client = new Horde_Http_Client(array('request' => new self::$_requestClass()));
     $response = $client->get('http://' . $this->_server . '/doesntexist');
     $content = $response->getBody();
     $this->assertGreaterThan(0, strlen($content));
 }
Esempio n. 4
0
 public function testBug12991()
 {
     $this->_skipMissingConfig();
     $client = new Horde_Http_Client(array('request' => new Horde_Http_Request_Fopen()));
     $response = $client->get($this->_server . '/doesntexist');
     $body = $response->getBody();
     $this->assertTrue(strlen($body) > 0);
 }
Esempio n. 5
0
 /**
  * Fetch the given remote document into a local target path.
  *
  * @param string            $remote  The remote URI.
  * @param string            $local   The local target path.
  * @param Components_Output $output  The output handler.
  *
  *
  * @return NULL
  */
 public function _fetchDocument($remote, $local, Components_Output $output)
 {
     $this->_client->{'request.timeout'} = 60;
     $content = stream_get_contents($this->_client->get($remote)->getStream());
     $content = preg_replace('#^(\\.\\. _`([^`]*)`: )((?!http://).*)#m', '\\1\\2', $content);
     file_put_contents($this->_docs_origin[1] . '/' . $local, $content);
     $output->ok(sprintf('Fetched remote %s into %s!', $remote, $this->_docs_origin[1] . '/' . $local));
 }
Esempio n. 6
0
 /**
  * Process a Thunderbird autoconfig entry.
  *
  * @param string $domain                    Domain name.
  * @param string $tag                       XML tag to parse.
  * @param array $types                      List of $tag types to process.
  * @param Horde_Mail_Rfc822_Address $email  Username.
  */
 protected function _process($domain, $tag, $types, $email)
 {
     $out = array();
     $urls = array('http://' . urlencode($domain) . '/.well-known/autoconfig/mail/config-v1.1.xml', $this->ispdb . urlencode($domain));
     if (!is_null($email)) {
         array_unshift($urls, 'http://autoconfig.' . urlencode($domain) . '/mail/config-v1.1.xml?emailaddress=' . urlencode($email->bare_address));
     }
     if (is_null($this->http)) {
         $this->http = new Horde_Http_Client();
     }
     foreach ($urls as $url) {
         try {
             $get = $this->http->get($url);
             if ($get->code == 404) {
                 continue;
             }
             try {
                 $xml = new SimpleXMLElement($get->getBody());
             } catch (Exception $e) {
                 // No valid XML; ignore
                 continue;
             }
             $label = strval($xml->emailProvider->displayName);
             foreach ($xml->emailProvider->{$tag} as $val) {
                 if (in_array($val['type'], $types)) {
                     switch ($val['type']) {
                         case 'imap':
                             $ob = new Horde_Mail_Autoconfig_Server_Imap();
                             break;
                         case 'pop3':
                             $ob = new Horde_Mail_Autoconfig_Server_Pop3();
                             break;
                         case 'smtp':
                             $ob = new Horde_Mail_Autoconfig_Server_Msa();
                             break;
                     }
                     $ob->host = strval($val->hostname);
                     $ob->port = intval(strval($val->port));
                     $ob->label = $label;
                     if (strcasecmp($val->socketType, 'SSL') === 0) {
                         $ob->tls = 'tls';
                     }
                     if (!is_null($email) && $email->valid && strlen($val->username)) {
                         $ob->username = str_replace(array('%EMAILADDRESS%', '%EMAILLOCALPART%'), array($email->bare_address, $email->mailbox), $val->username);
                     }
                     $out[] = $ob;
                 }
             }
             if (count($out)) {
                 return $out;
             }
         } catch (Horde_Http_Exception $e) {
             // Not found; ignore.
         }
     }
     return false;
 }
Esempio n. 7
0
 /**
  * Returns the first matching key ID for an email address from a public
  * keyserver.
  *
  * @param string $address  The email address of the PGP key.
  *
  * @return string  The PGP key ID.
  * @throws Horde_Crypt_Exception
  */
 public function getKeyId($address)
 {
     $pubkey = null;
     /* Connect to the public keyserver. */
     $url = $this->_createUrl('/pks/lookup', array('op' => 'index', 'options' => 'mr', 'search' => $address));
     try {
         $output = $this->_http->get($url)->getBody();
     } catch (Horde_Http_Exception $e) {
         throw new Horde_Crypt_Exception($e);
     }
     if (strpos($output, '-----BEGIN PGP PUBLIC KEY BLOCK') !== false) {
         $pubkey = $output;
     } elseif (strpos($output, 'pub:') !== false) {
         $output = explode("\n", $output);
         $keyids = $keyuids = array();
         $curid = null;
         foreach ($output as $line) {
             if (substr($line, 0, 4) == 'pub:') {
                 $line = explode(':', $line);
                 /* Ignore invalid lines and expired keys. */
                 if (count($line) != 7 || !empty($line[5]) && $line[5] <= time()) {
                     continue;
                 }
                 $curid = $line[4];
                 $keyids[$curid] = $line[1];
             } elseif (!is_null($curid) && substr($line, 0, 4) == 'uid:') {
                 preg_match("/<([^>]+)>/", $line, $matches);
                 $keyuids[$curid][] = $matches[1];
             }
         }
         /* Remove keys without a matching UID. */
         foreach ($keyuids as $id => $uids) {
             $match = false;
             foreach ($uids as $uid) {
                 if ($uid == $address) {
                     $match = true;
                     break;
                 }
             }
             if (!$match) {
                 unset($keyids[$id]);
             }
         }
         /* Sort by timestamp to use the newest key. */
         if (count($keyids)) {
             ksort($keyids);
             $pubkey = $this->get(array_pop($keyids));
         }
     }
     if ($pubkey) {
         $sig = $this->_pgp->pgpPacketSignature($pubkey, $address);
         if (!empty($sig['keyid']) && (empty($sig['public_key']['expires']) || $sig['public_key']['expires'] > time())) {
             return substr($this->_pgp->getKeyIDString($sig['keyid']), 2);
         }
     }
     throw new Horde_Crypt_Exception(Horde_Crypt_Translation::t("Could not obtain public key from the keyserver."));
 }
Esempio n. 8
0
 /**
  * Fetch the provided URL as string.
  *
  * @param string $url The URL.
  *
  * @return string The response as string.
  */
 private function _read($url)
 {
     $response = $this->_client->get($url);
     if ($response->code === 200) {
         return $response->getBody();
     } else {
         return false;
     }
 }
Esempio n. 9
0
 public function testAddArrayResponses()
 {
     $mock = new Horde_Http_Request_Mock();
     $mock->addResponses(array(array('body' => 'A'), array('code' => 404), array('uri' => 'http://example.org'), array('headers' => 'test: TEST')));
     $client = new Horde_Http_Client(array('request' => $mock));
     $this->assertEquals('A', $client->get()->getBody());
     $this->assertEquals(404, $client->get()->code);
     $this->assertEquals('http://example.org', $client->get()->uri);
     $this->assertEquals('TEST', $client->get()->getHeader('test'));
 }
Esempio n. 10
0
 /**
  * Fetch remote data.
  *
  * @param Horde_Controller_Response  $response The response handler.
  *
  * @return NULL
  */
 public function _passThrough(Horde_Controller_Response $response)
 {
     $url = $this->getUrlWithCredentials($this->_user->getPrimaryId(), $this->_user->getPassword());
     $origin = $this->_client->get($url);
     if ($origin->code !== 200) {
         $url = $this->getUrlWithCredentials($this->_user, 'XXX');
         throw new Horde_Kolab_FreeBusy_Exception_Unauthorized(sprintf('Unable to read free/busy information from %s', $url));
     }
     $response->setHeader('X-Redirect-To', $url);
     $response->setBody($origin->getStream());
 }
Esempio n. 11
0
File: Base.php Progetto: horde/horde
 /**
  * Imports a URL.
  *
  * @since 2.1.0
  */
 public function importUrl($url, $header = false)
 {
     if (!isset($this->_http)) {
         throw new LogicException('Missing http parameter.');
     }
     try {
         $response = $this->_http->get($url);
         if ($response->code != 200) {
             throw new Horde_Data_Exception(sprintf(Horde_Data_Translation::t("URL %s not found"), $url));
         }
         $data = $response->getBody();
     } catch (Horde_Http_Exception $e) {
         throw new Horde_Data_Exception($e);
     }
     return $this->importData($data, $header);
 }
Esempio n. 12
0
File: Owa.php Progetto: horde/horde
 /**
  * Lists all events in the given time range.
  *
  * @param Horde_Date $startDate Start of range date object.
  * @param Horde_Date $endDate   End of range data object.
  *
  * @return array Events in the given time range.
  *
  * @throws Horde_Kolab_FreeBusy_Exception If retrieving the events failed.
  */
 public function listEvents(Horde_Date $startDate, Horde_Date $endDate)
 {
     $url = $this->_params['url'] . '/public/?cmd=freebusy' . '&start=' . $startDate->format('c') . '&end=' . $endDate->format('c') . '&interval=' . $this->_params['interval'] . '&u=SMTP:' . $this->_owner->getOwner();
     $response = $this->_client->get($url, array('User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)'));
     if ($response->code !== 200) {
         throw new Horde_Kolab_FreeBusy_Exception_NotFound(sprintf('Unable to fetch free/busy information from %s', $url));
     }
     $owa = new Horde_Kolab_FreeBusy_Freebusy_Helper_Owa($response->getStream());
     $result = $owa->convert($startDate, $endDate, $this->_params['interval']);
     if (!isset($result[$this->_owner->getOwner()])) {
         return array();
     }
     $events = array();
     foreach ($result[$this->_owner->getOwner()] as $item) {
         $events[] = new Horde_Kolab_FreeBusy_Object_Event($item);
     }
     return $events;
 }
Esempio n. 13
0
 /**
  * Downloads a timezone database.
  *
  * @throws Horde_Timezone_Exception if downloading fails.
  */
 protected function _download()
 {
     $url = @parse_url($this->_params['location']);
     if (!isset($url['scheme'])) {
         throw new Horde_Timezone_Exception('"location" parameter is missing an URL scheme.');
     }
     if (!in_array($url['scheme'], array('http', 'ftp', 'file'))) {
         throw new Horde_Timezone_Exception(sprintf('Unsupported URL scheme "%s"', $url['scheme']));
     }
     if ($url['scheme'] == 'http') {
         if (isset($this->_params['client'])) {
             $client = $this->_params['client'];
         } else {
             $client = new Horde_Http_Client();
         }
         $response = $client->get($this->_params['location']);
         $this->_tmpfile = Horde_Util::getTempFile('', true, isset($this->_params['temp']) ? $this->_params['temp'] : '');
         stream_copy_to_stream($response->getStream(), fopen($this->_tmpfile, 'w'));
         return;
     }
     if ($url['scheme'] == 'ftp') {
         try {
             $vfs = new Horde_Vfs_Ftp(array('hostspec' => $url['host'], 'username' => 'anonymous', 'password' => 'anonymous', 'pasv' => true));
             $this->_tmpfile = $vfs->readFile(dirname($url['path']), basename($url['path']));
         } catch (Horde_Vfs_Exception $e) {
             throw new Horde_Timezone_Exception($e);
         }
     } else {
         $this->_tmpfile = $this->_params['location'];
         if (!is_readable($this->_tmpfile)) {
             $e = new Horde_Timezone_Exception(sprintf('Unable to open file %s.', $this->_params['location']));
             if (isset($php_errormsg)) {
                 $e->details = $php_errormsg;
             }
             throw $e;
         }
     }
 }
Esempio n. 14
0
 /**
  * Check if the library has a CI job.
  *
  * @return boolean True if a CI job is defined.
  */
 protected function _hasCi()
 {
     if ($this->getChannel() != 'pear.horde.org') {
         return false;
     }
     $client = new Horde_Http_Client(array('request.timeout' => 15));
     try {
         $response = $client->get('http://ci.horde.org/job/' . str_replace('Horde_', '', $this->getName() . '/api/json'));
     } catch (Horde_Http_Exception $e) {
         return false;
     }
     return $response->code != 404;
 }
Esempio n. 15
0
 /**
  * Fetch the avatar image.
  *
  * @param string $mail  The mail address.
  * @param mixed $opts   Additional options. See getAvatarUrl().
  *
  * @return resource  The image as stream resource, or null if the server
  *                   returned an error.
  */
 public function fetchAvatar($mail, $opts = array())
 {
     $get = $this->_client->get($this->getAvatarUrl($mail, $opts));
     return $get->code == 404 ? null : $get->getStream();
 }
Esempio n. 16
0
File: Feed.php Progetto: horde/horde
 /**
  * Read a feed located at $uri
  *
  * @param string $uri The URI to fetch the feed from.
  * @param Horde_Http_Client $httpclient The HTTP client to use.
  *
  * @throws Horde_Feed_Exception
  *
  * @return Horde_Feed_Base
  */
 public static function readUri($uri, Horde_Http_Client $httpclient = null)
 {
     if (is_null($httpclient)) {
         $httpclient = new Horde_Http_Client();
     }
     try {
         $response = $httpclient->get($uri);
     } catch (Horde_Http_Exception $e) {
         throw new Horde_Feed_Exception('Error reading feed: ' . $e->getMessage());
     }
     if ($response->code != 200) {
         throw new Horde_Feed_Exception('Unable to read feed, got response code ' . $response->code);
     }
     $feed = $response->getBody();
     return self::read($feed, $uri);
 }
Esempio n. 17
0
<?php

/**
 * Basic example for fetching a page with Horde_Http_Client
 *
 * Copyright 2007-2015 Horde LLC (http://www.horde.org/)
 *
 * @author   Chuck Hagenbuch <*****@*****.**>
 * @license  http://www.horde.org/licenses/bsd BSD
 * @category Horde
 * @package  Http
 */
require 'Horde/Autoloader/Default.php';
$client = new Horde_Http_Client();
$response = $client->get('http://www.example.com/');
var_dump($response);
echo $response->getBody();
Esempio n. 18
0
 /**
  * Returns the first matching key for an email address from a public
  * keyserver.
  *
  * @param string $address  The email address to search for.
  *
  * @return Horde_Pgp_Element_PublicKey  The PGP public key.
  * @throws Horde_Pgp_Exception
  */
 public function getKeyByEmail($address)
 {
     $output = null;
     /* Connect to the public keyserver. */
     $url = $this->_createUrl('/pks/lookup', array('op' => 'index', 'options' => 'mr', 'search' => $address));
     // Some keyservers are broken, third time's a charm.
     for ($i = 0; $i < 3; $i++) {
         try {
             $response = $this->_http->get($url);
             // Some keyservers return HTML, try again.
             if (strpos($response->getHeader('Content-Type'), 'text/plain') !== 0) {
                 continue;
             }
             $output = ltrim($response->getBody());
         } catch (Horde_Http_Exception $e) {
             throw new Horde_Pgp_Exception($e);
         }
     }
     if (!$output) {
         throw new Horde_Pgp_Exception(Horde_Pgp_Translation::t("Could not obtain public key from the keyserver."));
     }
     if (strpos($output, '-----BEGIN PGP PUBLIC KEY BLOCK') !== false) {
         return Horde_Pgp_Element_PublicKey::create($output);
     } elseif (strpos($output, 'pub:') !== false) {
         $output = explode("\n", $output);
         $keyids = $keyuids = array();
         $curid = null;
         foreach ($output as $line) {
             if (substr($line, 0, 4) == 'pub:') {
                 $line = explode(':', $line);
                 /* Ignore invalid lines and expired keys. */
                 if (count($line) != 7 || !empty($line[5]) && $line[5] <= time()) {
                     continue;
                 }
                 $curid = $line[4];
                 $keyids[$curid] = $line[1];
             } elseif (!is_null($curid) && substr($line, 0, 4) == 'uid:') {
                 preg_match("/<([^>]+)>/", $line, $matches);
                 $keyuids[$curid][] = $matches[1];
             }
         }
         /* Remove keys without a matching UID. */
         foreach ($keyuids as $id => $uids) {
             $match = false;
             foreach ($uids as $uid) {
                 if ($uid == $address) {
                     $match = true;
                     break;
                 }
             }
             if (!$match) {
                 unset($keyids[$id]);
             }
         }
         /* Sort by timestamp to use the newest key. */
         if (count($keyids)) {
             ksort($keyids);
             return $this->get(array_pop($keyids));
         }
     }
     throw new Horde_Pgp_Exception(Horde_Pgp_Translation::t("Could not obtain public key from the keyserver."));
 }