note 99099 added to book.xmlrpc

From: Date: Tue, 27 Jul 2010 12:53:49 +0000
Subject: note 99099 added to book.xmlrpc
Groups: php.notes 
Request: Send a blank email to php-notes+get-171171@lists.php.net to get a copy of this message
Here is also my wrapper with some magic inside =) It has interface very similar to python's xmlrpclib. see example in the bottom of post class PrettyXMLRPCFault { const ConnectionError = 8002; public $code = 0; public $string = ""; public function __construct($code, $string) { $this->code = $code; $this->string = $string; } public function __toString() { return "XML-RPC Fault<".$this->code.": ".$this->string.">"; } } class PrettyXMLRPCCrumb { public $parent = null; protected $name = ""; public function __construct($parent, $name) { $this->parent = $parent; $this->name = $name; } public function __get($name) { return new PrettyXMLRPCCrumb($this, $name); } public function __call($name, $arguments) { $this->parent->__call($this->name.".".$name, $arguments); } } class PrettyXMLRPC extends PrettyXMLRPCCrumb { public $url = ""; public function __construct($url, $backend) { $this->url = $url; $this->backend = $backend; $this->backend->setUrl($url); } public function __call($name, $arguments) { echo $this->backend->call($name, $arguments); } } class PrettyXMLRPCEpiBackend { private $server; private $url; private $user; private $password; public function __construct() { $this->server = xmlrpc_server_create(); } public function __destruct() { xmlrpc_server_destroy($this->server); } public function setUrl($url) { $url = parse_url($url); $scheme = array_key_exists("scheme", $url) ? $url["scheme"] : "http"; $this->user = array_key_exists("user", $url) ? $url["user"] : ""; $this->password = array_key_exists("pass", $url) ? $url["pass"] : ""; $port = array_key_exists("port", $url) ? $url["port"] : ($this->scheme == 'https'?443:80); $this->url = $scheme."://".$url["host"].":".$port .(array_key_exists("path", $url) ? $url["path"] : "") .(array_key_exists("query", $url) ? '?'.$url["query"] : "") .(array_key_exists("fragment", $url) ? "#".$url["fragment"] : ""); } public function call($name, $arguments) { $request = xmlrpc_encode_request($name, $arguments); $headers = array("Content-Type: text/xml"); if ($this->user) { array_push($headers, "Authorization: Basic " .base64_encode($this->user.":".$this->password)); } $header = (version_compare(phpversion(), '5.2.8')) > 0 ? $headers : implode("\r\n", $headers); // allow_self_signed is for my own use. remove it for more safety $context = stream_context_create(array('http' => array( 'method' => "POST", 'header' => $header, 'content' => $request, 'user_agent' => 'PrettyXMLRPC', 'allow_self_signed' => true ))); $file = @file_get_contents($this->url, false, $context); if ($file === false) { $le = error_get_last(); return new PrettyXMLRPCFault(PrettyXMLRPCFault::ConnectionError, $le["message"]); } else { $response = xmlrpc_decode($file); if (is_array($response) && xmlrpc_is_fault($response)) { return new PrettyXMLRPCFault($response["faultCode"], $response["faultString"]); } else { return $response; } } } } $r = new PrettyXMLRPC("https://guest:guest@127.0.0.1:1234", new PrettyXMLRPCEpiBackend); $r->people->programmers->php->notifyAll("This message is sent by PrettyXMLRPC php class."); ---- Server IP: 69.147.83.197 Probable Submitter: 212.49.121.164 ---- Manual Page -- http://www.php.net/manual/en/book.xmlrpc.php Edit -- https://master.php.net/note/edit/99099 Del: integrated -- https://master.php.net/note/delete/99099/integrated Del: useless -- https://master.php.net/note/delete/99099/useless Del: bad code -- https://master.php.net/note/delete/99099/bad+code Del: spam -- https://master.php.net/note/delete/99099/spam Del: non-english -- https://master.php.net/note/delete/99099/non-english Del: in docs -- https://master.php.net/note/delete/99099/in+docs Del: other reasons-- https://master.php.net/note/delete/99099 Reject -- https://master.php.net/note/reject/99099 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#171171) next »