0

I'm trying to create the box app users using PHP. The curl for create user as follows, and it is working on terminal

curl https://api.box.com/2.0/users \
-H "Authorization: Bearer <Access token>" \
-d '{"name": "New User", "is_platform_access_only": true}' \
-X POST

Same thing I have tried with php But it is giving the following error

{"type":"error","status":400,"code":"invalid_request_parameters","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Invalid input parameters in request","request_id":"6688622675982fb5339a37"}

The following one I have tried

$developer_token = "TOKEN" ;
$access_token_url = "https://api.box.com/2.0/users";

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $access_token_url);

//Adding Parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'name'=>'NEW USER', 
    'is_platform_access_only'=>'true',
     ));

//Adding Header 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer '.$developer_token
     ));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response1 = curl_exec($ch);

If I remove the Post parameters, and run with only headers it is give the result of users. But with post it is throws error.

1 Answer 1

1

I have rise the same question in Perl tag with Perl code. There I got answer by user @melpomene.

We should encode the data as JSON. It is working, Then the final code is

$data = array(name=>SOMENAME,is_platform_access_only=>true);
$data = json_encode($data);
$header = array("Authorization: Bearer <TOKEN>");
$ch = curl_init("https://api.box.com/2.0/users/");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response1 = curl_exec($ch);
curl_close($ch);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.