我在命令行以下代码运行良好:
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
以下是我的尝试:
$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;
请建议PHP等效的请求。我不确定如何在此处理duration
和-i
。
我在上面的curl请求中收到以下错误消息。
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"}
我已经提到了Content-Type:application / josn,而它正在采用multipart / form-data。请帮帮我!!
答案 0 :(得分:3)
这很简单,就像manual所说的那样:
<?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);
?>