Search code examples
phpcurlowncloudnextcloud

cURL PUT Request with Nextcloud / owncloud API


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;

Solution

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