/** * 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()); }
/** * 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; } } }
public function filter_send_mail($handled, $mail) { if (!$handled) { $headers = array('Accept: application/json', 'Content-Type: application/json', 'X-Postmark-Server-Token: ' . Options::get('postmark__apikey')); $data = array('To' => $mail['to'], 'subject' => $mail['subject'], 'TextBody' => $mail['message'], 'From' => $mail['headers']['From']); $rr = new RemoteRequest('http://api.postmarkapp.com/email', 'POST'); $rr->set_body(json_encode($data)); $rr->add_headers($headers); try { $rr->execute(); EventLog::log(_t('Send message to %s via Postmark', array($mail['to'])), 'info', 'default', null, array($data, $headers)); Session::notice(var_export($rr->get_response_headers(), 1)); } catch (Exception $e) { EventLog::log(_t('Failed to send message to %s via Postmark', array($mail['to'])), 'err', 'default', null, array($e->getMessage(), $data, $headers)); Session::error('There was a problem sending your message. Please contact the site administrators directly.'); } } return true; }
/** * 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; } }
$rr->__set_processor( $processor ); $rr->set_params( array ( 'query' => 'var', 'another' => 'variable', ) ); $res_get= $rr->execute(); if ( $res_get === TRUE ) { $results[]= array( get_class( $processor ), $rr->get_response_headers(), substr( $rr->get_response_body(), 0 ) ); } else { $results[]= array( get_class( $processor ), $res_get, ); } $rr= new RemoteRequest( 'http://test.habariproject.org/post', 'POST' ); $rr->__set_processor( $processor ); $rr->set_body( 'If you can read this, the test was successful.' ); $res_post= $rr->execute(); if ( $res_post === TRUE ) { $results[]= array( get_class( $processor ), $rr->get_response_headers(), substr( $rr->get_response_body(), 0 ) ); } else { $results[]= array( get_class( $processor ), $res_post, ); } foreach ( $tests as $name => $group ) { print( "<h2>{$name}</h2>\n" ); foreach ( $group as $test ) { $result= eval( 'return (' . $test . ');' ); printf( "<p><strong>%s</strong> == ( %s )</p>\n", bs( $result ), var_export( $test, TRUE ) ); Utils::debug( array_shift( $results ) );
/** * 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(); }
public static function file_issue($user, $repo, $title, $body, $tag = 'bug') { EventLog::log(_t('Filed issue for %s/%s - %s', array($user, $repo, $title)), 'info'); $gitpassword = Options::get('habariupdatebot_password'); $rr = new RemoteRequest("https://*****:*****@api.github.com/repos/{$user}/{$repo}/issues", 'POST'); $rr->set_body('{"title": "' . addslashes($title) . '","body": "' . addslashes($body) . '","labels":["' . addslashes($tag) . '"]}'); $rr->execute(); }
function do_stupid_things_in_global_scope() { /** * Test for the RemoteRequest class. */ include '../htdocs/system/classes/remoterequest.php'; include '../htdocs/system/classes/curlrequestprocessor.php'; include '../htdocs/system/classes/socketrequestprocessor.php'; include '../htdocs/system/classes/utils.php'; include '../htdocs/system/classes/error.php'; error_reporting(E_ALL | E_STRICT); function bs($v) { return $v ? 'TRUE' : 'FALSE'; } $tests_failed = array(); $tests = array('GET http://test.habariproject.org/' => array("\$res"), 'GET http://test.habariproject.org/get' => array("\$res_get"), 'POST http://test.habariproject.org/post' => array("\$res_post")); print "<h1>Running tests</h1>\n"; $processors = array(new CURLRequestProcessor(), new SocketRequestProcessor()); foreach ($processors as $processor) { $rr = new RemoteRequest('http://test.habariproject.org/'); $rr->__set_processor($processor); $res = $rr->execute(); if ($res === TRUE) { $results[] = array(get_class($processor), $rr->get_response_headers(), substr($rr->get_response_body(), 0)); } else { $results[] = array(get_class($processor), $res); } $rr = new RemoteRequest('http://test.habariproject.org/get'); $rr->__set_processor($processor); $rr->set_params(array('query' => 'var', 'another' => 'variable')); $res_get = $rr->execute(); if ($res_get === TRUE) { $results[] = array(get_class($processor), $rr->get_response_headers(), substr($rr->get_response_body(), 0)); } else { $results[] = array(get_class($processor), $res_get); } $rr = new RemoteRequest('http://test.habariproject.org/post', 'POST'); $rr->__set_processor($processor); $rr->set_body('If you can read this, the test was successful.'); $res_post = $rr->execute(); if ($res_post === TRUE) { $results[] = array(get_class($processor), $rr->get_response_headers(), substr($rr->get_response_body(), 0)); } else { $results[] = array(get_class($processor), $res_post); } foreach ($tests as $name => $group) { print "<h2>{$name}</h2>\n"; foreach ($group as $test) { $result = eval('return (' . $test . ');'); printf("<p><strong>%s</strong> == ( %s )</p>\n", bs($result), var_export($test, TRUE)); Utils::debug(array_shift($results)); if (!$result) { $tests_failed[$name][] = $test; } } } } if (count($tests_failed)) { print "<h1>Failed tests</h1>\n"; foreach ($tests_failed as $name => $tests) { print "<h2>{$name}</h2>\n"; foreach ($tests as $test) { print "<p>{$test}</p>\n"; } } } else { print "<h1>All tests successful</h1>\n"; } }