【问题标题】:save multiple twitter profiles to single xml file将多个 Twitter 配置文件保存到单个 xml 文件
【发布时间】:2010-06-01 10:56:06
【问题描述】:

您好,我正在尝试为客户确定关键的 Twitter 影响者,我有一个包含 170 个 Twitter id 的列表,我需要了解更多信息。

我希望脚本循环遍历 Twitter Id 列表并将输出保存到单个 XML 文件 -

http://twitter.com/users/show/mattmuller.xml

http://twitter.com/users/show/welovecrowds.xml

http://twitter.com/users/show/jlyon.xml

本质上,我需要编写一个脚本来传递每个 url 并将输出保存为服务器上的单个 xml 文件。关于如何使用 PHP 执行此操作的任何想法,我是否需要使用 Curl?

感谢您的帮助。

干杯

乔纳森

【问题讨论】:

    标签: php xml twitter


    【解决方案1】:

    这是一个简单的例子,说明如何使用 cURL 实现这一目标:

    // array of twitter accounts
    $ids = array('mattmuller', 'welovecrowds', 'jlyon' /* (...) */);    
    
    $ch = curl_init();
    $url = 'http://twitter.com/users/show/';
    $xml = '<?xml version ="1.0" encoding="utf-8"?>';
    
    // make curl return the contents instead of outputting them
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    foreach($ids as $id) {
        // set the url base on the account id
        curl_setopt($ch, CURLOPT_URL, "$url$id.xml");
        // fetch the url contents and remove the xml heading
        $xml .= preg_replace ('/\<\?xml .*\?\>/i', '', curl_exec($ch));
    }
    
    // save the contents of $xml into a file
    file_put_contents('users.xml', $xml);
    

    【讨论】:

    • WTF - 你在开玩笑吗!!!!这就像一个魅力 - 非常感谢!!!!!!!!!!!!!!!!!!!
    • 只是一个简单的问题,我是否需要以不同的方式构建 xml 架构才能在 excel 中打开它?目前,由于结构是 它会引发错误,但如果我在 xml 中只有一个用户,那很好 - 有什么想法吗?谢谢乔纳森
    • 不确定 Excel 如何处理纯 XML 文件,但如果您想让它真正可移植,您可以将其转换为 CSV:codestips.com/php-xml-to-csv
    猜你喜欢
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多