remote_request() public static method

Makes an authorized remote request using Jetpack_Signature
public static remote_request ( $args, $body = null ) : array | WP_Error
return array | WP_Error WP HTTP response on success
 function query()
 {
     $args = func_get_args();
     $method = array_shift($args);
     $request = new IXR_Request($method, $args);
     $xml = trim($request->getXml());
     $response = Jetpack_Client::remote_request($this->jetpack_args, $xml);
     if (is_wp_error($response)) {
         $this->error = new IXR_Error(-10520, sprintf('Jetpack: [%s] %s', $response->get_error_code(), $response->get_error_message()));
         return false;
     }
     if (!$response) {
         $this->error = new IXR_Error(-10520, 'Jetpack: Unknown Error');
         return false;
     }
     if (200 != wp_remote_retrieve_response_code($response)) {
         $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200');
         return false;
     }
     $content = wp_remote_retrieve_body($response);
     // Now parse what we've got back
     $this->message = new IXR_Message($content);
     if (!$this->message->parse()) {
         // XML error
         $this->error = new IXR_Error(-32700, 'parse error. not well formed');
         return false;
     }
     // Is the message a fault?
     if ($this->message->messageType == 'fault') {
         $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString);
         return false;
     }
     // Message must be OK
     return true;
 }
Example #2
0
function stats_get_remote_csv($url)
{
    $method = 'GET';
    $timeout = 90;
    $user_id = JETPACK_MASTER_USER;
    $get = Jetpack_Client::remote_request(compact('url', 'method', 'timeout', 'user_id'));
    $get_code = wp_remote_retrieve_response_code($get);
    if (is_wp_error($get) || 2 != intval($get_code / 100) && 304 != $get_code || empty($get['body'])) {
        return array();
        // @todo: return an error?
    } else {
        return stats_str_getcsv($get['body']);
    }
}
 /**
  * Query the WordPress.com REST API using the blog token
  *
  * @param string  $path
  * @param string  $version
  * @param array   $args
  * @param string  $body
  * @return array|WP_Error $response Data.
  */
 static function wpcom_json_api_request_as_blog($path, $version = self::WPCOM_JSON_API_VERSION, $args = array(), $body = null)
 {
     $filtered_args = array_intersect_key($args, array('method' => 'string', 'timeout' => 'int', 'redirection' => 'int'));
     /**
      * Determines whether Jetpack can send outbound https requests to the WPCOM api.
      *
      * @since 3.6.0
      *
      * @param bool $proto Defaults to true.
      */
     $proto = apply_filters('jetpack_can_make_outbound_https', true) ? 'https' : 'http';
     // unprecedingslashit
     $_path = preg_replace('/^\\//', '', $path);
     // Use GET by default whereas `remote_request` uses POST
     if (isset($filtered_args['method']) && strtoupper($filtered_args['method'] === 'POST')) {
         $request_method = 'POST';
     } else {
         $request_method = 'GET';
     }
     $validated_args = array_merge($filtered_args, array('url' => sprintf('%s://%s/rest/v%s/%s', $proto, JETPACK__WPCOM_JSON_API_HOST, $version, $_path), 'blog_id' => (int) Jetpack_Options::get_option('id'), 'method' => $request_method));
     return Jetpack_Client::remote_request($validated_args, $body);
 }