WordPress uses APIs such as wp_remote_get/post to replace cURL
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
/** *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; } }