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