0

これが問題かどうかはわかりません。cUrl を使用して wordpress フィードを読み込んでいます。これで問題なく動作します。

いくつかのワードプレスフィードで試しました。しかし、同じドメインの worpress フィードで動作させることができません

そこで、 http://www.digins.nl/blogのフィードをhttp://www.digins.nlで表示したいと思います。

ここに私が使用するコードがあります:

$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) "ホストに接続できませんでした"」というメッセージが表示されました。ブラウザでフィードを確認すると、問題なく動作しているようです。

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 に答える