如何在php中使用curl、发布json数据和下载文件流

如何在php中使用curl、发布json数据和下载文件流,php,curl,Php,Curl,下面的代码是php代码,但它不下载任何东西 $postdata=$jsonstr; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', //'Con

下面的代码是php代码,但它不下载任何东西

 $postdata=$jsonstr;
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);        
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json', 
        //'Content-Type: application/x-www-form-urlencoded"',
        'Content-Length: ' . strlen($postdata)
  ));        
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata );
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    echo "<br/>-------------------<br/>";
    //print_r($output);
    echo "<br/>-------------------<br/>";
    mySaveFile($output, "./file.tmp");        
    curl_close($ch);
$postdata=$jsonstr;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,数组(
'内容类型:application/json',
//“内容类型:application/x-www-form-urlencoded”,
“内容长度:”.strlen($postdata)
));        
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postdata);
$output=curl\u exec($ch);
$info=curl\u getinfo($ch);
回声“
--------------
”; //打印(输出); 回声“
--------------
”; mySaveFile($output,“./file.tmp”); 卷曲关闭($ch);
我得到的'file.tmp'总是大小为0。我怀疑我为CURLOPT_HTTPHEADER设置了一个错误,
谢谢大家的帮助。

您检查了
$output
$info
的内容了吗?您还应该显示
mySaveFile()的代码。
$output不只是打印东西。$info如下:$output是不打印东西的,$info如下:数组([url]=>[content\u type]=>[http\u code]=>415[header\u size]=>73[请求大小]=>265[文件时间]=>1[ssl验证结果]=>0[重定向计数]=>0[总时间]=>0.109[姓名查找时间]=>0[连接时间]=>0.063[预传输时间]=>0.063[上传大小]=>109[下载大小]=>0[下载速度]=>0[上传速度]=>1000[下载内容长度]=>0[上传内容长度]=>0[开始传输时间]=>0.109[redirect\u time]=>0[http\u code]=>415这是您的状态代码。我们对您正在使用的API/服务一无所知,因此我无法确切说明您为什么会获得该状态,但您应该阅读该页面上的信息,并查阅您尝试使用的API。