WordPress uses APIs such as wp_remote_get/post to replace cURL

Plug in involves obtaining remote resources, which I usually use PHP Provided curl Method, but the test found some hosts curl There are compatibility issues, especially in iis + php Environment. After consulting relevant materials, we found that WordPress had already encapsulated some HTTP APIs, so we replaced them. The problem was solved.

Wp_remote Series API

  • wp_remote_request(string $url, array $args)
    WordPress encapsulated HTTP general request function, which can be used to execute GET POST PUT DELETE And almost all HTTP requests. Here's a code:

     //See wp includes/http.php for function declaration $response = wp_remote_request( ' http://Www.azimiao.com/user.php ', array( 'method'     => 'DELETE', 'httpversion' => '1.1', 'headers' => array( 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( array('userid'=>1001) ) ) ); $body = wp_remote_retrieve_body($response); echo $body;
  • wp_safe_remote_request($url, $args)
    wp_remote_request This is a high security version of. It avoids redirection and hijacking attacks when accessing arbitrary urls.
  • wp_remote_get($url, $args)
    wp_remote_request Of GET Exceptions, i.e method Default is GET
    The use method is the same as wp_remote_request (Body is not recommended).
  • wp_safe_remote_get($url, $args)
    wp_remote_get This is a high security version of. It avoids redirection and hijacking attacks when accessing arbitrary urls.
  • wp_remote_post($url, $args)
    wp_remote_request Of POST Exceptions, i.e method Default is POST
    The use method is the same as wp_remote_request
  • wp_safe_remote_post($url, $args)
    wp_remote_post This is a high security version of. It avoids redirection and hijacking attacks when accessing arbitrary urls.
  • wp_remote_head($url, $args)
    wp_remote_request Of HEAD Exceptions, i.e method Default is HEAD
    The use method is the same as wp_remote_request

Use Instance

I used it in the Chasing plugin wp_remote_get And wp_remote_post , encapsulate them in the same object, which is convenient to call everywhere:

 /** *Easy to use encapsulation of WP_REMOTE method * @author Yetu < admin@azimiao.com > */ class WPNetwork{ public static function http_get_contents($_url,$_getHeader = array()) { $m_req = wp_remote_get($_url,array("headers"=>$_getHeader)); $m_res = wp_remote_retrieve_body($m_req); return $m_res; } //Get content by post public static function http_post_contents($_url,$_postHeader = array(),$_postBody  = array()) { $m_req = wp_remote_post($_url,array('headers'=>$_postHeader,'body' => $_postBody)); $m_res = wp_remote_retrieve_body($m_req); return $m_res; } }
Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/5970.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*

Comment area

  1. c0sMx 03-20 07:46 reply

    Why should I use Typecho

  2. roughly 03-20 12:09 reply

    I saw it here yesterday. Once I looked at the code, it turned out that there was a set of functions in 2.9
    Can you control whether the agent set in wp config is used or not? I test the default is to go.