首页 > 解决方案 > 将数据从 wordpress 发送到 3rd 方 API 的最佳做法是什么

问题描述

我需要将数据从 wordpress 站点发送到 3rd 方 API,并且想知道如何以最佳和正确的方式进行操作。据我了解,我可以使用 php 函数或 javascript 来完成。什么是更好和正确的方法?它也有任何内置的wordpress功能吗?

标签: jsonwordpressfetch

解决方案


要将数据从站点 B 发送到站点 A(服务器的站点),这两个站点需要进行通信。并且通讯的方式是api。由于站点 A 将接收数据,站点 B 将发送数据,因此站点 A 必须提供 api,站点 B 将使用该 api 将数据发送到站点 A。这就是概念。

现在什么是 api:现在的 api 一般指的是 REST API,因为它是现在的普遍趋势。它是为您提供 url 的东西,您可以通过它使用 post 或 get 方法以 json 格式获取或发送数据。有关此主题的更多详细信息,请参见此处:https ://www.smashingmagazine.com/2018/01/understanding-using-rest-api/

由于站点 A 在 ruby​​ on rails 中,您可以按照以下链接的步骤制作 api:https ://scotch.io/tutorials/build-a-restful-json-api-with-rails-5-part-一

在站点 A 制作 api 后,您可能有任何用于发送数据的 url。假设 url 是 {site_name}/send_data 并且这个 url 使用 post 方法从任何站点(在这种情况下,从站点 B)接收数据

现在,由于站点 B 使用 wp,这已经具有内置函数 wp_remote_post,它使用 api url 和数据作为参数将数据发送到该 url。这样您就可以将数据发送到站点 A。

wp_remote_post( '{site_name}/send_data', $data_to_send_as_array )

更多关于 wp_remote_post:https ://developer.wordpress.org/reference/functions/wp_remote_post/


推荐阅读