返回介绍

wp_remote_request()

发布于 2017-09-11 12:44:41 字数 5086 浏览 1648 评论 0 收藏 0

wp_remote_request( string $url,  array $args = array() )

Retrieve the raw response from the HTTP request.


description

The array structure is a little complex:

$res = array(
    'headers'  => array(),
    'response' => array(
        'code'    => int,
        'message' => string
    )
);

All of the headers in $res[‘headers’] are with the name as the key and the
value as the value. So to get the User-Agent, you would do the following.

$user_agent = $res['headers']['user-agent'];

The body is the raw response content and can be retrieved from $res[‘body’].

This function is called first to make the request and there are other API functions to abstract out the above convoluted setup.

Request method defaults for helper functions:

  • Default ‘GET’ for wp_remote_get()
  • Default ‘POST’ for wp_remote_post()
  • Default ‘HEAD’ for wp_remote_head()

参数

$url

(string) (Required) Site URL to retrieve.

$args

(array) (Optional) Request arguments.

Default value: array()


返回值

(WP_Error|array) The response or WP_Error on failure.


源代码

File: wp-includes/http.php

function wp_remote_request($url, $args = array()) {
	$http = _wp_http_get_object();
	return $http->request( $url, $args );
}

更新日志

Versiondescription
2.7.0Introduced.

相关函数

Uses

  • wp-includes/http.php: _wp_http_get_object()

Used By

  • wp-includes/class-http.php: WP_Http::handle_redirects()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Abiral Neupane

    Send a Delete request with wp_remote_post

    $response = wp_remote_request( 'http://www.example.com/index.php',
        array(
            'method'     => 'DELETE'
        )
    );
    
    $body = wp_remote_retrieve_body($response);
    echo $body;
    /* Prints response provided from the service provider. Most of the cases, it will be JSON  */
    
  2. You can insert the DELETE method inside wp_remote_request() like this:

    
    $url = 'http://wordpress.org';
    $args = array(
    	'method' => 'DELETE'
    );
    $response = wp_remote_request( $url, $args );
    

    Sample ConvertKit API call function using wp_remote_request()

      private function call($args, $endpoint, $api_secret = null, $method = 'GET') {
        if(is_null($api_secret)) { $api_secret = $this->api_secret(); }
    
        //Populate the correct endpoint for the API request
        $url                = "https://api.convertkit.com/v3/{$endpoint}?api_secret={$api_secret}";
    
        //Allow 3rd parties to alter the $args
        $args               = apply_filters('convertkit-call-args', $args, $endpoint, $method);
    
        //Populate the args for use in the wp_remote_request call
        $wp_args            = array('body' => $args);
        $wp_args['method']  = $method;
        $wp_args['timeout'] = 30;
    
        //Make the call and store the response in $res
        $res = wp_remote_request($url, $wp_args);
    
        //Check for success
        if(!is_wp_error($res) && ($res['response']['code'] == 200 || $res['response']['code'] == 201)) {
          return $res['body'];
        }
        else {
          return false;
        }
      }

    What about calling this function? Well here’s another function which updates a contact’s email address/name

      public function update_subscriber($contact, $new_email = '') {
        $id = $this->get_subscriber_id_by_email($contact);
    
        if(!$id) { return; } //Nada found?
    
        $args = array(
          'email_address' => (!empty($new_email) && is_email($new_email))?$new_email:$contact->user_email,
          'first_name'    => (string)$contact->first_name
        );
    
        $resp_body = (array)json_decode($this->call($args, "subscribers/{$id}", null, 'PUT'));
    
        //don't really care what the response was at this point, maybe we'll update this later
        return true;
      }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文