0

我不确定这是否是问题所在。我正在使用 cUrl 来阅读 wordpress 提要。这现在可以了。

我尝试了几个 wordpress 提要。但我无法让它与同一域上的 wopress 提要一起使用

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

这是我使用的代码:

$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 我收到了“字符串(24)“无法连接到主机”消息。如果我通过浏览器检查提要,它似乎工作正常。

4

2 回答 2

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 运行它时它工作正常。

于 2013-11-01T10:30:12.217 回答
0

问题仍然是无法从同一域读取提要。所以此时我通过 feedburner 阅读了提要。不幸的是,它减慢了速度。

于 2013-11-03T14:40:15.633 回答