我正在尝试使用wordpress API远程创建wordpress版本4.9.1中的用户。下面是我的PHP代码:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "8080",
CURLOPT_URL => "http://localhost:8080/my_site/wp-json/wp/v2/users/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\\r\\nContent- Disposition: form-data; name=\\"username\\"\\r\\n\\r\\nwakawaka\\r\\n------WebKitFormBoundary7MA4YWxkTrZu0gW\\r\\nContent-Disposition: form-data; name=\\"email\\"\\r\\n\\r\\[email protected]\\r\\n------WebKitFormBoundary7MA4YWxkTrZu0gW\\r\\nContent-Disposition: form-data; name=\\"password\\"\\r\\n\\r\\ncITt xOSx HGKG GjTw No33 hzpG\\r\\n------WebKitFormBoundary7MA4YWxkTrZu0gW\\r\\nContent-Disposition: form-data; name=\\"ACCESS_TOKEN\\"\\r\\n\\r\\ncITt xOSx HGKG GjTw No33 hzpG\\r\\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=---- WebKitFormBoundary7MA4YWxkTrZu0gW",
"postman-token: 84cad22c-9f08-d2b1-18f6-6eb9880f3f5f"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
当我执行代码时,创建新用户失败,这是我得到的错误代码:
{
"code":"rest_cannot_create_user",
"message":"Sorry, you are not allowed to create new users.",
"data": {"status":401}
}
如何使用wordpress Rest API在wordpress中安全地远程创建新用户?