Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从服务器调用WordPress REST API_Php_Wordpress_Rest - Fatal编程技术网

Php 从服务器调用WordPress REST API

Php 从服务器调用WordPress REST API,php,wordpress,rest,Php,Wordpress,Rest,我有一个用PHP编写的应用服务器,它需要通过WP的RESTAPI与WordPress部署交互。我很难通过身份验证调用WP的RESTAPI。例如,如何在使用WP进行身份验证后创建用户 我在网上找到的大多数示例都是使用Ajax或Cookie进行身份验证,这在服务器环境下不起作用 有人能提出建议吗?所以你的问题是:在使用WP进行身份验证后,如何创建用户? 我认为你在认证过程中没有问题。 如果您使用PHP,则可以使用PHP文件获取内容函数或cURL 下面我将给你一个PHP cURL的例子 $post =

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

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


有人能提出建议吗?

所以你的问题是:在使用WP进行身份验证后,如何创建用户? 我认为你在认证过程中没有问题。 如果您使用PHP,则可以使用PHP文件获取内容函数或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\u get\u contents函数。您可以在这里找到另一个使用php的WP REST API示例

我们使用php curl调用其他php端点。我建议你调查一下。