0

I have below code running fine from command line :

curl https://view-api.box.com/1/sessions \
-H "Authorization: Token API KEY" \
-H "Content-Type: application/json" \
-d '{"document_id": "626ef23c0c924328b6f61505786df619", "duration": 60}' \
-X POST \
-i

Here is what I have tried :

$curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'https://view-api.box.com/1/sessions',
        CURLOPT_HTTPHEADER => array('Authorization :Token iiiiiiiiiiiiiiiiiiiijjjjj','Content-type : application/json'),
        CURLOPT_POST => 1,
        CURLOPT_POSTFIELDS => array(document_id => $docid,duration => 60)                  
        ));
        $resp = curl_exec($curl);
        curl_close($curl);  
        echo $resp;

Please suggest a PHP equivalent of the request . I am not sure how duration and -i will be handled here .

I am getting below error message in the above curl request .

Modified Request Code :
$curl1 = curl_init();
                curl_setopt_array($curl1, array( 
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_URL => 'https://view-api.box.com/1/sessions',
                CURLOPT_HTTPHEADER => array('Authorization :Token API KEY','Content-type : application/json'),
                CURLOPT_POST => 1,
                CURLOPT_POSTFIELDS => array("document_id" => $documentid)
                ));
                $resp1 = curl_exec($curl1);
                curl_close($curl1);  
                echo $resp1;

Error :

{"message": "Unsupported media type 'multipart/form-data; boundary=----------------------------6a38938da01d' in request.", "type": "error", "request_id": "5f212af123cb4f37be9926431714fc63"}

I have mentioned Content-Type : application/josn whereas it is taking multipart/form-data . Please help me !!

3
  • See this answer for -I argument (-i doesn't exist in the manpage, it must be wrong in your code). This answer for how to put POST in PHP curl. Commented Sep 5, 2014 at 7:25
  • @DanFromGermany -i --include option is to include the HTTP-header in the output, I've use it before:) Commented Sep 5, 2014 at 8:58
  • @jfly thx, you are right, so many parameters I didn't see it :) Commented Sep 5, 2014 at 9:05

1 Answer 1

3

It's simple, just do as the manual says:

 <?php
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL,"https://view-api.box.com/1/sessions");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1); // for -i option

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
    array('document_id' => '626ef23c0c924328b6f61505786df619', 'duration' => 60)));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Authorization: Token jizb4owywrjwi0qbcjh3l5q40rmdxt63'
    ) ); // encode data into JSON format.

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the transfer as a string

    $server_output = curl_exec ($ch);
    print "curl response is:" . $server_output;
    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.