post() public method

Sends a POST request.
public post ( string $uri = null, array | string $data = null, array $headers = [] ) : Horde_Http_Response_Base
$uri string Request URI.
$data array | string Data fields or data body.
$headers array Additional request headers.
return Horde_Http_Response_Base
Esempio n. 1
0
 /**
  * Push content to the recipient.
  *
  * @param Horde_Push $content The content element.
  * @param array      $options Additional options.
  *
  * @return NULL
  */
 public function push(Horde_Push $content, $options = array())
 {
     $entry = new Horde_Feed_Entry_Atom(null, $this->_client);
     $types = $content->getMimeTypes();
     if (isset($types['text/html'])) {
         $body = $content->getStringContent($types['text/html'][0]);
     } else {
         if (isset($types['text/plain'])) {
             $body = $content->getStringContent($types['text/plain'][0]);
         } else {
             $body = '';
         }
     }
     /* Give the entry its initial values. */
     $entry->{'atom:title'} = $content->getSummary();
     $entry->{'atom:title'}['type'] = 'text';
     $entry->{'atom:content'} = $body;
     $entry->{'atom:content'}['type'] = 'text';
     if (!empty($options['pretend'])) {
         return sprintf("Would push \n\n%s\n\n to %s.", (string) $entry, $this->_params['url']);
     }
     /* Authenticate. */
     $response = $this->_client->post('https://www.google.com/accounts/ClientLogin', 'accountType=GOOGLE&service=blogger&source=horde-push&Email=' . $this->_params['username'] . '&Passwd=' . $this->_params['password'], array('Content-type', 'application/x-www-form-urlencoded'));
     if ($response->code !== 200) {
         throw new Horde_Push_Exception('Expected response code 200, got ' . $response->code);
     }
     $auth = null;
     foreach (explode("\n", $response->getBody()) as $line) {
         $param = explode('=', $line);
         if ($param[0] == 'Auth') {
             $auth = $param[1];
         }
     }
     if (empty($auth)) {
         throw new Horde_Push_Exception('Missing authentication token in the response!');
     }
     /* Do the initial post. */
     try {
         $entry->save($this->_params['url'], array('Authorization' => 'GoogleLogin auth=' . $auth));
         $reference = $entry->link('alternate');
         if (!empty($reference)) {
             $content->addReference($reference);
         }
     } catch (Horde_Exception $e) {
         throw new Horde_Push_Exception($e);
     }
     return sprintf('Pushed blog entry to %s.', $this->_params['url']);
 }
Esempio n. 2
0
 /**
  * Sends a PGP public key to a public keyserver.
  *
  * @param mixed $key  The PGP public key.
  *
  * @throws Horde_Pgp_Exception
  */
 public function put($key)
 {
     $key = Horde_Pgp_Element_PublicKey::create($key);
     /* See if the public key already exists on the keyserver. */
     try {
         $this->get($key->id);
         throw new Horde_Pgp_Exception(Horde_Pgp_Translation::t("Key already exists on the public keyserver."));
     } catch (Horde_Pgp_Exception $e) {
     }
     $pubkey = 'keytext=' . urlencode(trim(strval($key)));
     try {
         $this->_http->post($this->_createUrl('/pks/add'), $pubkey, array('User-Agent: Horde Application Framework', 'Content-Type: application/x-www-form-urlencoded', 'Content-Length: ' . strlen($pubkey), 'Connection: close'));
     } catch (Horde_Http_Exception $e) {
         throw new Horde_Pgp_Exception($e);
     }
 }
Esempio n. 3
0
 /**
  * Sends a PGP public key to a public keyserver.
  *
  * @param string $pubkey  The PGP public key
  *
  * @throws Horde_Crypt_Exception
  */
 public function put($pubkey)
 {
     /* Get the key ID of the public key. */
     $info = $this->_pgp->pgpPacketInformation($pubkey);
     /* See if the public key already exists on the keyserver. */
     try {
         $this->get($info['keyid']);
     } catch (Horde_Crypt_Exception $e) {
         $pubkey = 'keytext=' . urlencode(rtrim($pubkey));
         try {
             $this->_http->post($this->_createUrl('/pks/add'), $pubkey, array('User-Agent: Horde Application Framework', 'Content-Type: application/x-www-form-urlencoded', 'Content-Length: ' . strlen($pubkey), 'Connection: close'));
         } catch (Horde_Http_Exception $e) {
             throw new Horde_Crypt_Exception($e);
         }
     }
     throw new Horde_Crypt_Exception(Horde_Crypt_Translation::t("Key already exists on the public keyserver."));
 }
Esempio n. 4
0
 /**
  * Obtain an access token from a request token
  *
  * @param Horde_Oauth_Token $token Open auth token containing the oauth_token
  *                                 returned from provider after authorization
  *                                 and the token secret returned with the
  *                                 original request token.
  * @param array $params           Any additional parameters for this request
  *
  * @return unknown_type
  */
 public function getAccessToken($token, $params = array())
 {
     $params['oauth_consumer_key'] = $this->key;
     $params['oauth_token'] = $token->key;
     $request = new Horde_Oauth_Request($this->accessTokenUrl, $params);
     $request->sign($this->signatureMethod, $this, $token);
     $client = new Horde_Http_Client();
     try {
         $response = $client->post($this->accessTokenUrl, $request->buildHttpQuery());
     } catch (Horde_Http_Exception $e) {
         throw new Horde_Oauth_Exception($e->getMessage());
     }
     return Horde_Oauth_Token::fromString($response->getBody());
 }
