2017-05-06 166 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