Services_Akismet
[ class tree: Services_Akismet ] [ index: Services_Akismet ] [ all elements ]

Source for file Stream.php

Documentation is available at Stream.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * Contains a streams-based HTTP client class for the Services_Akismet package
  7.  *
  8.  * PHP version 5
  9.  *
  10.  * LICENSE:
  11.  *
  12.  * Copyright (c) 2008 silverorange
  13.  *
  14.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  15.  * of this software and associated documentation files (the "Software"), to deal
  16.  * in the Software without restriction, including without limitation the rights
  17.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18.  * copies of the Software, and to permit persons to whom the Software is
  19.  * furnished to do so, subject to the following conditions:
  20.  *
  21.  * The above copyright notice and this permission notice shall be included in
  22.  * all copies or substantial portions of the Software.
  23.  *
  24.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  30.  * THE SOFTWARE.
  31.  *
  32.  * @category  Services
  33.  * @package   Services_Akismet
  34.  * @author    Michael Gauthier <mike@silverorange.com>
  35.  * @copyright 2008 silverorange
  36.  * @license   http://www.opensource.org/licenses/mit-license.html MIT License
  37.  * @version   CVS: $Id: Stream.php,v 1.3 2008/04/16 03:10:41 gauthierm Exp $
  38.  * @link      http://pear.php.net/package/Services_Akismet
  39.  */
  40.  
  41. /**
  42.  * PEAR Exception.
  43.  */
  44. require_once 'PEAR/Exception.php';
  45.  
  46. /**
  47.  * Exception thrown when a communications error occurs.
  48.  */
  49. require_once 'Services/Akismet/CommunicationException.php';
  50.  
  51. /**
  52.  * HTTP client interface.
  53.  */
  54. require_once 'Services/Akismet/HttpClient.php';
  55.  
  56. // {{{ class Services_Akismet_HttpClient_Stream
  57.  
  58. /**
  59.  * Streams-based simple HTTP client for accessing the Akismet REST API
  60.  *
  61.  * This streams-based HTTP client requires PHP to support the
  62.  * stream_context_create(), stream_context_set_option() and file_get_contents()
  63.  * functions and requires that the HTTP stream wrapper is enabled.
  64.  *
  65.  * This HTTP client only supports the HTTP POST method since that is all that
  66.  * is needed for the Akismet API.
  67.  *
  68.  * @category  Services
  69.  * @package   Services_Akismet
  70.  * @author    Michael Gauthier <mike@silverorange.com>
  71.  * @copyright 2008 silverorange
  72.  * @license   http://www.opensource.org/licenses/mit-license.html MIT License
  73.  * @link      http://pear.php.net/package/Services_Akismet
  74.  * @link      http://akismet.com/development/api/
  75.  */
  76. {
  77.     // {{{ private properties
  78.  
  79.     /**
  80.      * Akismet API server host name
  81.      *
  82.      * @var string 
  83.      *
  84.      * @see Services_Akismet_HttpClient_Stream::__construct()
  85.      */
  86.     private $_host '';
  87.  
  88.     /**
  89.      * TCP/IP Port on which to connect
  90.      *
  91.      * @var integer 
  92.      *
  93.      * @see Services_Akismet_HttpClient_Stream::__construct()
  94.      */
  95.     private $_port = 80;
  96.  
  97.     /**
  98.      * HTTP user agent string of this HTTP client
  99.      *
  100.      * @var string 
  101.      *
  102.      * @see Services_Akismet_HttpClient_Stream::__construct()
  103.      */
  104.     private $_userAgent '';
  105.  
  106.     /**
  107.      * The stream context used for HTTP connections
  108.      *
  109.      * @var resource 
  110.      *
  111.      * @see http://ca.php.net/manual/en/wrappers.http.php
  112.      * @see Services_Akismet_HttpClient_Stream::__construct()
  113.      * @see Services_Akismet_HttpClient_Stream::post()
  114.      */
  115.     private $_streamContext = null;
  116.  
  117.     // }}}
  118.     // {{{ post()
  119.  
  120.     /**
  121.      * Makes a HTTP POST request on the Akismet API server
  122.      *
  123.      * @param string $path    the resource to post to.
  124.      * @param string $content the data to post.
  125.      * @param string $apiKey  optional. The Wordpress API key to use for the
  126.      *                         request. If not specified, no API key information
  127.      *                         is included in the request. This is used for key
  128.      *                         validation.
  129.      *
  130.      * @return string the content of the HTTP response from the Akismet API
  131.      *                 server.
  132.      *
  133.      * @throws Services_Akismet_CommunicationException if there is an error
  134.      *          reading from the HTTP stream.
  135.      */
  136.     public function post($path$content$apiKey '')
  137.     {
  138.         if (strlen($this->_port== 0{
  139.             $url sprintf('http://%s%s'$this->_host$path);
  140.         else {
  141.             $url sprintf('http://%s:%s%s'$this->_host$this->_port$path);
  142.         }
  143.  
  144.         if (strlen($apiKey> 0{
  145.             $hostHeader $apiKey '.' $this->_host;
  146.         else {
  147.             $hostHeader $this->_host;
  148.         }
  149.  
  150.         $headers = array(
  151.             'Host'         => $hostHeader,
  152.             'Content-type' => 'application/x-www-form-urlencoded; charset=utf-8',
  153.         );
  154.  
  155.         $header '';
  156.         foreach ($headers as $key => $value{
  157.             $header .= $key ': ' $value "\r\n";
  158.         }
  159.  
  160.         $streamOptions = array(
  161.             'http' => array(
  162.                 'header'  => $header,
  163.                 'content' => $content
  164.             )
  165.         );
  166.  
  167.         // set header and post data content on stream context
  168.         $result stream_context_set_option($this->_streamContext,
  169.             $streamOptions);
  170.  
  171.         // read response
  172.         $response @file_get_contents($urlfalse$this->_streamContext);
  173.  
  174.         if ($response === false{
  175.             throw new Services_Akismet_CommunicationException('Error reading ' .
  176.                 'HTTP stream.');
  177.         }
  178.  
  179.         return $response;
  180.     }
  181.  
  182.     // }}}
  183.     // {{{ __construct()
  184.  
  185.     /**
  186.      * Creates a new streams-based HTTP client for accessing the Akismet REST
  187.      * API
  188.      *
  189.      * Instances of this HTTP client must be instantiated using the
  190.      * {@link Services_Akismet_HttpClient::factory()} method.
  191.      *
  192.      * @param string  $host      the Akismet API server host name.
  193.      * @param integer $port      the TCP/IP connection port of this HTTP
  194.      *                            client.
  195.      * @param string  $userAgent the HTTP user agent of this HTTP client.
  196.      *
  197.      * @throws PEAR_Exception if the HTTP streams wrapper is not enabled for
  198.      *          this PHP installation.
  199.      */
  200.     protected function __construct($host$port$userAgent)
  201.     {
  202.         $this->_host      strval($host);
  203.         $this->_port      intval($port);
  204.         $this->_userAgent strval($userAgent);
  205.  
  206.         // make sure we have the HTTP wrapper enabled
  207.         $streamWrappers stream_get_wrappers();
  208.         if (!in_array('http'$streamWrappers)) {
  209.             throw new PEAR_Exception('HTTP streams wrapper is not enabled ' .
  210.                 'for this PHP installation. The streams-based HTTP client ' .
  211.                 'may not be used.');
  212.         }
  213.  
  214.         // create stream context
  215.         $streamOptions        = array('http' => array('method' => 'POST'));
  216.         $this->_streamContext stream_context_create($streamOptions);
  217.     }
  218.  
  219.     // }}}
  220. }
  221.  
  222. // }}}
  223.  
  224. ?>

Documentation generated on Sat, 03 May 2008 17:00:16 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.