I am implementing Youtube push notification and implemented webhook. Youtube gives updates in the form of atom feed. My problem is i can't parse that feed.
This is the XML:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:yt="http://www.youtube.com/xml/schemas/2015">
<link rel="hub" href="https://pubsubhubbub.appspot.com" />
<link rel="self" href="https://www.youtube.com/xml/feeds/videos.xml?channel_id=UCaNoTnXcQQt3ody_cLZSihw" />
<title>YouTube video feed</title>
<updated>2018-03-01T07:21:59.144766801+00:00</updated>
<entry>
<id>yt:video:vNQyYJqFopE</id>
<yt:videoId>vNQyYJqFopE</yt:videoId>
<yt:channelId>UCaNoTnXcQQt3ody_cLZSihw</yt:channelId>
<title>Test Video 4</title>
<link rel="alternate" href="https://www.youtube.com/watch?v=vNQyYJqFopE" />
<author>
<name>Testing</name>
<uri>https://www.youtube.com/channel/UCaNoTnXcQQt3ody_cLZSihw</uri>
</author>
<published>2018-03-01T07:21:48+00:00</published>
<updated>2018-03-01T07:21:59.144766801+00:00</updated>
</entry>
<?php
$xml = '<?xml versio......';
$obj = simplexml_load_string($xml);
echo '<pre>';print_r($obj);echo '</pre>';
Screenshot
How to get the value of yt:videoId element. I am new to PHP, if I did anything wrong please correct me.
It seems the XML elements containing the yt namespace (e.g. <yt:videoId>) are not being parsed by simplexml_load_string. I don't know why but in your case the video id is also present in the <id> element you just need to extract the last value or simply cut of yt:video: in front of it. That is at least an easy workaround.
Also it works if you use a direct XPath to the <yt:videoId> element like this:
echo $obj->xpath('//yt:videoId')[0];
// output: vNQyYJqFopE
XPath always returns an array so you need to get the first element with [0].
Try this (updated)
$str = $obj->entry->id;
echo substr($str, strpos($str, "video:")+ 6);
Get the channel
$chan = $obj->entry->author->uri;
echo substr($chan , strpos($chan , "channel/")+ 8);
Related
I'm using PHP to process XML information. How can I get from the XML youtube the video id?
Question I have:
> $vid['title'] = $video->title; $vid['date'] = $video->updated;
that works. Only I also want to be getting the video id
$vid['id'] = ?
I use this XML as example , off course I use the real feed.
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns:yt="http://www.youtube.com/xml/schemas/2015" xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.w3.org/2005/Atom">
<link rel="self" href="http://www.youtube.com/feeds/videos.xml?channel_id=UCCXoCcu9Rp7NPbTzIvogpZg&orderby=published"/>
<id>yt:channel:UCCXoCcu9Rp7NPbTzIvogpZg</id>
<yt:channelId>UCCXoCcu9Rp7NPbTzIvogpZg</yt:channelId>
<title>Fox Business</title>
<link rel="alternate" href="https://www.youtube.com/channel/UCCXoCcu9Rp7NPbTzIvogpZg"/>
<author>
<name>Fox Business</name>
<uri>https://www.youtube.com/channel/UCCXoCcu9Rp7NPbTzIvogpZg</uri>
</author>
<published>2008-02-04T12:35:54+00:00</published>
<entry>
<id>yt:video:yt9cwC3bySI</id>
<yt:videoId>yt9cwC3bySI</yt:videoId>
(1) For <yt:videoId> You can try:
$vid['id'] = $video->{'yt:videoId'}
(2) For <id> You can try:
$vid['title'] = $video->id;
Or else finally try...
Read not as XML but as a string of text. Use the PHP String functions to extract the text that exists between the <yt:videoId> and </yt:videoId>.
I am trying to read the value for 3 specific XML nodes (bill_codes, sent_total, clicked_unique_total) I have done a lot of testing and I feel like I need someone with fresh eyes to look at this and help me find out what I no longer see..
I am using the simplexml_load_string function to load the XML into an array..
Here is the code that I have so far:
$xml = simplexml_load_string($content);
echo $xml->methodResponse->item->responseData->message_data->message->bill_codes;
This is the XML that I am using (comes from an API Call so I have no access to modifying/updating the structure of the XML)
<?xml version="1.0" encoding="utf-8"?>
<methodResponse>
<item>
<methodName>
<![CDATA[legacy.message_stats]]>
</methodName>
<responseData>
<message_data>
<message id="2345456">
<message_subject>
<![CDATA[#1 Item You Should Be Hoarding in 2015]]>
</message_subject>
<date_sent>2014-12-18 04:01:34</date_sent>
<message_notes>
<![CDATA[Sample Notes]]>
</message_notes>
<withheld_total>0</withheld_total>
<globally_suppressed>0</globally_suppressed>
<suppressed_total>0</suppressed_total>
<bill_codes>
<![CDATA[8578]]>
</bill_codes>
<sent_total>734273</sent_total>
<link_append_statement/>
<timezone/>
<message_name>
<![CDATA[Sample Message Name]]>
</message_name>
<optout_total>4054</optout_total>
<optout_rate_total>0.55</optout_rate_total>
<clicked_total>5363</clicked_total>
<clicked_unique>4350</clicked_unique>
<clicked_rate_unique>13.71</clicked_rate_unique>
<campaign_id>228640</campaign_id>
<campaign_type>C</campaign_type>
<included_groups>
<segment id="1208891">
<![CDATA[Segment Name Here]]>
</segment>
</included_groups>
<included_smartlists></included_smartlists>
<excluded_groups></excluded_groups>
<excluded_smartlists></excluded_smartlists>
<attributes></attributes>
<link id="40278272">
<has_name>1</has_name>
<clicked_unique_total>4350</clicked_unique_total>
</link>
</message>
</message_data>
</responseData>
<responseNum>
<![CDATA[1]]>
</responseNum>
<responseCode>
<![CDATA[201]]>
</responseCode>
</item>
</methodResponse>
No need to include the parent, just start with the ->item:
echo $xml->item->responseData->message_data->message->bill_codes;
Sample Output
I have looked through several posts on how to parse the namespace of a Google Analytics api feed (XML file) in PHP. They all say I need to have this bit of code:
$properties = $item->children('http://schemas.google.com/analytics/2009');
(or something similar, all involving the URL "http://schemas.google.com/analytics/2009")
The problem is, however, that I then get an error:
Fatal error: Call to a member function children() on a non-object
It seems as though the schemas.google.com doesn't exist, but I can't find where I need to be pointing to... any ideas?
Here's a snippet of the XML file I'm working with:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dxp="http://schemas.google.com/analytics/2009" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/">
<id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:42418300&dimensions=ga:pagePath&metrics=ga:pageviews,ga:uniquePageviews&sort=-ga:pageviews&start-date=2013-12-01&end-date=2014-01-01&max-results=10</id>
<updated>2014-01-03T22:21:31.057Z</updated>
<title type="text">Google Analytics Data for View (Profile) xxxxxxx</title>
<link rel="self" type="application/atom+xml" href="https://www.googleapis.com/analytics/v2.4/data?ids=ga:42418300&dimensions=ga:pagePath&metrics=ga:pageviews,ga:uniquePageviews&sort=-ga:pageviews&start-date=2013-12-01&end-date=2014-01-01&max-results=10"/>
<link rel="next" type="application/atom+xml" href="https://www.googleapis.com/analytics/v2.4/data?ids=ga:42418300&dimensions=ga:pagePath&metrics=ga:pageviews,ga:uniquePageviews&sort=-ga:pageviews&start-date=2013-12-01&end-date=2014-01-01&start-index=11&max-results=10"/>
<author>
<name>Google Analytics</name>
</author>
<generator>Google Analytics</generator>
<openSearch:totalResults>4826</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>10</openSearch:itemsPerPage>
<dxp:aggregates>
<dxp:metric name="ga:pageviews" type="integer" value="166656"/>
<dxp:metric name="ga:uniquePageviews" type="integer" value="132895"/>
</dxp:aggregates>
<dxp:containsSampledData>false</dxp:containsSampledData>
<dxp:dataSource>
<dxp:property name="ga:profileId" value="xxxxxxxx"/>
<dxp:property name="ga:webPropertyId" value="UA-xxxxxxxx-1"/>
<dxp:property name="ga:accountName" value="Provost"/>
<dxp:tableId>ga:42418300</dxp:tableId>
<dxp:tableName>WebSite</dxp:tableName>
</dxp:dataSource>
<dxp:endDate>2014-01-01</dxp:endDate>
<dxp:startDate>2013-12-01</dxp:startDate>
<entry>
<id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:42418300&ga:pagePath=/&start-date=2013-12-01&end-date=2014-01-01</id>
<updated>2014-01-03T22:21:31.057Z</updated>
<title type="text">ga:pagePath=/</title>
<link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
<dxp:dimension name="ga:pagePath" value="/"/>
<dxp:metric name="ga:pageviews" type="integer" value="38197"/>
<dxp:metric name="ga:uniquePageviews" type="integer" value="29385"/>
</entry>
<entry>
<id>https://www.googleapis.com/analytics/v2.4/data?ids=ga:42418300&ga:pagePath=/page2/&start-date=2013-12-01&end-date=2014-01-01</id>
<updated>2014-01-03T22:21:31.057Z</updated>
<title type="text">ga:pagePath=/page2/</title>
<link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
<dxp:dimension name="ga:pagePath" value="/page2/"/>
<dxp:metric name="ga:pageviews" type="integer" value="13964"/>
<dxp:metric name="ga:uniquePageviews" type="integer" value="10974"/>
</entry>
Here's my PHP so far:
<?php
//ini_set('auto_detect_line_endings',TRUE);
$xml = simplexml_load_file("/ga-feed.xml");
$namespaces = $xml->getNamespaces(true);
foreach ($xml->entry as $key => $value) {
$value->registerXPathNamespace('dxp', 'http://schemas.google.com/analytics/2009');
echo $value->xpath('dxp:metric[pageviews]') . "<br />\n";
}
?>
Eventually, I need to be able run some calculations with pageviews and unique pageviews (show top 4 sites, a 5th "Other" would be a combination of all the other sites) against the overall pageviews/unique pageviews. Am I at least going in a semi-correct direction?
Update: In my foreach, I removed "feed" so it just says $xml->entry as $key, and now it will display a list of text:
Array
Array
Array
Array
Array
Array
Array
Array
Array
Array
Not quite what I'm looking for... but progress? lol
Try as I may, I cannot seem to grab the value of the "Id" attribute in the nested apcm:Property element, where the "Name" attribute equals "sequenceNumber", on line 12. As you can see, there element of interest is buried in a nest of other elements with an identical name and namespace.
Using PHP, I'm having a difficult time wrapping my head around how to grab that Id value.
<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:apcm="http://ap.org/schemas/03/2005/apcm" xmlns:apnm="http://ap.org/schemas/03/2005/apnm" xmlns:georss="http://www.georss.org/georss">
<id>urn:publicid:ap.org:30085</id>
<title type="xhtml">
<apxh:div xmlns:apxh="http://www.w3.org/1999/xhtml">
<apxh:span>AP New York State News - No Weather</apxh:span>
</apxh:div>
</title>
<apcm:Property Name="FeedProperties">
<apcm:Property Name="Entitlement" Id="urn:publicid:ap.org:product:30085" Value="AP New York State News - No Weather" />
<apcm:Property Name="FeedSequencing">
<apcm:Property Name="sequenceNumber" Id="169310964" />
<apcm:Property Name="minDateTime" Value="2012-05-22T18:04:18.913Z" />
</apcm:Property>
</apcm:Property>
<updated>2012-05-22T18:04:18.913Z</updated>
<author>
<name>The Associated Press</name>
<uri>http://www.ap.org</uri>
</author>
<rights>Copyright 2012 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.</rights>
<link rel="self" href="http://syndication.ap.org/AP.Distro.Feed/GetFeed.aspx?idList=30085&idListType=products&maxItems=20" />
<entry>
...
</entry>
</feed>
You have to register the namespaces, and use the [] predicate to identify which Property element you are interested in. It is safest if you do NOT use double slash, i.e., if you start the look up from the document element.
<?php
$xml = <<<EOD
...
EOD;
$sxe = new SimpleXMLElement($xml);
$sxe->registerXPathNamespace('apcm', 'http://ap.org/schemas/03/2005/apcm');
$sxe->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom');
$result = $sxe->xpath('/atom:feed/acpm:Property[#Name=\'FeedProperties\']/acpm:Property[#Name=\'FeedSequencing\']/acpm:Property[#Name=\'sequenceNumber\']/#Id');
foreach ($result as $sequenceNumber) {
echo $sequenceNumber . "\n";
}
?>
Note that there may theoretically be multiple sibling Property elements with the same #Name and so this Xpath may produce multiple nodes (#Id values).
Trying to parse an iTunes Atom feed with a PHP script. If you visit the iTunes RSS Generator, you can generate an Atom feed like this:
http://itunes.apple.com/us/rss/topsongs/limit=10/genre=16/explicit=true/xml
which gives an iTunes RSS feed result like this:
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:im="http://itunes.apple.com/rss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<id>http://itunes.apple.com/us/rss/topsongs/limit=10/genre=16/explicit=true/xml</id><title>iTunes Store: Top Songs in Soundtrack</title><updated>2012-04-01T07:22:41-07:00</updated><link rel="alternate" type="text/html" href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewTop?id=17&popId=1"/><link rel="self" href="http://itunes.apple.com/us/rss/topsongs/limit=10/genre=16/explicit=true/xml"/><icon>http://phobos.apple.com/favicon.ico</icon><author><name>iTunes Store</name><uri>http://www.apple.com/itunes/</uri></author><rights>Copyright 2008 Apple Inc.</rights>
<entry>
<updated>2012-04-01T07:22:41-07:00</updated>
<id im:id="509605055">http://itunes.apple.com/us/album/eyes-open/id509605019?i=509605055&uo=2</id>
<title>Eyes Open - Taylor Swift</title>
<im:name>Eyes Open</im:name>
<link rel="alternate" type="text/html" href="http://itunes.apple.com/us/album/eyes-open/id509605019?i=509605055&uo=2"/>
<im:contentType term="Music" label="Music"><im:contentType term="Track" label="Track"/></im:contentType>
<category term="Soundtrack" scheme="http://itunes.apple.com/us/genre/music-soundtrack/id16?uo=2" label="Soundtrack"/>
<link title="Preview" rel="enclosure" type="audio/x-m4a" href="http://a2.mzstatic.com/us/r1000/116/Music/88/70/a6/mzi.gcauwkkw.aac.p.m4a" im:assetType="preview"><im:duration>30000</im:duration></link>
<im:artist href="http://itunes.apple.com/us/artist/taylor-swift/id159260351?uo=2">Taylor Swift</im:artist>
<im:price amount="1.29000" currency="USD">$1.29</im:price>
<im:image height="55">http://a3.mzstatic.com/us/r1000/069/Music/v4/15/59/19/15591949-a525-99e8-0c50-45697b0ec78b/UMG_cvrart_00602527969206_01_RGB72_1200x1200_12UMGIM10247.55x55-70.jpg</im:image>
<im:image height="60">http://a5.mzstatic.com/us/r1000/069/Music/v4/15/59/19/15591949-a525-99e8-0c50-45697b0ec78b/UMG_cvrart_00602527969206_01_RGB72_1200x1200_12UMGIM10247.60x60-50.jpg</im:image>
<im:image height="170">http://a3.mzstatic.com/us/r1000/069/Music/v4/15/59/19/15591949-a525-99e8-0c50-45697b0ec78b/UMG_cvrart_00602527969206_01_RGB72_1200x1200_12UMGIM10247.170x170-75.jpg</im:image>
<rights>2012 Universal Republic Records, a division of UMG Recordings, Inc.</rights>
<im:releaseDate label="March 20, 2012">2012-03-20T00:00:00-07:00</im:releaseDate>
<im:collection><im:name>The Hunger Games (Songs from District 12 and Beyond)</im:name><link rel="alternate" type="text/html" href="http://itunes.apple.com/us/album/hunger-games-songs-from-district/id509605019?uo=2"/><im:contentType term="Music" label="Music"><im:contentType term="Album" label="Album"/></im:contentType></im:collection>
(etc...)
With the PHP script, I'm able to get results for things like the title, id, im:image for each [entry] to use in the script output. What I need to get is the url from one of the link entries. Specially I need the url from the "Preview" link:
<link title="Preview" rel="enclosure" type="audio/x-m4a" href="http://a2.mzstatic.com/us/r1000/116/Music/88/70/a6/mzi.gcauwkkw.aac.p.m4a" im:assetType="preview"><im:duration>30000</im:duration></link>
In this case, we would need the a2.mzstatic.com/us/r1000/116/Music/88/70/a6/mzi.gcauwkkw.aac.p.m4a link for use in the script results for each of the 10 entries.
How do I capture that href for the .m4a audio file "Preview" link in the above Atom feed?
Here is a portion of the PHP script where we get the contents of the iTunes Atom url, cycle through the 10 results, and generate HTML for each entry via $rssresults that is called in a site template.
$string = file_get_contents('http://itunes.apple.com/us/rss/topsongs/limit=10/genre=16/explicit=true/xml');
// Remove the colon ":" in the <xxx:yyy> to be <xxxyyy>
$string = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $string);
if ($f = #fopen($cache_file, 'w')) {
fwrite ($f, $string, strlen($string));
fclose($f);
}
}
$xml = simplexml_load_string($string);
// Output
$rssresults = '';
$count = 1;
$max = 11;
foreach ($xml->entry as $val) {
if ($count < $max) {
$rssresults .= '
<img src="'.$val->imimage[2].'" alt="'.$val->title.'">
// .m4a preview url?
<div><a href=" ">Preview</div>
<div><strong>'.$count.'. '.$val->title.'</strong></div>
<div> from '.$val->imcollection->imname.'</div>;
}
$count++;
}
Any ideas on how to add the ".m4a preview url" to the above script for each entry?
Appreciate any help.
In your foreach loop try $val->link[1]["href"] would give you the URL
foreach ($xml->entry as $val) {
// echo the link of Preview
echo $val->link[1]["href"];
}
Explanation:
As there are multiple link entry you can access them by array index. So index 1 is used to access the second link entry. Each attribute of an Element can be accessed by its name as a key to the element. Hence $val->link[1]["href"] would give you http://a2.mzstatic.com/us/r1000/116/Music/88/70/a6/mzi.gcauwkkw.aac.p.m4a
Viper-7