web/wp-includes/class-wp-http-ixr-client.php
changeset 194 32102edaa81b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
       
     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 		} else {
       
    24 			$this->scheme = 'http';
       
    25 			$this->server = $server;
       
    26 			$this->path = $path;
       
    27 			$this->port = $port;
       
    28 		}
       
    29 		$this->useragent = 'The Incutio XML-RPC PHP Library';
       
    30 		$this->timeout = $timeout;
       
    31 	}
       
    32 
       
    33 	function query() {
       
    34 		$args = func_get_args();
       
    35 		$method = array_shift($args);
       
    36 		$request = new IXR_Request($method, $args);
       
    37 		$xml = $request->getXml();
       
    38 
       
    39 		$port = $this->port ? ":$this->port" : '';
       
    40 		$url = $this->scheme . '://' . $this->server . $port . $this->path;
       
    41 		$args = array(
       
    42 			'headers'    => array('Content-Type' => 'text/xml'),
       
    43 			'user-agent' => $this->useragent,
       
    44 			'body'       => $xml,
       
    45 		);
       
    46 
       
    47 		// Merge Custom headers ala #8145
       
    48 		foreach ( $this->headers as $header => $value )
       
    49 			$args['headers'][$header] = $value;
       
    50 
       
    51 		if ( $this->timeout !== false )
       
    52 			$args['timeout'] = $this->timeout;
       
    53 
       
    54 		// Now send the request
       
    55 		if ( $this->debug )
       
    56 			echo '<pre class="ixr_request">' . htmlspecialchars($xml) . "\n</pre>\n\n";
       
    57 
       
    58 		$response = wp_remote_post($url, $args);
       
    59 
       
    60 		if ( is_wp_error($response) ) {
       
    61 			$errno    = $response->get_error_code();
       
    62 			$errorstr = $response->get_error_message();
       
    63 			$this->error = new IXR_Error(-32300, "transport error: $errno $errorstr");
       
    64 			return false;
       
    65 		}
       
    66 
       
    67 		if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
       
    68 			$this->error = new IXR_Error(-32301, 'transport error - HTTP status code was not 200 (' . wp_remote_retrieve_response_code( $response ) . ')');
       
    69 			return false;
       
    70 		}
       
    71 
       
    72 		if ( $this->debug )
       
    73 			echo '<pre class="ixr_response">' . htmlspecialchars( wp_remote_retrieve_body( $response ) ) . "\n</pre>\n\n";
       
    74 
       
    75 		// Now parse what we've got back
       
    76 		$this->message = new IXR_Message( wp_remote_retrieve_body( $response ) );
       
    77 		if ( ! $this->message->parse() ) {
       
    78 			// XML error
       
    79 			$this->error = new IXR_Error(-32700, 'parse error. not well formed');
       
    80 			return false;
       
    81 		}
       
    82 
       
    83 		// Is the message a fault?
       
    84 		if ( $this->message->messageType == 'fault' ) {
       
    85 			$this->error = new IXR_Error($this->message->faultCode, $this->message->faultString);
       
    86 			return false;
       
    87 		}
       
    88 
       
    89 		// Message must be OK
       
    90 		return true;
       
    91 	}
       
    92 }