2017-05-06 179 views
0

我試圖通過IPBoards REST Api上傳文件,但並不真正知道如何格式化文件。通過REST API上傳文件,IPBoard

這是它的外觀,但現在只得到錯誤:

{ 
    "errorCode": "1L296\/B", 
    "errorMessage": "NO_FILES" 
} 

代碼:

 $post = array(
     'category' => 1, 
     'author' => 1, 
     'title' => 'Test title', 
     'description' => 'test description', 
     'files' => "{'test.txt':'".file_get_contents('/home/test/test.txt')."'}", 
     ); 
     $target_url = 'https://example.com/api/downloads/files'; 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 86400); 
     curl_setopt($ch, CURLOPT_URL,$target_url); 
     curl_setopt($ch, CURLOPT_POST,1); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
     curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC); 
     curl_setopt($ch, CURLOPT_USERPWD,$apikey.":"); 
     $result = curl_exec ($ch); 
     curl_close ($ch); 

這裏的API文檔:API doc

+0

嘗試:' '文件'=> json_encode([ '的test.txt'=> json_encode(的file_get_contents( '/家庭/測試/的test.txt'))]), '而不是手動構建json字符串。 'json_encode()'將處理任何潛在的轉義問題。 –

+0

不幸運。相同的錯誤 – user1588740

回答

1

嘗試使用http_build_query方法和變化$ post數組有點:

'files' => array($filename => $contents),

而變化:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));

+0

它的工作原理,但文件被損壞? – user1588740