/** * Post a status to service * @param string $svcurl Catenation of user server, API endpoints * @param string $notice The new status to post **/ public function post_status($svcurl, $notice, $name, $pw) { $request = new RemoteRequest($svcurl, 'POST'); $request->add_header(array('Authorization' => 'Basic ' . base64_encode("{$name}:{$pw}"))); $request->set_body('source=habari&status=' . urlencode($notice)); $request->execute(); }
/** * Execute the request. Populates result field. */ public function execute() { $rr = new RemoteRequest($this->url, 'POST'); $rr->add_header('Content-Type: text/xml;charset=utf-8'); $rr->set_body($this->request_body); // should throw an error on failure $rr->execute(); // in that case, we should never get here $this->result = xmlrpc_decode($rr->get_response_body()); }
/** * Execute a call to the Picasa Service * * @param $url string The destination url * @param $args array Optional Extra arguments * @param $post_data Optional Post data * @return mixed false on error or xml string */ public function call($url, $args = array(), $post_data = '') { $token = Options::get('picasa_token_' . User::identify()->id); $default_args = array('method' => 'GET'); $args = array_merge($default_args, $args); $request = new RemoteRequest($url, $args['method']); // set authorisation header $request->add_header(array("Authorization" => "AuthSub token=" . $token)); // add extra headers if (isset($args['http_headers'])) { foreach ($args['http_headers'] as $key => $value) { $request->add_header(array($key => $value)); } } if ($post_data != '') { $request->set_body($post_data); } $request->set_timeout(30); // execute request $result = $request->execute(); if (Error::is_error($result)) { return $result; } // get the response if executed if ($request->executed()) { $response = $request->get_response_body(); } if (!$response) { return Error::raise('API call failure', E_USER_WARNING); } // parse the result try { $xml = new SimpleXMLElement($response); return $xml; } catch (Exception $e) { Session::error('Currently unable to connect to the Picasa API.', 'Picasa API'); return false; } }
/** * Allow method overloading for this class. * This method allows any method name to be called on this object. The method * called is the method called via RPC, within the scope defined in $this->scope. * * @param string $fname The function name to call * @param array $args An array of arguments that were called with the function * @return array The result array */ public function __call($fname, $args) { if ($this->scope != '') { $rpc_method = "{$this->scope}.{$fname}"; } else { $rpc_method = $fname; } $rpx = new SimpleXMLElement('<methodCall/>'); $rpx->addChild('methodName', $rpc_method); if (count($args) > 0) { $params = $rpx->addchild('params'); foreach ($args as $arg) { $param = $params->addchild('param'); XMLRPCUtils::encode_arg($param, $arg); } } $request = new RemoteRequest($this->entrypoint, 'POST'); $request->add_header('Content-Type: text/xml;charset=utf-8'); $request->set_body($rpx->asXML()); $request->execute(); if ($request->executed()) { $response = $request->get_response_body(); // @todo this should use the MultiByte class, not directly call mb_string functions $enc = mb_detect_encoding($response); $responseutf8 = mb_convert_encoding($response, 'UTF-8', $enc); try { // @todo this should use libxml_use_internal_errors() instead of trying to hide the PHP warning see the plugin info parsing code for an example $bit = ini_get('error_reporting'); error_reporting($bit && !E_WARNING); $responsexml = new SimpleXMLElement($responseutf8); error_reporting($bit); $tmp = $responsexml->xpath('//params/param/value'); if (!($responsestruct = reset($tmp))) { $tmp = $responsexml->xpath('//fault/value'); if (!($responsestruct = reset($tmp))) { throw new Exception(_t('Invalid XML response.')); } } return XMLRPCUtils::decode_args($responsestruct); } catch (Exception $e) { //Utils::debug( $response, $e ); error_reporting($bit); return false; } } }
/** * Allow method overloading for this class. * This method allows any method name to be called on this object. The method * called is the method called via RPC, within the scope defined in $this->scope. * * @param string $fname The function name to call * @param array $args An array of arguments that were called with the function * @return array The result array */ public function __call($fname, $args) { if ($this->scope != '') { $rpc_method = "{$this->scope}.{$fname}"; } else { $rpc_method = $fname; } $rpx = new SimpleXMLElement('<methodCall/>'); $rpx->addChild('methodName', $rpc_method); if (count($args) > 0) { $params = $rpx->addchild('params'); foreach ($args as $arg) { $param = $params->addchild('param'); XMLRPCUtils::encode_arg($param, $arg); } } $request = new RemoteRequest($this->entrypoint, 'POST'); $request->add_header('Content-Type: text/xml'); $request->set_body($rpx->asXML()); $request->execute(); if ($request->executed()) { $response = $request->get_response_body(); $enc = mb_detect_encoding($response); $responseutf8 = mb_convert_encoding($response, 'UTF-8', $enc); try { $bit = ini_get('error_reporting'); error_reporting($bit && !E_WARNING); $responsexml = new SimpleXMLElement($responseutf8); error_reporting($bit); $tmp = $responsexml->xpath('//params/param/value'); if (!($responsestruct = reset($tmp))) { $tmp = $responsexml->xpath('//fault/value'); if (!($responsestruct = reset($tmp))) { throw new Exception(_t('Invalid XML response.')); } } return XMLRPCUtils::decode_args($responsestruct); } catch (Exception $e) { //Utils::debug($response, $e); error_reporting($bit); return false; } } }
/** * Post a status to Twitter * @param string $tweet The new status to post **/ public function post_status($tweet, $name, $pw) { $request = new RemoteRequest('http://twitter.com/statuses/update.xml', 'POST'); $request->add_header(array('Authorization' => 'Basic ' . base64_encode("{$name}:{$pw}"))); $request->set_body('source=habari&status=' . urlencode($tweet)); $request->execute(); }
/** * PhotozouAPI: photo_album * * @access public * @return object */ public function photo_album() { $request = new RemoteRequest($this->base_url . 'photo_album', 'GET'); $request->add_header('Authorization: Basic ' . rtrim(base64_encode($this->username . ':' . $this->password), '=')); $result = $request->execute(); if ($result !== true) { return false; } $xml = simplexml_load_string($request->get_response_body()); if ($xml['stat'] != 'ok') { return false; } $albums = array(); for ($i = 0; $i < count($xml->info->album); $i++) { $albums[(string) $xml->info->album[$i]->user_id . ':' . (string) $xml->info->album[$i]->album_id] = (string) $xml->info->album[$i]->name; } return $albums; }
/** * Grabs update times from weblogs.com * @todo use update time from weblogs.com instead of gmdate. * @todo parse urls so we search for only '%host.domain.tld/path%' with no http:// */ public function filter_blogroll_update_cron($success) { if (Options::get('blogroll__use_updated')) { $request = new RemoteRequest('http://rpc.weblogs.com/changes.xml', 'GET'); $request->add_header(array('If-Modified-Since', Options::get('blogroll__last_update'))); try { if ($request->execute()) { try { $xml = new \SimpleXMLElement($request->get_response_body()); } catch (\Exception $e) { EventLog::log('Could not parse weblogs.com Changes XML file'); return false; } $atts = $xml->attributes(); $updated = strtotime((string) $atts['updated']); foreach ($xml->weblog as $weblog) { $atts = $weblog->attributes(); $match = array(); $match['url'] = (string) $atts['url']; $match['feedurl'] = (string) $atts['rssUrl']; $update = $updated - (int) $atts['when']; // use LIKE for info matching $posts = DB::get_results('SELECT * FROM {posts} WHERE {posts}.id IN ( SELECT post_id FROM {postinfo} WHERE ( (name = ? AND value LIKE ? ) OR (name = ? AND value LIKE ? ) ) ) AND status = ? AND content_type = ?', array('url', "%{$match['url']}%", 'feedurl', "%{$match['feedurl']}%", Post::status('published'), Post::type(self::CONTENT_TYPE)), 'Post'); if ($posts instanceof Posts && $posts->count() > 0) { foreach ($posts as $post) { $post->updated = HabariDateTime::create($update); $post->update(); EventLog::log("Updated {$post->title} last update time from weblogs.com"); } } } Options::set('blogroll__last_update', gmdate('D, d M Y G:i:s e')); return true; } else { EventLog::log('Could not connect to weblogs.com'); return false; } } catch (\Exception $e) { EventLog::log('Could not connect to weblogs.com (request failed)', 'error', 'default', 'Blogroll', $e); return false; } } return $success; }