Rose debug info
---------------

/core.php, line 2
Error 8192: explode(): Passing null to parameter #2 ($string) of type string is deprecated

/core.php, line 2
Error 8192: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated

/core.php, line 2
Error 8192: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated

Как распарсить <yandex:full-text>

Cуть задачи: есть xml, отдаваемый для Яндекс.Новости, необходимо из него получить содержимое тега при помощи SimpleXML. Трудность возникает из-за использования в данном случае пространства имен (namespace).

Решение, на самом деле, простое, главное хорошо прочитать мануал:

<?php
// Создать контекст и получить XML с сайта
$path = 'http://inforos.ru/yandex.xml';
$ctx = stream_context_create( array(
  'http' => array( 'timeout' => 6 )
) );
$file = file_get_contents( $path, 0, $ctx );
if ( $file ) {
    // Распарсить полученный XML
    $rss = simplexml_load_string( $file );
    foreach ( $rss->channel->item as $item ){
        $namespaces = $item->getNameSpaces( true );
        $yandex = $item->children( $namespaces[ 'yandex' ] );
        $full_text = (string) $yandex->{'full-text'};
        echo ( $full_text );
        echo ( "\n\n<hr />\n\n" );
    }
}
?>
Поделиться
Отправить
 41   11 мес   php