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 !!

Brijesh Kushwaha
  • 273
  • 1
  • 3
  • 11
  • See [this answer](http://stackoverflow.com/questions/1378915/header-only-retrieval-in-php-via-curl) for `-I` argument (`-i` doesn't exist in the manpage, it must be wrong in your code). [This answer](http://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code) for how to put `POST` in PHP curl. – Daniel W. Sep 05 '14 at 07:25
  • @DanFromGermany `-i --include option` is to include the HTTP-header in the output, I've use it before:) – jfly Sep 05 '14 at 08:58
  • @jfly thx, you are right, so many parameters I didn't see it :) – Daniel W. Sep 05 '14 at 09:05

1 Answers1

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);
 ?>
jfly
  • 7,715
  • 3
  • 35
  • 65