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

时间:2018-07-17 15:41:02

标签: 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', 
        //'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);

我得到的'file.tmp'总是大小为0。我怀疑我为CURLOPT_HTTPHEADER设置了一个错误, 谢谢大家的帮助。

0 个答案:

没有答案