使用php curl http post在ownCloud上创建用户

时间:2016-01-02 14:03:36

标签: php api authentication curl owncloud

在owncloud相关网站和stackoverflow本身的以下链接中,所有相关信息都以破碎形式显示:

  1. User Provisioning Api - Owncloud
  2. PHP + curl, HTTP POST sample code
  3. Create user on ownCloud using Ajax Jquery
  4. User Provisioning - php Authentication error
  5. 我正在尝试做一些非常简单的事情:

    1. 我在localhost中设置了一个owncloud服务器,
    2. 我有一个html页面,其中包含用户名和密码的字符串值
    3. 我通过以下php脚本发送要处理的页面请求。

      <?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";
      ?>
      
    4. 我得到的结果如下:

        

      开始处理凭据,首先它将存储在本地   变量
      你好弗兰克你的密码是frankspassword
        failure 997 Unauthorized
      在owncloud中创建了一个新用户

      我还尝试使用以下php脚本登录自己的云:

      // 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;
      

      即使这个也失败了。

      总之,它不起作用。我也无法通过类似的脚本登录owncloud。这样做的正确方法是什么?我缺少什么设置?有人可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

owncloud文档指出身份验证是通过基本的HTTP身份验证标头完成的。您目前正在做的是将凭证作为参数发送到API调用。您需要添加以下行:

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

在CURLOPT_RETURNTRANSFER($curl而不是$ch)中也有拼写错误。

答案 1 :(得分:1)

由于此问题具体涉及owncloud,因此我创建了一个帐户,并在owncloud forum中发布了一个将此链接与其关联的问题。

我自己的主人@RealRancor建议我,以下,

  

再看看,也许它很容易替换:

     

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

     

     

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

     

如文档中所示。

令人惊讶的是,它就像一个魅力。所以这是整个修改过的php脚本:

<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>

这是输出:

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

答案 2 :(得分:0)

由于您的关联帖子(Create user on Owncloud)显示您需要具有一些凭据的基本身份验证标头。您需要提供管理员凭据,afaict。