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