2016-07-02 2 views
0

PHP 및 CURL 호출을 사용하여 Dropbox v2 API 호출에 액세스 해보십시오. 여기 모래 상자 https://dropbox.github.io/dropbox-api-v2-explorer/#files_search에 따라 "쇼 코드"는 다음과 같습니다. 내가 그것을 실행하면Dropbox v2 API PHP CURL 호출 문제 해결

curl -X POST https://api.dropboxapi.com/2/files/search \ 
    --header 'Authorization: Bearer myAccessTokenHere \ 
    --header 'Content-Type: application/json' \ 
    --data '{"path":"/My Cars","query":"\".j\"","start":0,"max_results":50}' 

내 PHP 코드는 내가

응답 = 얻을이

function performSearch() 
{ 
    $cheaders = array("Authorization: Bearer myAccessTokenHere", 
       'Content-Type: application/json', 
       'Dropbox-API-Arg: {"path":"/My Cars","query":"\".j\"","start":"0","max_results":"50"}'); 

    $ch = curl_init('https://content.dropboxapi.com/2/files/search'); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $response = curl_exec($ch); 

    echo "\n response = $response"; 
    curl_close($ch); 
} 

처럼 보이는 { "오류": "찾을 수 없음"}

답변

1

다음을 사용해주세요.

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, "https://api.dropboxapi.com/2/files/search"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"path\":\"/My Cars\",\"query\":\"\".j\"\",\"start\":0,\"max_results\":50}"); 
curl_setopt($ch, CURLOPT_POST, 1); 

curl_setopt($ch, CURLOPT_HTTPHEADER, [ 
    "Authorization: Bearer myAccessTokenHere", 
    "Content-Type: application/json" 
]); 

$result = curl_exec($ch); 
if (curl_errno($ch)) { 
    echo 'Error:' . curl_error($ch); 
} 
curl_close ($ch); 
echo "\n response = $result"; 
+0

응답 해 주셔서 감사합니다 이 코드는 오류와 함께 반환됩니다. 어떤 부분에 대해 이야기하고 있는지 확실하지 않습니다. 응답 = API 함수 "파일/검색"에 대한 호출 오류 : 요청 본문 : 입력을 JSON으로 디코딩 할 수 없습니다 – JMoskwa

+0

오류는 귀하의 요청 본문에있는 JSON (즉, AP에 대한 매개 변수 내가 부름)이 유효하지 않습니다. ''\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ , ""\ "start \": 0, \ "max_results \": 50} ") – Greg

0

포인터를 주셔서 감사합니다 - 새 코드 - 오류가 발생하지 않고 작동하지만 이제는 결과가 반환되지 않습니다 .... 실제 호출 외부에서 일부 변환을 추가하여 JSON 문자열 다음은 새로운 코드 여기

$path = "/My Cars"; 
$query = ".j"; 
$start = 0; 
$max_results = 50; 

$accessToken = $this->accessToken; 
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, "https://api.dropboxapi.com/2/files/search"); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      curl_setopt($ch, CURLOPT_POST, 1); 


$arr = array('path'=>$path, "query"=>$query, "start"=>$start, "max_results"=>$max_results, 
            "mode"=>(array(".tag"=>"filename_and_content"))); 
$jArr = json_encode($arr); 

$arrNull = array("body"=>""); 
$jNull = json_encode($arrNull); 

echo "\n\n" . $jArr; 
echo "\n\n" . $jNull; 

curl_setopt($ch, CURLOPT_POSTFIELDS, $jArr); 

curl_setopt($ch, CURLOPT_HTTPHEADER, [ 
          "Authorization: Bearer $accessToken", 
          "Content-Type: application/json" 
      ]); 


$result = curl_exec($ch); 
if (curl_errno($ch)) { 
      echo 'Error:' . curl_error($ch); 
} 
curl_close ($ch); 
echo "\n result = $result"; 

return ($result); 

내가 출력을 위해 얻을 무엇이다

전달되고 있었다 - 아니하는 오류하지만 내용을 ???? (다른 디렉토리를 시도했습니다. "/ My Cars"는 6 개의 결과를 반환합니다.)

{ "path": "/ My Cars", "query": ".j", "start": 0 "MAX_RESULTS"50, "모드"{ ". 태그": "filename_and_content"}}

{ "본체": ""}

결과 = { "일치"[] " 더 "거짓,"시작 ": 6}

+0

샌드 박스가 실제 API 끝점과 다르게 작동하는 것 같습니다."query "대신"j " .jpg "그리고 그 디렉토리에있는 6 jpg 파일을 반환했습니다. 나는 또한 모드를 추가하려고 :"{ ".태그 ":"filename_and_content "}}하지만 나는 그것을 밖으로두고 그래서 오류가 발생합니다.이 스레드는 v2 인터페이스 문제가있는 사람이 도움이되기를 바랍니다 – JMoskwa

0

해결 - =

$ 쿼리에 $ 쿼리 변경".JPG ";

결과가 다시 나타납니다. FYI

: 더 이상 코드에 필요한 상기

$ arrNull = 배열 ​​("본체"= "" ")는 다음 라인이고; $ jNull = json_encode ($ arrNull);

하고 "에코"기능은 생산에 사용하는 호출을 제거 ..., 희망이

요한 다른 사람을 도움이 될 것입니다 내 오류를 해결 돕는

감사