Source for file Request.php
Documentation is available at Request.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* A class for running parallel requests to the API
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
* @author Joe Stump <joe@joestump.net>
* @copyright 1997-2007 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @link http://pear.php.net/package/Services_Digg
* A class for running parallel requests to the API
* This class allows you to send parallel asynchronous requests to the Digg API
* without locking your script waiting for those requests to return.
* // Make sure your keys are valid PHP variable names so it doesn't throw
* // an error when trying to access the results later on.
* 'popular' => Services_Digg_Request::buildCall('/stories/popular'),
* 'errors' => Services_Digg_Request::buildCall('/errors'),
* 'topics' => Services_Digg_Request::buildCall('/topics')
* // Start the requests. You'll want to run this at the very top of your
* // script so that the parallel requests start working immediately.
* $req = new Services_Digg_Request($endPoints);
* // The calls are asynchronous and ran in parallel so keep on working on
* // other stuff while that runs in the background.
* // Now I need the popular stories
* foreach ($req->popular->stories as $story) {
* echo '<h2>' . $story->title . '</h2>' . "\n";
* Keep in mind that the requests to the API run both in parallel and
* asynchronously in the background. This means that calls to the API are no
* longer blocking and only take as long as the slowest API request.
* @author Joe Stump <joe@joestump.net>
* A socket for each API endpoint
private $sockets = array ();
* Results from each API endpoint
* @see Services_Digg_Request::__get()
private $results = array ();
* The actual calls that are made
* Create an instance of parallel asynchronous requests to the Digg API by
* passing an array of endpoints keyed by a valid PHP variable name.
* @param array $urls Array of endpoints keyed by name
foreach ($urls as $name => $endpoint) {
if (!isset ($parts['port'])) {
$this->initialize ($name, $parts);
* Initialize a request to the API
* Sets up the basic HTTP request so that the API can start churning along
* until we need the actual data. Data is read down and returned via the
* @param string $name Name of request
* @param array $h Host/port, etc. from parse_url()
* @throws Services_Digg_Exception
* @see Services_Digg_Request::__construct()
* @see Services_Digg_Request::__get()
private function initialize ($name, $h)
$this->sockets[$name] = socket_create (AF_INET , SOCK_STREAM , SOL_TCP );
$res = socket_connect ($this->sockets[$name], $h['host'], $h['port']);
if (isset ($h['query'])) {
$request .= '?' . $h['query'];
$init = array ('GET ' . $request . ' HTTP/1.1',
'User-Agent: Services_Digg_Request (' . Services_Digg::$appKey . ')',
foreach ($init as $header) {
socket_write ($this->sockets[$name], $header . "\r\n");
socket_write ($this->sockets[$name], "\r\n");
$this->results[$name] = null;
$this->urls[$name] = 'http://' . $h['host'] . ':' . $h['port'] . $request;
* Read results back in via getter
* When you create your parallel request you need to add keys to the array
* of endpoints, which you'll reference with the getter. If the result has
* not been parsed yet it will be read from the stream and then parsed.
* @param string $name Name of request
* @return mixed The result object or an exception on error
public function __get($name)
if (!is_null($this->results[$name])) {
return $this->results[$name];
while ($b = socket_read ($this->sockets[$name], 8096 )) {
socket_close ($this->sockets[$name]);
// Responses can contain \r\n\r\n elsewhere after the headers so we
// shift off the headers and then implode again on \r\n\r\n to get the
$parts = explode("\r\n\r\n", $res);
$php = implode("\r\n\r\n", $parts);
$this->results[$name] = $response->parse ();
} catch (Services_Digg_Response_Exception $e) {
return $this->results[$name];
* Closes any sockets that might still be open at the end of the script or
* when the variable is overwritten.
foreach ($this->sockets as $socket) {
* Builds a raw call to the API
* @param string $endPoint API endpoint (e.g. /topics)
* @param array $params GET parameters key/value pair
* @return string The RAW response with your API key, etc.
static public function buildCall($endPoint, array $params = array ())
foreach ($params as $key => $val) {
$sets[] = $key . '=' . $val;
return Services_Digg ::$uri . $endPoint . '?' . implode ('&', $sets);
Documentation generated on Fri, 10 Jul 2009 01:30:06 +0000 by phpDocumentor 1.4.2. PEAR Logo Copyright © PHP Group 2004.
|