use*_*895 4 php rest json file-get-contents owncloud
我为我自己的Cloud 应用程序编写了一个REST 接口。我有一个方法getFileFromRemote($path)应该返回一个包含文件内容的 JSON 对象。不幸的是,这只在我指定的文件$path是纯文本文件时才有效。当我尝试调用图像方法或PDF状态代码为 200 但响应为空时。为了返回文件内容,我使用它file_get_contents来检索内容。
注意:我知道 ownCloud 有一个 WebDAV 接口,但我只想用 REST 来解决这个问题。
编辑 这是代码服务器端(ownCloud):
public function synchroniseDown($path)
{
$this->_syncService->download(array($path));//get latest version
$content = file_get_contents($this->_homeFolder.urldecode($path));
return new DataResponse(['path'=>$path, 'fileContent'=>$content]);
}
Run Code Online (Sandbox Code Playgroud)
第一行检索在 ownCloud 服务器上下载内容并完全正常工作。
您可能必须修改base64_encode文件内容才能使 json_encode/decode 正确处理它:
return new DataResponse([
'path'=>$path,
'fileContent' => base64_encode($content) // convert binary data to alphanum
]);
Run Code Online (Sandbox Code Playgroud)
然后,当通过第二方接收文件时,您必须始终:
$fileContent = base64_decode($response['fileContent']);
Run Code Online (Sandbox Code Playgroud)
这只是一种,但却是处理该问题的最简单方法。顺便说一句,迟早你会发现Mime-Type回应是有用的。
| 归档时间: |
|
| 查看次数: |
10521 次 |
| 最近记录: |