2010-10-28 18:13:24 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WP_HTTP_IXR_Client
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @since 3.1.0
|
|
|
|
*/
|
2010-12-11 00:51:49 -05:00
|
|
|
class WP_HTTP_IXR_Client extends IXR_Client {
|
2014-11-02 21:32:22 -05:00
|
|
|
public $scheme;
|
|
|
|
/**
|
|
|
|
* @var IXR_Error
|
|
|
|
*/
|
|
|
|
public $error;
|
2011-04-29 16:05:12 -04:00
|
|
|
|
2014-11-30 20:34:24 -05:00
|
|
|
/**
|
|
|
|
* @param string $server
|
|
|
|
* @param string|bool $path
|
|
|
|
* @param int|bool $port
|
|
|
|
* @param int $timeout
|
|
|
|
*/
|
2017-11-30 18:11:00 -05:00
|
|
|
public function __construct( $server, $path = false, $port = false, $timeout = 15 ) {
|
2010-12-11 00:51:49 -05:00
|
|
|
if ( ! $path ) {
|
|
|
|
// Assume we have been given a URL instead
|
2017-11-30 18:11:00 -05:00
|
|
|
$bits = parse_url( $server );
|
2010-12-11 00:51:49 -05:00
|
|
|
$this->scheme = $bits['scheme'];
|
|
|
|
$this->server = $bits['host'];
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->port = isset( $bits['port'] ) ? $bits['port'] : $port;
|
|
|
|
$this->path = ! empty( $bits['path'] ) ? $bits['path'] : '/';
|
2010-10-28 18:13:24 -04:00
|
|
|
|
2010-12-11 00:51:49 -05:00
|
|
|
// Make absolutely sure we have a path
|
2014-03-15 01:01:14 -04:00
|
|
|
if ( ! $this->path ) {
|
2010-12-11 00:51:49 -05:00
|
|
|
$this->path = '/';
|
2014-03-15 01:01:14 -04:00
|
|
|
}
|
2014-05-19 01:51:15 -04:00
|
|
|
|
2014-03-15 01:01:14 -04:00
|
|
|
if ( ! empty( $bits['query'] ) ) {
|
|
|
|
$this->path .= '?' . $bits['query'];
|
|
|
|
}
|
2010-12-11 00:51:49 -05:00
|
|
|
} else {
|
|
|
|
$this->scheme = 'http';
|
|
|
|
$this->server = $server;
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->path = $path;
|
|
|
|
$this->port = $port;
|
2010-12-11 00:51:49 -05:00
|
|
|
}
|
|
|
|
$this->useragent = 'The Incutio XML-RPC PHP Library';
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->timeout = $timeout;
|
2010-12-11 00:51:49 -05:00
|
|
|
}
|
2010-10-28 18:13:24 -04:00
|
|
|
|
2015-05-22 02:18:25 -04:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-05-19 01:51:15 -04:00
|
|
|
public function query() {
|
2017-11-30 18:11:00 -05:00
|
|
|
$args = func_get_args();
|
|
|
|
$method = array_shift( $args );
|
|
|
|
$request = new IXR_Request( $method, $args );
|
|
|
|
$xml = $request->getXml();
|
2010-10-28 18:13:24 -04:00
|
|
|
|
2011-05-05 14:57:29 -04:00
|
|
|
$port = $this->port ? ":$this->port" : '';
|
2017-11-30 18:11:00 -05:00
|
|
|
$url = $this->scheme . '://' . $this->server . $port . $this->path;
|
2010-12-11 00:51:49 -05:00
|
|
|
$args = array(
|
2017-11-30 18:11:00 -05:00
|
|
|
'headers' => array( 'Content-Type' => 'text/xml' ),
|
2010-12-11 00:51:49 -05:00
|
|
|
'user-agent' => $this->useragent,
|
|
|
|
'body' => $xml,
|
|
|
|
);
|
2010-11-17 13:47:34 -05:00
|
|
|
|
2010-12-11 00:51:49 -05:00
|
|
|
// Merge Custom headers ala #8145
|
2015-09-14 22:54:24 -04:00
|
|
|
foreach ( $this->headers as $header => $value ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$args['headers'][ $header ] = $value;
|
2015-09-14 22:54:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-22 14:15:28 -04:00
|
|
|
* Filters the headers collection to be sent to the XML-RPC server.
|
2015-09-14 22:54:24 -04:00
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
2018-03-25 15:33:31 -04:00
|
|
|
* @param string[] $headers Associative array of headers to be sent.
|
2015-09-14 22:54:24 -04:00
|
|
|
*/
|
|
|
|
$args['headers'] = apply_filters( 'wp_http_ixr_client_headers', $args['headers'] );
|
2010-11-17 13:47:34 -05:00
|
|
|
|
2015-09-14 22:54:24 -04:00
|
|
|
if ( $this->timeout !== false ) {
|
2010-10-28 18:13:24 -04:00
|
|
|
$args['timeout'] = $this->timeout;
|
2015-09-14 22:54:24 -04:00
|
|
|
}
|
2010-11-17 13:47:34 -05:00
|
|
|
|
2010-12-11 00:51:49 -05:00
|
|
|
// Now send the request
|
2015-09-14 22:54:24 -04:00
|
|
|
if ( $this->debug ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
echo '<pre class="ixr_request">' . htmlspecialchars( $xml ) . "\n</pre>\n\n";
|
2015-09-14 22:54:24 -04:00
|
|
|
}
|
2010-10-28 18:13:24 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
$response = wp_remote_post( $url, $args );
|
2010-10-28 18:13:24 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( is_wp_error( $response ) ) {
|
|
|
|
$errno = $response->get_error_code();
|
|
|
|
$errorstr = $response->get_error_message();
|
|
|
|
$this->error = new IXR_Error( -32300, "transport error: $errno $errorstr" );
|
2010-10-28 18:13:24 -04:00
|
|
|
return false;
|
|
|
|
}
|
2010-11-17 13:47:34 -05:00
|
|
|
|
2011-05-14 15:45:07 -04:00
|
|
|
if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->error = new IXR_Error( -32301, 'transport error - HTTP status code was not 200 (' . wp_remote_retrieve_response_code( $response ) . ')' );
|
2010-12-11 00:51:49 -05:00
|
|
|
return false;
|
|
|
|
}
|
2010-10-28 18:13:24 -04:00
|
|
|
|
2015-09-14 22:54:24 -04:00
|
|
|
if ( $this->debug ) {
|
2011-05-14 15:45:07 -04:00
|
|
|
echo '<pre class="ixr_response">' . htmlspecialchars( wp_remote_retrieve_body( $response ) ) . "\n</pre>\n\n";
|
2015-09-14 22:54:24 -04:00
|
|
|
}
|
2010-10-28 18:13:24 -04:00
|
|
|
|
2010-12-11 00:51:49 -05:00
|
|
|
// Now parse what we've got back
|
2011-05-14 15:45:07 -04:00
|
|
|
$this->message = new IXR_Message( wp_remote_retrieve_body( $response ) );
|
2010-12-11 00:51:49 -05:00
|
|
|
if ( ! $this->message->parse() ) {
|
|
|
|
// XML error
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->error = new IXR_Error( -32700, 'parse error. not well formed' );
|
2010-12-11 00:51:49 -05:00
|
|
|
return false;
|
|
|
|
}
|
2010-10-28 18:13:24 -04:00
|
|
|
|
2010-12-11 00:51:49 -05:00
|
|
|
// Is the message a fault?
|
|
|
|
if ( $this->message->messageType == 'fault' ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$this->error = new IXR_Error( $this->message->faultCode, $this->message->faultString );
|
2010-12-11 00:51:49 -05:00
|
|
|
return false;
|
|
|
|
}
|
2010-10-28 18:13:24 -04:00
|
|
|
|
2010-12-11 00:51:49 -05:00
|
|
|
// Message must be OK
|
|
|
|
return true;
|
|
|
|
}
|
2010-10-28 18:13:24 -04:00
|
|
|
}
|