通用API文档

向外部服务发送POST数据

💡 云策文档标注

概述

本文档介绍了在WordPress中如何使用wp_remote_post()函数向外部服务发送POST请求,包括数据构建和API交互的基本流程。

关键要点

  • POST请求用于向服务器发送数据以触发操作,例如处理联系表单。
  • 使用wp_remote_post()函数发送POST请求,其参数与wp_remote_get()相同。
  • 数据需构建为关联数组并赋值给'body'参数,服务器端可通过$_POST变量访问。
  • API交互通常需要认证,如API密钥,以确保应用权限。

代码示例

$body = array(
    'name'    => sanitize_text_field( 'Jane Smith' ),
    'email'   => sanitize_email( 'some@email.com' ),
    'subject' => sanitize_text_field( 'Checkout this API stuff' ),
    'comment' => sanitize_textarea_field( 'I just read a great tutorial. You gotta check it out!' ),
);
$args = array(
    'body' => $body,
);
$response = wp_remote_post( 'https://your-contact-form.com', $args );

📄 原文内容

POST is used to send data to the server for the server to act upon in some way. For example, a contact form. When you enter data into the form fields and click the submit button the browser takes the data and sends a POST request to the server with the text you entered into the form. From there the server will process the contact request.

POSTing data to an API

The same helper methods (<a href="https://developer.wordpress.org/reference/functions/wp_remote_retrieve_body/">wp_remote_retrieve_body()</a>, etc ) are available for all of the HTTP method requests, and utilized in the same fashion.

POSTing data is done using the <a href="https://developer.wordpress.org/reference/functions/wp_remote_post/">wp_remote_post()</a> function, and takes exactly the same parameters as <a href="https://developer.wordpress.org/reference/functions/wp_remote_get/">wp_remote_get()</a>.

To send data to the server you will need to build an associative array of data. This data will be assigned to the 'body' value. From the server side of things the value will appear in the $_POST variable as you would expect. i.e. if body => array( 'myvar' => 5 ) on the server $_POST['myvar'] = 5.

Because GitHub does not allow POSTing to the API used in the previous example, this example will pretend that it does. Typically if you want to POST data to an API you will need to contact the maintainers of the API and get an API key or some other form of authentication token. This simply proves that your application is allowed to manipulate data on the API the same way logging into a website as a user does to the website.

Let’s assume we are submitting a contact form with the following fields: name, email, subject, comment. To set up the body we do the following:

$body = array(
	'name'    => sanitize_text_field( 'Jane Smith' ),
	'email'   => sanitize_email( 'some@email.com' ),
	'subject' => sanitize_text_field( 'Checkout this API stuff' ),
	'comment' => sanitize_textarea_field( 'I just read a great tutorial. You gotta check it out!' ),
);

Now we add the body to the $args array that will be passed as the second argument. (The second argument accepts many options, see Advanced section for more details)

$args = array(
	'body'        => $body,
);

Then of course to make the call

$response = wp_remote_post( 'https://your-contact-form.com', $args );