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