使用cUrl读取同一域上的feed

时间:2013-10-31 21:11:40

标签: php wordpress curl xml-parsing subdomain

我不确定这是不是问题。我正在使用cUrl来阅读wordpress feed。现在这个工作正常。

我用几个wordpress feed尝试了它。但我无法让它在同一个域上使用worpress Feed

所以我想在http://www.digins.nl/blog上显示来自http://www.digins.nl结果的Feed。

这是我使用的代码:

$wpsite = $wpsite.'/feed';
   $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $wpsite);
  curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 15);
  $returned = curl_exec($ch);
  curl_close($ch);

  // $xml === False on failure
  $xml = simplexml_load_string($returned, 'SimpleXMLElement', LIBXML_NOCDATA);

它是不是在同一个域上工作或者我的代码有问题吗?

使用curl_error我得到'string(24)“无法连接到主机”'消息。如果我通过我的浏览器检查提要,它似乎工作正常。

2 个答案:

答案 0 :(得分:1)

你为什么使用卷曲?您可以使用file_get_contents()函数轻松加载xml数据。

试试这个:

<pre>
<?php

$xml = simplexml_load_string(
    file_get_contents('/blog/feed/'),
    'SimpleXMLElement',
    LIBXML_NOCDATA
);

print_r($xml);

?>

编辑:以下是使用CURL的另一种方法(已确认正常工作)

<?php

// CURL HTTP Get Helper Function
function CurlGet($fromUrl)
{
    // create curl resource
    $ch = curl_init();

    // set url
    curl_setopt($ch, CURLOPT_URL, $fromUrl);

    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // $output contains the output string
    $output = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch);

    // Return Output
    return (!empty($output) ? $output : false);
}   

// Get XML Data From RSS Feed
$xml_str = CurlGet('http://www.digins.nl/blog/feed/');

// Check If We Have Data
if ($xml_str)
{
    // Load XML
    $xml = simplexml_load_string($xml_str, 'SimpleXMLElement', LIBXML_NOCDATA);

    // Debug
    echo '<pre>';
    print_r($xml);
    echo '</pre>';
}
else
{
    // Curl request failed
}

?>

这是我测试时得到的输出:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 2.0
        )

    [channel] => SimpleXMLElement Object
        (
            [title] => Digins news
            [link] => http://www.digins.nl/blog
            [description] => Just another WordPress site
            [lastBuildDate] => Thu, 31 Oct 2013 10:24:25 +0000
            [language] => en-US
            [generator] => http://wordpress.org/?v=3.7.1
            [item] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [title] => test3
                            [link] => http://www.digins.nl/blog/test3/
                            [comments] => http://www.digins.nl/blog/test3/#comments
                            [pubDate] => Thu, 31 Oct 2013 10:24:25 +0000
                            [category] => Uncategorized
                            [guid] => http://www.digins.nl/blog/?p=9
                            [description] => 3e bericht
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [title] => hello world 2
                            [link] => http://www.digins.nl/blog/hello-world-2/
                            [comments] => http://www.digins.nl/blog/hello-world-2/#comments
                            [pubDate] => Thu, 31 Oct 2013 10:07:35 +0000
                            [category] => Uncategorized
                            [guid] => http://www.digins.nl/blog/?p=5
                            [description] => Dit is een test bericht
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [title] => Hello world!
                            [link] => http://www.digins.nl/blog/hello-world/
                            [comments] => http://www.digins.nl/blog/hello-world/#comments
                            [pubDate] => Wed, 25 Sep 2013 21:25:08 +0000
                            [category] => Uncategorized
                            [guid] => http://www.digins.nl/blog/?p=1
                            [description] => Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!
                        )

                )

        )

)

如果我更新的CURL方法不起作用,那么您的托管服务提供商就会遇到问题...请与他们核对,因为当我从开发PC运行它时,它可以正常工作。

答案 1 :(得分:0)

问题仍然是无法从同一个域读取Feed。所以此刻我通过feedburner读取了饲料。不幸的是,它减慢了速度。