/** * Send a request to the API * * @access public * @param string $endPoint Endpoint of API call * @param array $params GET arguments of API call * @return mixed */ protected function sendRequest($endPoint, $params = array()) { $uri = Services_Digg::$uri . $endPoint; if (!isset($params['type'])) { $params['type'] = 'php'; } $params['appkey'] = Services_Digg::$appKey; $sets = array(); foreach ($params as $key => $val) { $sets[] = $key . '=' . urlencode($val); } $uri .= '?' . implode('&', $sets); $this->lastCall = $uri; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $uri); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Services_Digg (' . Services_Digg::$appKey . ')'); $this->lastResponse = curl_exec($ch); curl_close($ch); $response = Services_Digg_Response::factory($params['type'], $this->lastResponse); try { return $response->parse(); } catch (Services_Digg_Response_Exception $e) { throw new Services_Digg_Exception($e->getMessage(), $e->getCode(), $this->lastCall, $this->lastResponse); } }
/** * 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. * * @access public * @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]; } $res = $b = ''; while ($b = socket_read($this->sockets[$name], 8096)) { $res .= $b; } 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 // full response body. $parts = explode("\r\n\r\n", $res); array_shift($parts); $php = implode("\r\n\r\n", $parts); try { $response = Services_Digg_Response::factory('php', $php); $this->results[$name] = $response->parse(); } catch (Services_Digg_Response_Exception $e) { $this->results[$name] = new Services_Digg_Exception($e->getMessage(), $e->getCode(), $this->urls[$name], $php); } return $this->results[$name]; }