【问题标题】:Calling WordPress REST API from server从服务器调用 WordPress REST API
【发布时间】:2018-07-06 19:16:38
【问题描述】:

我有一个用 PHP 编写的应用服务器,它需要通过 WP 的 REST API 与 WordPress 部署进行交互。我很难通过身份验证调用 WP 的 REST API。例如,通过WP认证后如何创建用户?

我在网上找到的大多数示例都是使用 Ajax 或 cookie 进行身份验证,这在服务器情况下不起作用。

有人可以推荐吗?

【问题讨论】:

标签: php wordpress rest


【解决方案1】:

所以你的问题是:如何在通过 WP 进行身份验证后创建用户? 我正在考虑您在身份验证过程中没有探针。 如果您使用 PHP,您可以使用 php file_get_contents 函数或 cURL 下面我会给你一个 PHP cURL 的例子

$post = array(
'username'  => 'user2',
'email'     => 'user2@somedomain.com',
'password'  =>  'somerandompassword');

$post = http_build_query($post);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/wp/wp-json/wp/v2/users");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "username" . ":" . "password"
);
$headers    = array();
$headers[]  = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
print_r(json_decode($result, true), true);

如果您的服务器由于某些原因不能使用 cURL,您也可以使用 file_get_contents 函数。您可以在这里找到另一个使用 php 的 WP REST API 示例www.wpsaga.info/tag/wp-rest-api

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 2021-11-17
    • 2018-11-09
    • 2016-08-26
    • 1970-01-01
    相关资源
    最近更新 更多