1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5/**
6 * Common class for Digg API endpoints
7 *
8 * PHP version 5.1.0+
9 *
10 * LICENSE: This source file is subject to version 3.0 of the PHP license
11 * that is available through the world-wide-web at the following URI:
12 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
13 * the PHP License and are unable to obtain it through the web, please
14 * send a note to license@php.net so we can mail you a copy immediately.
15 *
16 * @category    Services
17 * @package     Services_Digg
18 * @author      Joe Stump <joe@joestump.net>
19 * @copyright   1997-2007 The PHP Group
20 * @license     http://www.php.net/license/3_0.txt  PHP License 3.0
21 * @version     CVS: $Id:$
22 * @link        http://pear.php.net/package/Services_Digg
23 */
24
25require_once 'Services/Digg/Response.php';
26
27/**
28 * Services_Digg_Common
29 *
30 * @category    Services
31 * @package     Services_Digg
32 * @author      Joe Stump <joe@joestump.net>
33 */
34abstract class Services_Digg_Common
35{
36
37    /**
38     * Last API URI called
39     *
40     * @access      public
41     * @var         string      $lastCall
42     */
43    public $lastCall = '';
44
45    /**
46     * Raw response of last API call
47     *
48     * @access      public
49     * @var         string      $lastResponse
50     * @see         Services_Digg_Common::$lastCall
51     */
52    public $lastResponse = '';
53
54    /**
55     * Send a request to the API
56     *
57     * @access      public
58     * @param       string      $endPoint       Endpoint of API call
59     * @param       array       $params         GET arguments of API call
60     * @return      mixed
61     */
62    protected function sendRequest($endPoint, $params = array())
63    {
64        $uri = Services_Digg::$uri . $endPoint;
65        if (!isset($params['type'])) {
66            $params['type'] = 'php';
67        }
68
69        $params['appkey'] = Services_Digg::$appKey;
70        $sets = array();
71        foreach ($params as $key => $val) {
72            $sets[] = $key . '=' . urlencode($val);
73        }
74
75        $uri .= '?' . implode('&', $sets);
76        $this->lastCall = $uri;
77
78        $ch = curl_init();
79        curl_setopt($ch, CURLOPT_URL, $uri);
80        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
81        curl_setopt($ch, CURLOPT_USERAGENT, 'Services_Digg (' . Services_Digg::$appKey . ')');
82        $this->lastResponse = curl_exec($ch);
83
84        if ($this->lastResponse === false) {
85            throw new Services_Digg_Exception('Curl error: ' . curl_error($ch), curl_errno($ch));
86        }
87
88        curl_close($ch);
89
90        $response = Services_Digg_Response::factory(
91            $params['type'],
92            $this->lastResponse
93        );
94
95        try {
96            return $response->parse();
97        } catch (Services_Digg_Response_Exception $e) {
98            throw new Services_Digg_Exception($e->getMessage(), $e->getCode(), $this->lastCall, $this->lastResponse);
99        }
100    }
101}
102
103
104?>
105