3 /******************************************************************************
5 * Original file can be found on http://xmlrpc-epi.sourceforge.net/ *
6 * in the module xmlrpc-epi-php v0.51 file samples/utils/utils.php *
8 * The Polytechnique.org TEAM *
10 ******************************************************************************/
13 This file is part of, or distributed with, libXMLRPC - a C library for
14 xml-encoded function calls.
16 Author: Dan Libby (dan@libby.com)
17 Epinions.com may be contacted at feedback@epinions-inc.com
21 Copyright 2001 Epinions, Inc.
23 Subject to the following 3 conditions, Epinions, Inc. permits you, free
24 of charge, to (a) use, copy, distribute, modify, perform and display this
25 software and associated documentation files (the "Software"), and (b)
26 permit others to whom the Software is furnished to do so as well.
28 1) The above copyright notice and this permission notice shall be included
29 without modification in all copies or substantial portions of the
32 2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF
33 ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY
34 IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR
35 PURPOSE OR NONINFRINGEMENT.
37 3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
38 SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT
39 OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING
40 NEGLIGENCE), EVEN IF EPINIONS, INC. IS AWARE OF THE POSSIBILITY OF SUCH
45 /* xmlrpc utilities (xu)
46 * author: Dan Libby (dan@libby.com)
49 /* generic function to call an http server with post method */
50 function xu_query_http_post($request, $host, $uri, $port, $debug,
51 $timeout, $user, $pass, $secure=false
) {
53 if ($host && $uri && $port) {
54 $content_len = strlen($request);
56 $fsockopen = $secure ?
"fsockopen_ssl" : "fsockopen";
58 dbg1("opening socket to host: $host, port: $port, uri: $uri", $debug);
59 $query_fd = $fsockopen($host, $port, $errno, $errstr, 10);
65 $auth = "Authorization: Basic " . base64_encode($user . ":" . $pass) . "\r\n";
69 "POST $uri HTTP/1.0\r\n" .
70 "Host: $host:$port\r\n" .
72 "User-Agent: xmlrpc-epi-php/0.2 (PHP)\r\n" .
73 "Content-Type: text/xml\r\n" .
74 "Content-Length: $content_len\r\n" .
75 "Connection: Close\r\n" .
79 dbg1("sending http request:</h3> <xmp>\n$http_request\n</xmp>", $debug);
81 fputs($query_fd, $http_request, strlen($http_request));
83 dbg1("receiving response...", $debug);
85 $header_parsed = false
;
87 while (!feof($query_fd)) {
88 $line = fgets($query_fd, 4096);
89 if (!$header_parsed) {
90 if ($line === "\r\n" ||
$line === "\n") {
93 dbg2("got header - $line", $debug);
96 $response_buf .= $line;
103 dbg1("socket open failed", $debug);
107 dbg1("missing param(s)", $debug);
110 dbg1("got response:</h3>. <xmp>\n$response_buf\n</xmp>\n", $debug);
112 return $response_buf;
115 function xu_fault_code($code, $string) {
116 return array('faultCode' => $code,
117 'faultString' => $string);
121 function find_and_decode_xml($buf, $debug) {
123 $xml_begin = substr($buf, strpos($buf, "<?xml"));
124 if (strlen($xml_begin)) {
126 $retval = xmlrpc_decode($xml_begin);
129 dbg1("xml start token not found", $debug);
133 dbg1("no data", $debug);
140 * @param params a struct containing 3 or more of these key/val pairs:
141 * @param host remote host (required)
142 * @param uri remote uri (required)
143 * @param port remote port (required)
144 * @param method name of method to call
145 * @param args arguments to send (parameters to remote xmlrpc server)
146 * @param debug debug level (0 none, 1, some, 2 more)
147 * @param timeout timeout in secs. (0 = never)
148 * @param user user name for authentication.
149 * @param pass password for authentication
150 * @param secure secure. wether to use fsockopen_ssl. (requires special php build).
151 * @param output array. xml output options. can be null. details below:
153 * output_type: return data as either php native data types or xml
154 * encoded. ifphp is used, then the other values are ignored. default = xml
155 * verbosity: determine compactness of generated xml. options are
156 * no_white_space, newlines_only, and pretty. default = pretty
157 * escaping: determine how/whether to escape certain characters. 1 or
158 * more values are allowed. If multiple, they need to be specified as
159 * a sub-array. options are: cdata, non-ascii, non-print, and
160 * markup. default = non-ascii | non-print | markup
161 * version: version of xml vocabulary to use. currently, three are
162 * supported: xmlrpc, soap 1.1, and simple. The keyword auto is also
163 * recognized to mean respond in whichever version the request came
164 * in. default = auto (when applicable), xmlrpc
165 * encoding: the encoding that the data is in. Since PHP defaults to
166 * iso-8859-1 you will usually want to use that. Change it if you know
167 * what you are doing. default=iso-8859-1
171 * $output_options = array('output_type' => 'xml',
172 * 'verbosity' => 'pretty',
173 * 'escaping' => array('markup', 'non-ascii', 'non-print'),
174 * 'version' => 'xmlrpc',
175 * 'encoding' => 'utf-8'
179 * $output_options = array('output_type' => 'php');
181 function xu_rpc_http_concise($params) {
182 $output = $host = $uri = $port = $method = $args = $debug = null
;
183 $timeout = $user = $pass = $secure = $debug = null
;
195 $output = array('version' => 'xmlrpc');
199 if ($host && $uri && $port) {
200 $request_xml = xmlrpc_encode_request($method, $args, $output);
201 $response_buf = xu_query_http_post($request_xml, $host, $uri, $port, $debug,
202 $timeout, $user, $pass, $secure);
204 $retval = find_and_decode_xml($response_buf, $debug);
209 /* call an xmlrpc method on a remote http server. legacy support. */
210 function xu_rpc_http($method, $args, $host, $uri="/", $port=80, $debug=false
,
211 $timeout=0, $user=false
, $pass=false
, $secure=false
) {
212 return xu_rpc_http_concise(
220 'timeout' => $timeout,
229 function xu_is_fault($arg) {
230 // xmlrpc extension finally supports this.
231 return is_array($arg) ?
xmlrpc_is_fault($arg) : false
;
234 /* sets some http headers and prints xml */
235 function xu_server_send_http_response($xml) {
236 header("Content-type: text/xml");
237 header("Content-length: " . strlen($xml) );
243 echo "<h3>$msg</h3>"; flush();
245 function dbg1($msg, $debug_level) {
246 if ($debug_level >= 1) {
250 function dbg2($msg, $debug_level) {
251 if ($debug_level >= 2) {