I am trying to use Google Trends' news data as a proof of concept for a project I am doing. I have found the following code for parsing data from an RSS feed and have looked at several examples of custom tag fetching online but none seems to work for me. I can't seem to parse the "ht:news_item_url" tag from the RSS feed with the following code;
$domOBJ = new DOMDocument();
$domOBJ->load("https://trends.google.com.tr/trends/trendingsearches/daily/rss?geo=TR");//XML page URL
$content = $domOBJ->getElementsByTagName("item");
foreach( $content as $data )
{
$title = $data->getElementsByTagName("title")->item(0)->nodeValue;
$link = $data->getElementsByTagName("link")->item(0)->nodeValue;
$description = $data->getElementsByTagName("description")->item(0)->nodeValue;
$newsLink = $data->getElementsByTagNameNS("https://trends.google.com.tr/trends/trendingsearches/daily/rss?geo=TR", "ht:news_item_url")->item(0)->nodeValue;
echo "$newsLink";
}
The RSS that I am trying to parse can be found here: https://trends.google.com.tr/trends/trendingsearches/daily/rss?geo=TR
There are a couple of mistakes on your call to getElementsByTagNameNS()
. The first is you are using the wrong value for the namespace URI, you are using the URL of the source and not the namespace URI, this you should be using the value of xmlns:ht
from...
<rss xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:ht="https://trends.google.com.tr/trends/trendingsearches/daily"
version="2.0">
The second part is that you need to use the local name of the elements you are after, this is the element name without the namespace prefix, so just news_item_url
.
The end result should be...
$newsLink = $data->getElementsByTagNameNS("https://trends.google.com.tr/trends/trendingsearches/daily"
, "news_item_url")