json - 将数据从 wordpress 发送到 3rd 方 API 的最佳做法是什么
问题描述
我需要将数据从 wordpress 站点发送到 3rd 方 API,并且想知道如何以最佳和正确的方式进行操作。据我了解,我可以使用 php 函数或 javascript 来完成。什么是更好和正确的方法?它也有任何内置的wordpress功能吗?
解决方案
要将数据从站点 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/
推荐阅读
- .net - 运行时创建不同的 EDMX 模型
- python - 在 Python 中创建不可变对象
- flutter - 我不能使用 Icon(Icons.star, color: Colors.green[500]),错误
- wordpress - 单击后可以从 $_SESSION 存储值
- amazon-web-services - 如何在 Cloudformation 模板条件中使用 AWS SSM 参数存储值?
- vue.js - 如何在 Vue 中将组件的整个数据属性设置为保持反应性的新对象?
- r - Place text next to dodged points
- c++ - Visual Studio C++ cannot find OpenCV-Contrib modules
- android - 与 VirtualBox 机器一起运行 Android 模拟器
- mysql - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本“...”相对应的手册 JDBC Google Apps 脚本