How to set shared_link in cURL by PHP
Hi everyone.
I'm having trouble with cURL in PHP.
It will be a bat request by all means.
I want to make a file a shared link.
I refer to this API Reference.
curl -i -X PUT "https://api.box.com/2.0/files/32423234?fields=shared_link" \
-H "Authorization: Bearer <ACCESS_TOKEN>" \
-d '{
"shared_link": {
"access": "open",
"password": "mypassword",
"unshared_at": "2012-12-12T10:53:43-08:00",
"permissions": {
"can_download": false
}
}
}'
Here is the code I tried.
$url = 'https://api.box.com/2.0/files/<fileid>?fields=shared_link';
$curl = curl_init($url);
$params['shared_link']['access'] = 'open';
$params['shared_link']['password'] = 'mypassword';
$params['shared_link']['permissions'][can_download] = 'false';
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); // ※
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($curl, CURLOPT_POSTFIELDS, $params); // ※
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $HTTPHEADER);
$response = curl_exec($curl);
{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"invalid_parameter","name":"entity-body","message":"Invalid value 'shared_link=%7B%22access%22%3A+%22open%22%2C%22password%22%3A+%22mypassword%22%2C%22permissions%22%3A+%7B%22can_download%22%3A+false%7D%7D'. Entity body should be a correctly nested resource attribute name\/value pair"}]},"help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Bad Request","request_id":"*************"}
Please help someone.
Thank you for reading to the end.
-
Correct.
excuse me. Exclude the following statements.
curl_setopt($curl, CURLOPT_POSTFIELDS, $params); // ※
Response is
{"type":"error","status":400,"code":"bad_request","context_info":{"errors":[{"reason":"invalid_parameter","name":"entity-body","message":"Invalid value 'grant_type=urn......password%5D=mypassword'. Entity body should be a correctly nested resource attribute name\/value pair"}]},"help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Bad Request","request_id":"********"}
Thank you for reading to the end.
Please sign in to leave a comment.
Comments
1 comment