【问题标题】:JSON file_get_contents empty fileJSON file_get_contents 空文件
【发布时间】:2012-06-19 16:30:26
【问题描述】:

为什么会输出一个0字节的文件?

<?php
$jsonurl = "http://do.convertapi.com/Web2Pdf/json/?curl=http://stackoverflow.com/";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
file_put_contents('converted.pdf', $json_output);
?>

【问题讨论】:

    标签: file-get-contents json


    【解决方案1】:

    我猜你需要做以下事情:

    file_put_contents('converted.pdf', base64_decode($json_output->File));
    

    您实际上所做的是检索 JSON 数据并将其解码为 PHP 对象。您需要从该对象中检索“文件”属性的内容并将其放入文件中。

    【讨论】:

    • 非常感谢,文件现在大小正确,但我的 pdf 查看器显示文件已损坏。有什么想法吗?
    • Opps 对不起.. 离开了 base64_decode。忽略我的最后评论,因为它完美无缺。再次感谢 ioseb。
    【解决方案2】:

    $json_output 是一个对象,但 file_put_contents 只接受字符串或数组

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 2014-01-03
      • 1970-01-01
      相关资源
      最近更新 更多