cURL PUT Request with Nextcloud / owncloud API

大兔子大兔子 提交于 2020-01-25 07:33:36

问题


I tried to update an existing Nextcloud user through their API. When I do it directly via shell it works

curl -u user:pass -X PUT "https://example.org/ocs/v1.php/cloud/users/admin" -H "OCS-APIRequest: true" -d key="quota" -d value="5GB"

But when I try to do it via PHP with the following code it always returns "failure 997"

$url = 'https://' . $ownAdminname . ':' . $ownAdminpassword . '@example.org/ocs/v1.php/cloud/users/admin';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$fields = array("quota" => "5GB");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'OCS-APIRequest: true'
    ));
$response = curl_exec($ch);
curl_close($ch);
echo "Response: ".$response;

回答1:


The difference between the cURL command and the PHP code you pasted lies in a poorly designed user provisioning API.

Using these cURL arguments:

-d key="quota" -d value="5GB"

... is not equivalent to the fields you're posting:

$fields = array("quota" => "5GB");

... but rather:

$fields = array(
    'key' => 'quota',
    'value' => '5GB',
);

The explanation for the 997 code you're getting can be found in https://github.com/owncloud/core/blob/v10.0.3/apps/provisioning_api/lib/Users.php#L269-L272: since there's no "key" key in the data submitted ($parameters['_put']['key'] will evaluate as null) and hence the error.



来源:https://stackoverflow.com/questions/47314775/curl-put-request-with-nextcloud-owncloud-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!