lesson_topics (); * * foreach ($topics as $topic) { * printf ('

%s

', $topic->name); * * $lessons = $dojo->lesson_list ($topic->id); * * foreach ($lessons as $lesson) { * printf ('%s
', $lesson->url, $lesson->title); * } * } * * ?> * * Author: John Luxford * Copyright: 2008 Dojo Learning * License: LGPL, http://opensource.org/licenses/lgpl-2.1.php * Version: 1.0 * */ class DojoLearning { /** * The URI format for GET requests. * * @access private */ var $_uri = 'http://api.dojolearning.com/rest-action/%s?user=%s&apikey=%s%s'; /** * The URI format for POST requests. * * @access private */ var $_post_uri = 'http://api.dojolearning.com/rest-action/%s'; /** * The body of the POST request. * * @access private */ var $_post_args = 'user=%s&apikey=%s%s'; /** * Username for the account to access. * * @access public */ var $user; /** * The API key associated with the Dojo account. * * @access public */ var $apikey; /** * Error message on request failures. * * @access public */ var $error = false; /** * Error number on request failures. * * @access public */ var $errno = false; /** * Constructor method. * * @param string * @param string * @access public */ function DojoLearning ($user, $apikey) { $this->user = $user; $this->apikey = $apikey; // get the list of methods $this->_methods = $this->_send_and_receive ('', ''); } /** * Method call overload. This allows calling REST actions as client object * methods. * * @param string * @param array * @return array * @access public */ function __call ($method, $args) { $method = str_replace ('_', '.', $method); if (! isset ($this->_methods->{$method})) { $this->error = 'Unknown method'; return false; } $params = ''; if (count ($args) > 0 && is_array ($args[0])) { foreach ($args[0] as $k => $v) { $params .= '&' . $k . '=' . urlencode ($v); } } elseif (count ($args) > 0) { foreach ($args as $k => $v) { if (isset ($this->_methods->{$method}->parameters[$k])) { $params .= '&' . $this->_methods->{$method}->parameters[$k] . '=' . urlencode ($v); } } } return $this->_send_and_receive ($method, $params); } /** * Send, receive and parse the request. * * @param string * @param string * @return array * @access private */ function _send_and_receive ($request, $params) { // determine GET or POST $method = $this->_methods->{$request}->method; if (! $method) { $method = 'GET'; } // build request if ($method == 'GET') { $url = sprintf ( $this->_uri, $request, $this->user, $this->apikey, $params ); } else { $url = sprintf ( $this->_post_uri, $request ); $params = sprintf ( $this->_post_args, $this->user, $this->apikey, $params ); } // fetch response $res = $this->_fetch ($url, $method, $params); if (! $res) { return $res; } // parse response $res = json_decode ($res); return $res; } /** * Fetch the specified URL. * * @param string * @return string * @access private */ function _fetch ($url, $method, $params) { if (! extension_loaded ('curl')) { $this->errno = -1; $this->error = 'This class requires the curl extension'; return false; } $ch = curl_init (); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_MAXREDIRS, 3); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_VERBOSE, 0); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10); if ($method == 'POST') { curl_setopt ($ch, CURLOPT_POST, 1); curl_setopt ($ch, CURLOPT_POSTFIELDS, $params); } $res = curl_exec ($ch); if (! $res) { $this->errno = curl_errno ($ch); $this->error = curl_error ($ch); curl_close ($ch); return false; } curl_close ($ch); return $res; } } ?>