Ich versuche, Benutzer in WordPress Version 4.9.1 über die WordPress-API zu erstellen. Dies ist mein PHP Code:
$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;
}
Wenn ich den Code ausführe, schlägt das Erstellen eines neuen Benutzers fehl und dies ist der Fehlercode, den ich erhalte:
{
"code":"rest_cannot_create_user",
"message":"Sorry, you are not allowed to create new users.",
"data": {"status":401}
}
Wie kann ich mit der wordpress Rest API einen neuen Benutzer in WordPress sicher und remote erstellen?
Um Inhalte auf einer WordPress-Website über die REST-API zu erstellen, müssen Sie die Authentifizierung verwenden.
Sie haben zwei Möglichkeiten, OAuth-Token abzurufen. Detaillierte Erläuterungen finden Sie unter folgendem Link:
https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/