2016-01-15 29 views
0

我是Dropbox API集成的新手,我使用PHP cURL擴展來調用HTTP REST API,並且當我嘗試提出請求時我收到以下字符串:Dropbox HTTP API - PHP cURL自動添加標題項邊界

Error in call to API function "files/list_folder": 
Bad HTTP "Content-Type" header: 
"text/plain; boundary=----------------------------645eb1c4046b". 
Expecting one of "application/json", "application/json; charset=utf-8", 
"text/plain; charset=dropbox-cors-hack". 

我的代碼非常相似,這個發送此:

$sUrl = "https://api.dropboxapi.com/2/files/list_folder"; 
$oCurl = curl_init($sUrl); 
$aPostData = array('path' => '', 'recursive' => true, 'show_hidden' => true); 
$sBearer = "MY_TOKEN"; 
$aRequestOptions = array(
     CURLOPT_POST => true, 
     CURLOPT_HTTPHEADER => array('Content-Type: text/plain', 
      'Authorization: Bearer ' . $sBearer), 
     CURLOPT_POSTFIELDS => $aPostData, 
     CURLOPT_RETURNTRANSFER => true); 
curl_setopt_array($aRequestOptions); 
$hExec = curl_exec($oCurl); 
if ($hExec === false){ 
    // Some error info in JSON format 
} else { 
    var_dump($hExec); 
} 

回答

1

當你擁有它,你正在做一個多形式上傳,這是不API的期望。

有你需要做不同的幾件事情:

  • 你應該在體內發送了參數JSON。
  • 您應該相應地將Content-Type設置爲application/json
  • /files/list_folder上沒有show_hidden參數,但也許你打算髮送include_deleted
  • curl_setopt_array method需要兩個參數,第一個參數應該是捲曲句柄。

這裏是你的代碼的更新版本爲我的作品:

<?php 

$sUrl = "https://api.dropboxapi.com/2/files/list_folder"; 
$oCurl = curl_init($sUrl); 
$aPostData = array('path' => '', 'recursive' => true, 'include_deleted' => true); 
$sBearer = "MY_TOKEN"; 
$aRequestOptions = array(
     CURLOPT_POST => true, 
     CURLOPT_HTTPHEADER => array('Content-Type: application/json', 
      'Authorization: Bearer ' . $sBearer), 
     CURLOPT_POSTFIELDS => json_encode($aPostData), 
     CURLOPT_RETURNTRANSFER => true); 
curl_setopt_array($oCurl, $aRequestOptions); 
$hExec = curl_exec($oCurl); 
if ($hExec === false){ 
    // Some error info in JSON format 
} else { 
    var_dump($hExec); 
} 

?> 
+0

非常感謝,你是對的,這是隻此評論代碼,只需添加'json_encode'和改變'Content-type'頭部起作用。 – MikeVelazco