Get video Id from XML youtube video using PHP - php

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>.

Related

Unable to parse atom feed

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);

Google Analytics and XML namespace issue

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

PHP Parsing String of HTML

thanks for taking a second to look at this. I'm using a PHP script to get the source code of a page from a URL, and then I am attempting to parse it and display a certain part text. The problem appears to be that when I get the source for the link (with:
$data = file_get_contents($link);
) the variable $data stores it as HTML and not as just a string. I'm pretty new to PHP so I'm not 10% sure if that's the case, but I do know that if I try to display $data in any way it displays not as plain text but as HTML with HTML formatting.
Ordinarily this wouldn't be an issue but I am trying to get the value of something inside an HTML tag, like this:
$search = strpos($data, $searchterm);
and because it is either stored as HTML instead of as plain text or it is treated that way, strpos() will only search through the text that would be displayed if I loaded the page.
To be more specific, in my file (YouTube data about my account) it would only look at what would display if it were to be loaded as HTML, which is pure nonsense.
Here is the source that I want it to search through (I have replaced my account name with 'MyAccount' for privacy):
<entry gd:etag="W/"A0MFR347eCp7I2A9WhNQEU4."" xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:yt="http://gdata.youtube.com/schemas/2007">
<id>tag:youtube.com,2008:user:A1RDBCYeYWY9dydB9MmPlg</id>
<published>2007-01-23T15:39:30.000Z</published>
<updated>2012-11-17T08:03:36.000Z</updated>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://gdata.youtube.com/schemas/2007#userProfile"/>
<title>MyAccount</title>
<summary/>
<link rel="alternate" type="text/html" href="http://www.youtube.com/channel/UCA1RDBCYeYWY9dydB9MmPlg"/>
<link rel="self" type="application/atom+xml" href="http://gdata.youtube.com/feeds/api/users/A1RDBCYeYWY9dydB9MmPlg?v=2"/>
<author>
<name>MyAccount</name>
<uri>http://gdata.youtube.com/feeds/api/users/MyAccount</uri>
<yt:userId>A1RDBCYeYWY9dydB9MmPlg</yt:userId>
</author>
<yt:channelId>UCA1RDBCYeYWY9dydB9MmPlg</yt:channelId>
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.liveevent" href="http://gdata.youtube.com/feeds/api/users/MyAccount/live/events?v=2" countHint="0"/>
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.favorites" href="http://gdata.youtube.com/feeds/api/users/MyAccount/favorites?v=2" countHint="0"/>
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.contacts" href="http://gdata.youtube.com/feeds/api/users/MyAccount/contacts?v=2" countHint="71"/>
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.inbox" href="http://gdata.youtube.com/feeds/api/users/MyAccount/inbox?v=2"/>
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.playlists" href="http://gdata.youtube.com/feeds/api/users/MyAccount/playlists?v=2"/>
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.subscriptions" href="http://gdata.youtube.com/feeds/api/users/MyAccount/subscriptions?v=2" countHint="54"/>
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.uploads" href="http://gdata.youtube.com/feeds/api/users/MyAccount/uploads?v=2" countHint="41"/>
<gd:feedLink rel="http://gdata.youtube.com/schemas/2007#user.newsubscriptionvideos" href="http://gdata.youtube.com/feeds/api/users/MyAccount/newsubscriptionvideos?v=2"/>
<yt:location>US</yt:location>
<yt:maxUploadDuration seconds="43200"/>
<yt:statistics lastWebAccess="2012-07-08T15:58:07.000Z" subscriberCount="126" videoWatchCount="0" viewCount="3385" totalUploadViews="50179"/>
<media:thumbnail url="http://i2.ytimg.com/i/A1RDBCYeYWY9dydB9MmPlg/1.jpg?v=934f35"/>
<yt:userId>A1RDBCYeYWY9dydB9MmPlg</yt:userId>
<yt:username display="MyAccount">MyAccount</yt:username>
</entry>
And here is what it searches through/has access to:
tag:youtube.com,2008:user:A1RDBCYeYWY9dydB9MmPlg2007-01-23T15:39:30.000Z2012-11-17T08:03:36.000Z
MyAccounthttp://gdata.youtube.com/feeds/api/users/MyAccountA1RDBCYeYWY9dydB9MmPlgUCA1RDBCYeYWY9dydB9MmPlgUSA1RDBCYeYWY9dydB9MmPlgMyAccount
Any and all help is greatly appreciated!!
Try this,
$data = file_get_contents($link);
$searchterm = ''; //as necessary
$data = strtr($data,Array("<"=>"<","&"=>"&"));
$searchterm = strtr($searchterm,Array("<"=>"<","&"=>"&"));
$search = strpos($data, $searchterm);
The middle lines makes HTML readable for PHP to process

PHP+XML changing node order

I've made a simple news script that saves articles to rss which then gets used on the Character Generator Newsticker on TV, the problem is that the CG plays the nodes starting from the top of the rss file.
now the xml looks like this:
<?xml version="1.0" ?>
<rss version="2.0">
<channel>
<title>News</title>
<link>website.com</link>
<description>News</description>
<language>ar-sa</language>
<item>
<title>Headline 1</title>
<description>Headline one the news this hour</description>
</item>
<item>
<title>Headline 2</title>
<description>Fire here flooding over there</description>
</item>
<item>
<title>Headline 3</title>
<description>Fire here flooding over there</description>
</item>
</channel>
</rss>
What i would like todo is have an option to move articles up and down the xml file, so instead of having "Headline 3" third in the list i would like to move it up to be first.
I know with C# you can do this using:
XElement node = ...get the element...
//Move up
if (node.PreviousNode != null) {
node.PreviousNode.AddBeforeSelf(node);
node.Remove();
}
//Move down
if (node.NextNode != null) {
node.NextNode.AddAfterSelf(node);
node.Remove();
Anyone have an idea how i can do this in PHP?
Thanks!
You can have a look at this answer XML reforming with DOM where they use the DOM-parser to rearrange the XML

Parse iTunes RSS Atom feed with PHP?

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

Categories