Home Backend Development PHP Tutorial Analysis of related functions for sending http requests in WordPress

Analysis of related functions for sending http requests in WordPress

Jul 29, 2016 am 09:08 AM
gt http post request

There are many ways to send Http requests (GET / POST) in PHP, such as file_get_contents() function, fopen() function or cURL extension. However, due to different server conditions, they may not be compatible with all situations. If you want to send Http requests need to go through 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

1

2

3

4

5

6

/**

 *使用 WP_Http 类发送简单的 GET 请求

 *http://www.endskin.com/wp_http/

*/

$http = new WP_Http;

$result = $http->request( 'http://www.endskin.com' );

Copy after login

The code above 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, which 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 is an array

That is to say, the content of the target is $result['body']

Send a POST request

If you need to send a POST request, you must use WP_Http-> The second parameter of request(), see the example below:

1

2

3

4

5

6

7

/**

 *使用 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 content of the $result variable returned, please refer to the GET request above.

POST request that requires verification

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

1

2

3

4

5

6

7

8

9

10

// 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.

The above introduces the analysis of related functions to implement sending http requests in WordPress, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

What does http status code 520 mean? What does http status code 520 mean? Oct 13, 2023 pm 03:11 PM

HTTP status code 520 means that the server encountered an unknown error while processing the request and cannot provide more specific information. Used to indicate that an unknown error occurred when the server was processing the request, which may be caused by server configuration problems, network problems, or other unknown reasons. This is usually caused by server configuration issues, network issues, server overload, or coding errors. If you encounter a status code 520 error, it is best to contact the website administrator or technical support team for more information and assistance.

Understand common application scenarios of web page redirection and understand the HTTP 301 status code Understand common application scenarios of web page redirection and understand the HTTP 301 status code Feb 18, 2024 pm 08:41 PM

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

Common network communication and security problems and solutions in C# Common network communication and security problems and solutions in C# Oct 09, 2023 pm 09:21 PM

Common network communication and security problems and solutions in C# In today's Internet era, network communication has become an indispensable part of software development. In C#, we usually encounter some network communication problems, such as data transmission security, network connection stability, etc. This article will discuss in detail common network communication and security issues in C# and provide corresponding solutions and code examples. 1. Network communication problems Network connection interruption: During the network communication process, the network connection may be interrupted, which may cause

HTTP 200 OK: Understand the meaning and purpose of a successful response HTTP 200 OK: Understand the meaning and purpose of a successful response Dec 26, 2023 am 10:25 AM

HTTP Status Code 200: Explore the Meaning and Purpose of Successful Responses HTTP status codes are numeric codes used to indicate the status of a server's response. Among them, status code 200 indicates that the request has been successfully processed by the server. This article will explore the specific meaning and use of HTTP status code 200. First, let us understand the classification of HTTP status codes. Status codes are divided into five categories, namely 1xx, 2xx, 3xx, 4xx and 5xx. Among them, 2xx indicates a successful response. And 200 is the most common status code in 2xx

http request 415 error solution http request 415 error solution Nov 14, 2023 am 10:49 AM

Solution: 1. Check the Content-Type in the request header; 2. Check the data format in the request body; 3. Use the appropriate encoding format; 4. Use the appropriate request method; 5. Check the server-side support.

How to implement PHP to jump to the page and carry POST data How to implement PHP to jump to the page and carry POST data Mar 22, 2024 am 10:42 AM

PHP is a programming language widely used in website development, and page jumps and carrying POST data are common requirements in website development. This article will introduce how to implement PHP page jump and carry POST data, including specific code examples. In PHP, page jumps are generally implemented through the header function. If you need to carry POST data during the jump process, you can do it through the following steps: First, create a page containing a form, where the user fills in the information and clicks the submit button. Acti in the form

How to implement HTTP streaming using C++? How to implement HTTP streaming using C++? May 31, 2024 am 11:06 AM

How to implement HTTP streaming in C++? Create an SSL stream socket using Boost.Asio and the asiohttps client library. Connect to the server and send an HTTP request. Receive HTTP response headers and print them. Receives the HTTP response body and prints it.

See all articles