Creating a user on ownCloud using php curl http post

All relevant information is present in broken form in the following links on sites associated with their own eyes, and from the stackoverflow itself:

I am trying to do something very simple:

  • I have my own private server in my local host,
  • I have an html page that accepts string values โ€‹โ€‹of username and password
  • I am sending a page request for processing with the following php script.

      <? php
     echo "Begun processing credentials, first it will be stored in local variables".  "<br/>";
    
     // Loading into local variables
     $ userName = $ _POST ['username'];
     $ RRpassword = $ _POST ['password'];
    
     echo "Hello".  $ userName.  "<br/>";
     echo "Your password is".  $ RRpassword.  "<br/>";
    
     // Add data, to owncloud post array and then Send the http request for creating a new user
     $ ownCloudPOSTArray = array ('username' => $ userName, 'password' => $ RRpassword);
    
     $ url = 'http: //localhost/owncloud/ocs/v1.php/cloud/users';
     $ ch = curl_init ($ url);
    
     curl_setopt ($ ch, CURLOPT_POST, 1);
     curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ ownCloudPOSTArray);
     curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true);
    
     $ response = curl_exec ($ ch);
     curl_close ($ ch);
    
     echo "<br/> Created a new user in owncloud";
     ?>
    

I get output like:

Start processing credentials, first it will be saved in local variables
Hi Frank
Your password frankspassword
fail 997 unauthorized
Created a new user in his own club

I also tried to enter my own cloud using the following php script:

// Login As Admin $ownAdminname = 'ownAdmin'; $ownAdminpassword = 'ownAdminPassword'; $ch = curl_init('http://localhost/owncloud'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, "$ownAdminname:$ownAdminpassword"); $output = curl_exec($ch); curl_close($ch); echo $output; 

Even this one fails.

So this does not work. I also can not enter through a similar script in owncloud. What is the right way to do this? What settings do I miss? Can anyone help?

+5
source share
3 answers

Since this question is specific to owncloud, I created an account and posted a question related to it in the owncloud forum .

There I suggested my own master @RealRancor, the next one,

It was just a different look, maybe just replacing it:

$ url = ' http: //localhost/owncloud/ocs/v1.php/cloud/users ';

with

$ url = ' http: // adminuser: adminpass@localhost /owncloud/ocs/v1.php/cloud/users ';

as shown in the documentation.

And amazingly, it worked like a charm. So here is the whole modified php script:

 <pre> &lt;?php echo "Begun processing credentials , first it will be stored in local variables" . "<br/>"; // Loading into local variables $userName = $_POST['username']; $RRpassword = $_POST['password']; echo "Hello " . $userName . "<br/>"; echo "Your password is " . $RRpassword . "<br/>"; // Login Credentials as Admin $ownAdminname = 'ownAdmin'; $ownAdminpassword = 'ufi2016%%'; // Add data, to owncloud post array and then Send the http request for creating a new user $url = 'http://' . $ownAdminname . ':' . $ownAdminpassword . '@localhost/owncloud/ocs/v1.php/cloud/users'; echo "Created URL is " . $url . "<br/>"; $ownCloudPOSTArray = array('userid' => $userName, 'password' => $RRpassword ); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArray); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); echo "Response from curl :" . $response; echo "<br/>Created a new user in owncloud<br/>"; // Add to a group called 'Users' $groupUrl = $url . '/' . $userName . '/' . 'groups'; echo "Created groups URL is " . $groupUrl . "<br/>"; $ownCloudPOSTArrayGroup = array('groupid' => 'Users'); $ch = curl_init($groupUrl); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArrayGroup); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); echo "Response from curl :" . $response; echo "<br/>Added the new user to default group in owncloud"; ?> </pre> 

And here is the conclusion:

 Begun processing credentials , first it will be stored in local variables Hello Frank Your password is frankspassword Created URL is http://ownAdmin:ufi2016%%@localhost/owncloud/ocs/v1.php/cloud/users Response from curl : ok 100 Created a new user in owncloud Created groups URL is http://ownAdmin:ufi2016%%@localhost/owncloud/ocs/v1.php/cloud/users/Frank/groups Response from curl : ok 100 Added the new user to default group in owncloud 
+1
source

Our own documentation states that authentication is done using the main HTTP authentication header. What you are doing now sends the credentials as parameters for calling the API. You need to add the following line:

 curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $RRpassword); 

There is also a typo in CURLOPT_RETURNTRANSFER ( $curl instead of $ch ).

+1
source

As your linked post ( Create User in Owncloud ) shows, you need a basic auth header with some credentials. You need to provide admin credentials, afaict.

0
source

Source: https://habr.com/ru/post/1239669/


All Articles