Home > Backend Development > PHP Tutorial > Analysis of related functions for sending http requests in WordPress, wordpress function_PHP tutorial

Analysis of related functions for sending http requests in WordPress, wordpress function_PHP tutorial

WBOY
Release: 2016-07-12 09:02:03
Original
1013 people have browsed it

Analysis of related functions for sending http requests in WordPress, wordpress functions

There are many methods for sending Http requests (GET / POST) in PHP, such as the file_get_contents() function , fopen() function or cURL extension, but due to different server situations, it may not be compatible with all situations. In this way, sending an HTTP request requires a series of judgments, which is very troublesome.

However, WordPress provides a WP_Http class to help you determine compatibility. You only need to call the function inside to complete sending Http requests. Below I will briefly introduce the commonly used functions of this class.

Send GET request

/**
 *使用 WP_Http 类发送简单的 GET 请求
 *http://www.endskin.com/wp_http/
*/
$http = new WP_Http;
$result = $http->request( 'http://www.endskin.com' );
Copy after login

The above code stores the request target information in the $result variable. $result is an array with the following keys:

  • headers: The returned headers information is an array
  • body: The content of the target is the same as viewing it directly in the browser
  • response: the returned code. If the request is successful, array( 'code'=>200, 'message'=>'OK' )
  • Cookies: Cookie information, an array

In other words, the content of the target is $result['body']

Send POST request

If you need to send a POST request, you have to use the second parameter of WP_Http->request(). See the example below:

/**
 *使用 WP_Http 类发送简单的 POST 请求
 *http://www.endskin.com/wp_http/
*/
$http = new WP_Http;
$post = array( 'name' => '斌果', 'blog' => 'http://www.bgbk.org' );
$result = $http->request( 'http://www.endskin.com', array( 'method' => 'POST', 'body' => $post ) );
Copy after login

For the returned $result variable content, please refer to the GET request above.

POST request requiring verification

If you want to submit some information to some RESTFul API, you first need to authenticate. We need to send a base64-encoded string containing a username and password pair to the API, as detailed below:

// You would edit the following:
$username = 'denishua'; // login
$password = '123456'; // password
$message = "I'm posting with the API";
// Now, the HTTP request:
$api_url = 'http://your.api.url/update.xml';
$body = array( 'status' => $message );
$headers = array( 'Authorization' => 'Basic '.base64_encode("$username:$password") );
$request = new WP_Http;
$result = $request->request( $api_url , array( 'method' => 'POST', 'body' => $body, 'headers' => $headers ) );

Copy after login

After WordPress added the WP_Http class, it abandoned the Snoopy PHP Class. Therefore, it is recommended that when writing plug-ins for WordPress, try to use WP_Http to make HTTP requests.

Articles you may be interested in:

  • Tips for customizing the color scheme of the backend management interface in WordPress
  • How to set default content in the WordPress article editor
  • Explanation of PHP functions used to create and obtain sidebars in WordPress
  • Detailed explanation of how to use functions to add and perform actions in WordPress

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1086651.htmlTechArticleRelevant function analysis for sending http requests in WordPress, the wordpress function sends Http requests (GET/POST) in PHP ) There are many methods, such as file_get_contents() function, f...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template