0

I have a functionality like below and getting an error String could not be parsed as XML

$category_feed_url = "http://www.news4u.com/blogs/category/articles/feed/";
$file = file_get_contents($category_feed_url);
$xml = new SimpleXMLElement($file);

foreach($xml->channel->item as $feed)
{
  echo $feed->link;
  echo $feed->title;
  ...

why this error has occurred.

Manojkumar
  • 1,351
  • 5
  • 35
  • 63
  • 1
    The string passed to SimpleXMLElement should be well formed xml. The contents from your URL is plain HTML. – air4x Oct 29 '12 at 09:05

2 Answers2

1

The URL points to an HTML document.

It is possible for a document to be both HTML and XML, but this one isn't.

It fails because you are trying to parse not-XML as if it was XML.

See How to parse and process HTML with PHP? for guidance in parsing HTML using PHP.

You seem to be expecting an RSS feed though, and that document doesn't resemble one or reference one. The site looks rather spammy, possibly that URI used to point to an RSS feed but the domain has now fallen to a link farm spammer. If so, you should find an alternative source for the information you were collecting.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

"String could not be parsed as XML", your link is an html page.

Ydhem
  • 928
  • 2
  • 14
  • 36