]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-http-ixr-client.php
Wordpress 3.1.2-scripts
[autoinstalls/wordpress.git] / wp-includes / class-wp-http-ixr-client.php
1 <?php
2 /**
3  * WP_HTTP_IXR_Client
4  *
5  * @package WordPress
6  * @since 3.1.0
7  *
8  */
9 class WP_HTTP_IXR_Client extends IXR_Client {
10         function WP_HTTP_IXR_Client($server, $path = false, $port = 80, $timeout = 15) {
11                 if ( ! $path ) {
12                         // Assume we have been given a URL instead
13                         $bits = parse_url($server);
14                         $this->scheme = $bits['scheme'];
15                         $this->server = $bits['host'];
16                         $this->port = isset($bits['port']) ? $bits['port'] : 80;
17                         $this->path = !empty($bits['path']) ? $bits['path'] : '/';
18
19                         // Make absolutely sure we have a path
20                         if ( ! $this->path )
21                                 $this->path = '/';
22                 } else {
23                         $this->scheme = 'http';
24                         $this->server = $server;
25                         $this->path = $path;
26                         $this->port = $port;
27                 }
28                 $this->useragent = 'The Incutio XML-RPC PHP Library';
29                 $this->timeout = $timeout;
30         }
31
32         function query() {
33                 $args = func_get_args();
34                 $method = array_shift($args);
35                 $request = new IXR_Request($method, $args);
36                 $xml = $request->getXml();
37
38                 $url = $this->scheme . '://' . $this->server . ':' . $this->port . $this->path;
39                 $args = array(
40                         'headers'    => array('Content-Type' => 'text/xml'),
41                         'user-agent' => $this->useragent,
42                         'body'       => $xml,
43                 );
44
45                 // Merge Custom headers ala #8145
46                 foreach ( $this->headers as $header => $value )
47                         $args['headers'][$header] = $value;
48
49                 if ( $this->timeout !== false )
50                         $args['timeout'] = $this->timeout;
51
52                 // Now send the request
53                 if ( $this->debug )
54                         echo '<pre class="ixr_request">' . htmlspecialchars($xml) . "\n</pre>\n\n";
55
56                 $response = wp_remote_post($url, $args);
57
58                 if ( is_wp_error($response) ) {
59                         $errno    = $response->get_error_code();
60                         $errorstr = $response->get_error_message();
61                         $this->error = new IXR_Error(-32300, "transport error: $errno $errorstr");
62                         return false;
63                 }
64
65                 if ( $response['response']['code'] != 200 ) {
66                         $this->error = new IXR_Error(-32301, "transport error - HTTP status code was not 200 ({$response['response']['code']})");
67                         return false;
68                 }
69
70                 if ( $this->debug )
71                         echo '<pre class="ixr_response">' . htmlspecialchars($response['body']) . "\n</pre>\n\n";
72
73                 // Now parse what we've got back
74                 $this->message = new IXR_Message( $response['body'] );
75                 if ( ! $this->message->parse() ) {
76                         // XML error
77                         $this->error = new IXR_Error(-32700, 'parse error. not well formed');
78                         return false;
79                 }
80
81                 // Is the message a fault?
82                 if ( $this->message->messageType == 'fault' ) {
83                         $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString);
84                         return false;
85                 }
86
87                 // Message must be OK
88                 return true;
89         }
90 }
91 ?>