2

I am trying to create users in wordpress Version 4.9.1, remotely using the wordpress API. This is my PHP code below:

$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;
}

When i execute the code, create new user fails and this is error code I get:

 {
  "code":"rest_cannot_create_user",
  "message":"Sorry, you are not allowed to create new users.",
  "data":  {"status":401}
} 

How can I securely and remotely Create new user in wordpress using wordpress Rest API?

1
  • I can't help but notice there are no authentication headers on your request, and no CURLOPT_POST count set, as well as a huge number of unnecessary webkitformboundary annotations. You'll need to clean all of that up and make sure you're POST'ing properly. You might also want to change the password you use in your code as it's in your code snippet for all to see
    – Tom J Nowell
    Commented Dec 11, 2017 at 0:22

1 Answer 1

5

To create any content on a WordPress website via REST-API your must use authentication.

You have two options for getting OAuth tokens, Detailed explanations in the following link:

https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.