Esempio n. 5
0
 /**
  * Builds an XMLRPC request and sends it to the XMLRPC server.
  *
  * This statically called method is actually the XMLRPC client.
  *
  * @param string|Horde_Url $url      The path to the XMLRPC server on the
  *                                   called host.
  * @param string $method             The method to call.
  * @param Horde_Http_Client $client  The transport client
  * @param array $params              A hash containing any necessary
  *                                   parameters for the method call.
  *
  * @return mixed  The returned result from the method.
  * @throws Horde_Rpc_Exception
  */
 public static function request($url, $method, $client, $params = null)
 {
     $headers = array('User-Agent' => 'Horde RPC client', 'Content-Type' => 'text/xml');
     try {
         $result = $client->post($url, xmlrpc_encode_request($method, $params), $headers);
     } catch (Horde_Http_Exception $e) {
         throw new Horde_Rpc_Exception($e);
     }
     if ($result->code != 200) {
         throw new Horde_Rpc_Exception('Request couldn\'t be answered. Returned errorcode: "' . $result->code);
     } elseif (strpos($result->getBody(), '<?xml') === false) {
         throw new Horde_Rpc_Exception("No valid XML data returned:\n" . $result->getBody());
     } else {
         $response = @xmlrpc_decode(substr($result->getBody(), strpos($result->getBody(), '<?xml')));
         if (is_array($response) && isset($response['faultString'])) {
             throw new Horde_Rpc_Exception($response['faultString']);
         } elseif (is_array($response) && isset($response[0]) && is_array($response[0]) && isset($response[0]['faultString'])) {
             throw new Horde_Rpc_Exception($response[0]['faultString']);
         }
         return $response;
     }
 }
Esempio n. 6
0
/**
 * Example of posting a new Atom entry with Horde_Feed.
 *
 * @package Feed
 */
/* Parameters */
$blogUri = 'http://www.blogger.com/feeds/YOURBLOGID/posts/default';
$username = '******';
$password = '******';
/* Get a Horde framework include_path set up. */
require 'Horde/Autoloader/Default.php';
$httpClient = new Horde_Http_Client();
/* Authenticate. */
try {
    $response = $httpClient->post('https://www.google.com/accounts/ClientLogin', 'accountType=GOOGLE&service=blogger&source=horde-feed-blogger-example-1&Email=' . $username . '&Passwd=' . $password, array('Content-type', 'application/x-www-form-urlencoded'));
    if ($response->code !== 200) {
        throw new Horde_Feed_Exception('Expected response code 200, got ' . $response->code);
    }
} catch (Horde_Feed_Exception $e) {
    die('An error occurred authenticating: ' . $e->getMessage() . "\n");
}
$auth = null;
foreach (explode("\n", $response->getBody()) as $line) {
    $param = explode('=', $line);
    if ($param[0] == 'Auth') {
        $auth = $param[1];
    }
}
if (empty($auth)) {
    throw new Horde_Feed_Exception('Missing authentication token in the response!');
Esempio n. 7
0
 /**
  * Builds an JSON-RPC request and sends it to the server.
  *
  * This statically called method is actually the JSON-RPC client.
  *
  * @param string|Horde_Url $url  The path to the JSON-RPC server on the
  *                               called host.
  * @param string $method         The method to call.
  * @param Horde_Http_Client $client
  * @param array $params          A hash containing any necessary parameters
  *                               for the method call.
  *
  * @return mixed  The returned result from the method.
  * @throws Horde_Rpc_Exception
  */
 public static function request($url, $method, $client, $params = null)
 {
     $headers = array('User-Agent' => 'Horde RPC client', 'Accept' => 'application/json', 'Content-Type' => 'application/json');
     $data = array('version' => '1.1', 'method' => $method);
     if (!empty($params)) {
         $data['params'] = $params;
     }
     $data = Horde_Serialize::serialize($data, Horde_Serialize::JSON);
     try {
         $result = $client->post($url, $data, $headers);
     } catch (Horde_Http_Exception $e) {
         throw new Horde_Rpc_Exception($e->getMessage());
     }
     if ($result->code == 500) {
         $response = Horde_Serialize::unserialize($result->getBody(), Horde_Serialize::JSON);
         if (is_a($response, 'stdClass') && isset($response->error) && is_a($response->error, 'stdClass') && isset($response->error->name) && $response->error->name == 'JSONRPCError') {
             throw new Horde_Rpc_Exception($response->error->message);
             /* @todo: Include more information if we have an Exception that can handle this.
                return PEAR::raiseError($response->error->message,
                                        $response->error->code,
                                        null, null,
                                        isset($response->error->error) ? $response->error->error : null);
                */
         }
         throw new Horde_Rpc_Exception($http->getResponseBody());
     } elseif ($result->code != 200) {
         throw new Horde_Rpc_Exception('Request couldn\'t be answered. Returned errorcode: "' . $result->code);
     }
     return Horde_Serialize::unserialize($result->getBody(), Horde_Serialize::JSON);
 }