dongxiejie9387 2013-10-08 12:57 采纳率: 100%
浏览 72
已采纳

如何保存通过curl获取的xml文件?

I have this proxy done using curl. I t successfully gets the xml to be displayed in the page. I'm trying to save a copy locally with no success, i tried (already in the complete code below):

$fp = fopen('data.xml', 'w');
fwrite($fp, $xml);
fclose($fp);

But I got no error and an empty "data.xml" file...How can this be done? thanks

[EDIT2]: solved, i just added the flag: curl_setopt($session, CURLOPT_RETURNTRANSFER, true); and done! (updated in the code below The code with @spell suggestion.

define ('HOSTNAME', 'http://www.camara.gov.br');


$path = "http://www.camara.gov.br/SitCamaraWS/Deputados.asmx/ObterDeputados?";
$url = HOSTNAME.$path;

$session = curl_init($path);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPGET, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36');

header("Content-Type: text/xml");


$xml = curl_exec($session);


curl_close($session);

file_put_contents ('./data.xml', $xml); 
?>
  • 写回答

1条回答 默认 最新

  • douzai9405 2013-10-08 13:15
    关注

    You can just use file_put_contents function.

    In your case it will be:

    // Make the call
    $xml = curl_exec($session);
    
    
    // done, shutDown.
    curl_close($session);
    
    if (file_put_contents ('./data.xml', $xml) !== false) {
         echo 'Success!';
    } else {
         echo 'Failed';
    }
    

    And

    But I got no error and an empty "data.xml" file

    Next time provide an error

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据