简体   繁体   中英

PHP equivalent of CURL having -i in command line

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

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);
 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM