In WordPress to implement the relevant function of sending HTTP requests parsing, WordPress function
There are many ways to send HTTP requests (Get/post) in PHP, such as the file_get_contents () function, the fopen () function, or the cURL extension, but because the server is different, it is not necessarily compatible with all cases, so you want to send an HTTP request You need to pass a series of judgments, very troublesome.
However, WordPress provides a wp_http class to help you make a good sense of compatibility, you only need to invoke the inside of the function to complete the sending of Http requests. Let me briefly introduce the common functions of this class.
Send a GET request
/** * Send a simple GET request using the Wp_http class *http://www.endskin.com/wp_http/*/$http = new Wp_http; $result = $http->request (' http://w Ww.endskin.com ');
The above code stores the request target information in the $result variable, $result is an array, it has the key:
- Headers: Returns the headers information, which is an array
- Body: The content of the target, and in the browser directly see is the same
- Response: Returns the code if the request succeeds and returns an array (' Code ' =>200, ' message ' = ' OK ')
- Cookies:cookie information, is an array
This means that the target is $result [' body ']
Send a POST request
If you need to send a POST request to use the second parameter of Wp_http->request (), see the example below:
/** * Send a simple POST request using the Wp_http class *http://www.endskin.com/wp_http/*/$http = new Wp_http; $post = Array (' name ' = ' bin ', ' bl og ' = ' http://www.bgbk.org '); $result = $http->request (' http://www.endskin.com ', Array (' method ' = ' POST ', ' B Ody ' = $post));
Refer to the GET request above for the contents of the $result variable returned.
POST requests that need to be validated
If you want to submit some information in a RESTFul API, you first need to verify that we need to send a base64 encoded string containing the username and password pairs 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 '; $bod y = 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, ' He Aders ' = $headers));
WordPress joined Wp_http class, then gave up the Snoopy this PHP class, so we recommend to WordPress to write plug-ins, try to use Wp_http to do Http requests.
Articles you may be interested in:
- Tips for customizing the background Management interface color scheme in WordPress
- Ways to set default content in the WordPress article editor
- PHP functions for creating and getting sidebar in WordPress
- How to use the function of adding and executing actions in WordPress
http://www.bkjia.com/PHPjc/1086651.html www.bkjia.com true http://www.bkjia.com/PHPjc/1086651.html techarticle in WordPress to implement the relevant function of sending HTTP requests parsing, WordPress function in PHP send HTTP request (Get/post) There are many methods, such as the file_get_contents () function, f ...