How to parse YouTube XML with PHP, including namespace descriptions - php

Im trying to use PHP's SimpleXMLElement to parse a YouTube channel feed and include the description. I can do this easily to get the title and video url.
Here is what I have that can get the descriptions
$channel_id = 'UCtNjkMLQQOX251hjGqimx2w';
$yt_url = 'https://www.youtube.com/feeds/videos.xml?channel_id=';
$xml_str = file_get_contents($yt_url.$channel_id);
$xml = new SimpleXMLElement($xml_str);
$xml->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
$result = $xml->xpath("//media:description");
foreach($result as $r){
print $r;
print '<br />';
}
The Raw xml from YouTube looks something like this.
<entry>
<id>yt:video:OTYFJaT-Glk</id>
<yt:videoId>OTYFJaT-Glk</yt:videoId>
<yt:channelId>UCtNjkMLQQOX251hjGqimx2w</yt:channelId>
<title>Guitar E.R. - Setting up wood shop for O Positive custom speaker cabinets.</title>
<link rel="alternate" href="https://www.youtube.com/watch?v=OTYFJaT-Glk"/>
<author>
<name>Guitar ER</name>
<uri>https://www.youtube.com/channel/UCtNjkMLQQOX251hjGqimx2w</uri>
</author>
<published>2020-10-18T16:38:51+00:00</published>
<updated>2020-10-20T01:04:59+00:00</updated>
<media:group>
<media:title>Guitar E.R. - Setting up wood shop for O Positive custom speaker cabinets.</media:title>
<media:content url="https://www.youtube.com/v/OTYFJaT-Glk?version=3" type="application/x-shockwave-flash" width="640" height="390"/>
<media:thumbnail url="https://i4.ytimg.com/vi/OTYFJaT-Glk/hqdefault.jpg" width="480" height="360"/>
<media:description>In this video Doctor John Moran of Guitar E.R. gives us a glimpse into the new wood shop where he will be building custom guitar and bass cabinets, likely under the name O Positive Cabinets.</media:description>
<media:community>
<media:starRating count="0" average="0.00" min="1" max="5"/>
<media:statistics views="22"/>
</media:community>
</media:group>
</entry>
<entry>
As you can see the title and uri and pretty easy to get, but I'm having trouble combining my code to get the Title, URI and Description.
Here is my code to get the Title and URI
$xml_str = file_get_contents($yt_url.$channel_id);
$xml = new SimpleXMLElement($xml_str);
foreach($xml->entry as $entry){
print $entry->title;
print '<br />';
print $entry->author->uri;
print '<br />';
}
After working on this for awhile I was able to come up with a solution, not sure if this is the best answer though.
<?
$channel_id = 'UCtNjkMLQQOX251hjGqimx2w';
$yt_url = 'https://www.youtube.com/feeds/videos.xml?channel_id=';
$xml_str = file_get_contents($yt_url.$channel_id);
$xml = new SimpleXMLElement($xml_str);
$xml->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
$yt_data = array();
foreach($xml->entry as $entry){
$videoid = (string)$entry->children('yt', true)->videoId;
$yt_data[] = array(
'id' => $videoid,
'title' => (string)$entry->title,
'description' => (string)$entry->children('media', true)->group->description,
'img' => (string)'https://img.youtube.com/vi/'.$videoid.'/maxresdefault.jpg',
'thumb' => (string)'https://img.youtube.com/vi/'.$videoid.'/mqdefault.jpg',
'published' => (string)$entry->published,
'updated' => (string)$entry->updated,
'channel' => (string)$entry->children('yt', true)->channelId,
'author' => (string)$entry->author->name,
'uri' => (string)$entry->author->uri,
'views' => (string)$entry->children('media', true)->group->community->statistics->attributes()['views'],
'ratings_count' => (string)$entry->children('media', true)->group->community->starRating->attributes()['count'],
'ratings_avg' => (string)$entry->children('media', true)->group->community->starRating->attributes()['average'],
);
}
foreach($yt_data as $yt){
print '<div class="ytVidWrap">';
print '<a href="https://www.youtube.com/watch?v='.$yt['id'].'" target="_blank">';
print '<img src="'.$yt['thumb'].'" alt="YouTube Video" />';
print '<h1>'.$yt['title'].'</h1>';
print '<p>'.$yt['description'].'</p>';
print '<p>'.$yt['views'].' views • '.date('M j, Y',strtotime($yt['published'])).'</p>';
print '</a>';
print '</div>';
}
?>

Related

How do I put media:thumbnail in RSS feed

I can't get the thumbnail images to display in the RSS feed items. Where do I put the code for the image to so up?
first tried the getElementsByTagName but of course that doesn't work, then I tried your solution- $item_img = $item->getElementsByTagNameNS('the namespace URI you found','thumbnail')
->item(0)->getAttribute('url');
But that brought back an error that broke the whole page. I think I understand that thumbnail is a sub element of media: and I see that it is part of jwplayer:feedid under media:group. So, does it go in the array like the other items? I tried it inside the array and outside as well. Where do I put it to display the thumbnail in my newsfeed item?
<?php
$rss = new DOMDocument();
$rss->load('https://cdn.jwplayer.com/v2/playlists/IYxiCISJ?format=mrss');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
Here is the xml file output I am trying to parse:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:jwplayer="http://rss.jwpcdn.com/">
<channel>
<title>Car Repairs</title>
<description>Car Repairs</description>
<jwplayer:kind>MANUAL</jwplayer:kind>
<jwplayer:feedid>IYxiCISJ</jwplayer:feedid>
<jwplayer:feed_instance_id>7beba58b-b2a2-4000-af11-1e43a7cb8680</jwplayer:feed_instance_id>
<jwplayer:link rel="first" href="https://cdn.jwplayer.com/v2/playlists/IYxiCISJ?format=mrss&page_offset=1&page_limit=500"/>
<jwplayer:link rel="last" href="https://cdn.jwplayer.com/v2/playlists/IYxiCISJ?format=mrss&page_offset=1&page_limit=500"/>
<item>
<title>Preparing the Audi A8 for Service- The American Garage</title>
<link>https://cdn.jwplayer.com/previews/ybcuKyZl</link>
<description>Putting Audi A8 in service mode.</description>
<pubDate>Mon, 17 Sep 2018 14:35:11 -0000</pubDate>
<guid isPermaLink="false">ybcuKyZl</guid>
<enclosure url="https://cdn.jwplayer.com/videos/ybcuKyZl-qQFQ3TOZ.mp4" type="video/mp4" length="515"/>
<jwplayer:feedid>IYxiCISJ</jwplayer:feedid>
<media:group>
<media:content url="https://cdn.jwplayer.com/manifests/ybcuKyZl.m3u8" medium="video" type="application/vnd.apple.mpegurl" duration="515"/>
<media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-jTncGIBU.mp4" medium="video" type="video/mp4" duration="515" width="320" height="180" fileSize="19714361"/>
<media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-i4o7KXqD.mp4" medium="video" type="video/mp4" duration="515" width="480" height="270" fileSize="29378682"/>
<media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-XMc5nvLA.mp4" medium="video" type="video/mp4" duration="515" width="720" height="406" fileSize="40249178"/>
<media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-qQFQ3TOZ.mp4" medium="video" type="video/mp4" duration="515" width="1280" height="720" fileSize="109187664"/>
<media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-MTvbpSOY.m4a" medium="video" type="audio/mp4" duration="515" fileSize="7304077"/>
<media:thumbnail url="https://cdn.jwplayer.com/thumbs/ybcuKyZl-720.jpg" width="720" />
<media:keywords>The American Garage,thermostat,timing belt audi,audi a8,audi 4.2,water pump,service mode,A8,audi,car repairs</media:keywords>
</media:group>
<jwplayer:track file="https://cdn.jwplayer.com/strips/ybcuKyZl-120.vtt" kind="thumbnails"/>
</item>
</channel>
</rss>
All that's needed is to pass the correct namespace URI to DOMDocument::getElementsByTagNameNS(). (Rather than hard-coding it you can get the attribute from the XML document if you think it will change.)
In addition, you can't escape data for HTML just by replacing & with & there are built-in functions for that purpose, use them for all your data.
<?php
$rss = new DOMDocument();
$rss->load('https://cdn.jwplayer.com/v2/playlists/IYxiCISJ?format=mrss');
// get the namespace URI
$mediaURL = $rss->documentElement->getAttribute("xmlns:media");
foreach ($rss->getElementsByTagName('item') as $node) {
$feed[] = [
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
// get the namespaced element value
'thumbnail' => $node->getElementsByTagNameNS($mediaURL, 'thumbnail')->item(0)->getAttribute('url'),
];
}
foreach ($feed as $item) {
$title = htmlspecialchars($item["title"]);
$link = htmlspecialchars($item["link"]);
$description = htmlspecialchars($item["desc"]);
$date = htmlspecialchars(date('l F d, Y', strtotime($item['date'])));
$thumbnail = htmlspecialchars($item["thumbnail"]);
echo <<< HTML
<p>
<a href="$link" title="$title">
<img src="$thumbnail" alt="$title"/>
<br/>
<strong>$title</strong>
</a>
<small><em>Posted on $date</em></small>
</p>
<p>
$description
</p>
HTML;
}

How to display XML RSS feed by PHP

I have an assignment to display my XML RSS via PHP on a website, so far I have tried multiple things and all have failed. And I was unable to find the answer since most people do RSS feed by PHP from MySQL database to get a live feed of posts.
XML RSS
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Treehouse front page</title>
<link>https://teamtreehouse.com/</link>
<description>Programming Tutorials</description>
<item>
<title>Code Academy</title>
<link>https://teamtreehouse.com/</link>
<description>Programming Tutorials</description>
</item>
</channel>
</rss>
How can I display this file via PHP?
Try to use the DOMDocument() class, for example:
$rss = new DOMDocument();
$rss->load("http://yoursite.com/rss/");
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
This is a tip. I hope I have helped you.
Thank you very much for leading me onto the right track!
But that code would work for DOM file while mine was simplexml.
I used the following code to solve the problem
<?php
$rss = simplexml_load_file('rss.xml');
echo '<h4>'. $rss->channel->title . '</h4>';
foreach ($rss->channel->item as $item) {
echo '<h4>' . $item->title . "</h4>";
echo "<p>" . $item->title . "</p>";
echo "<p>" . $item->description . "</p>";
}
?>

How to parse <media:content> tag in RSS with simplexml

Structure of my RSS from http://rss.cnn.com/rss/edition.rss is:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?>
<?xml-stylesheet type="text/css" media="screen" href="http://rss.cnn.com/~d/styles/itemcontent.css"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title><![CDATA[CNN.com - RSS Channel - Intl Homepage - News]]></title>
<description><![CDATA[CNN.com delivers up-to-the-minute news and information on the latest top stories, weather, entertainment, politics and more.]]></description>
<link>http://www.cnn.com/intl_index.html</link>
...
<item>
<title><![CDATA[Russia responds to claims it has damaging material on Trump]]></title>
<description><![CDATA[The Kremlin denied it has compromising information about US President-elect Donald Trump, describing the allegations as "pulp fiction".]]></description>
<link>http://www.cnn.com/2017/01/11/politics/russia-rejects-trump-allegations/index.html</link>
<guid isPermaLink="true">http://www.cnn.com/2017/01/11/politics/russia-rejects-trump-allegations/index.html</guid>
<pubDate>Wed, 11 Jan 2017 14:44:49 GMT</pubDate>
<media:group>
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-super-169.jpg" height="619" width="1100" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-large-11.jpg" height="300" width="300" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-vertical-large-gallery.jpg" height="552" width="414" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-video-synd-2.jpg" height="480" width="640" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-live-video.jpg" height="324" width="576" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-t1-main.jpg" height="250" width="250" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-vertical-gallery.jpg" height="360" width="270" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-story-body.jpg" height="169" width="300" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-t1-main.jpg" height="250" width="250" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-assign.jpg" height="186" width="248" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-hp-video.jpg" height="144" width="256" />
</media:group>
</item>
...
</channel>
</rss>
If you parse this XML with simplexml like this:
$rss = simplexml_load_file($url, null, LIBXML_NOCDATA);
$rssjson = json_encode($rss);
$rssarray = json_decode($rssjson, TRUE);
you will see that <media:content> is simply missing in $rssarray items. So I found a tutorial with "namespace" solution. However, in the example author is using:
foreach ($xml->channel->item as $item) { ... }
but I am using (cannot use foreach for some reasons):
$rssjson = json_encode($rss);
$rssarray = json_decode($rssjson, TRUE);
So I modified the solution for my case like this:
$rss = simplexml_load_file($url, null, LIBXML_NOCDATA);
$namespaces = $rss->getNamespaces(true); // get namespaces
$rssjson = json_encode($rss);
$rssarray = json_decode($rssjson, TRUE);
if (isset($rssarray['channel']['item'])) {
foreach ($rssarray['channel']['item'] as $key => $item) {
$media_content = $rss->channel->item[$key]->children($namespaces['media']);
foreach($media_content as $tag) {
$tagjson = json_encode($tag);
$tagarray = json_decode($tagjson, TRUE);
}
}
}
But it does not work. For every item I get in $tagarray as a result an array with this structure:
Array(
'content' => array(
'0' => array(null),
'1' => array(null),
...
'11' => array(null),
)
)
It is an array with as many items as is the count of <media:content> tags, but every item is empty. I need to get an url attribute of every item. What am I doing wrong and getting an empty array?
Tags are actually empty:
<media:content ... />
^^
Information is contained in attributes, which can be fetched with SimpleXMLElement::attributes(), e.g.:
$rss = simplexml_load_file($url, null, LIBXML_NOCDATA);
$namespaces = $rss->getNamespaces(true);
$media_content = $rss->channel->item[0]->children($namespaces['media']);
foreach($media_content->group->content as $i){
var_dump((string)$i->attributes()->url);
}
I suspect the problem comes from the JSON trick. SimpleXML generates all its classes and properties dynamically (they aren't regular PHP classes), what means that you can't fully rely on standard PHP features like print_r() or json_encode(). This gets illustrated if you insert this in the above loop:
var_dump($i, json_encode($i), (string)$i->attributes()->url);
object(SimpleXMLElement)#2 (0) {
}
string(2) "{}"
string(91) "http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-super-169.jpg"
...
I had requirement to aggregate RSS news feeds from different source which had images tags in different formats so I used below code:
//Sample Feed 1: https://www.hindustantimes.com/rss/topnews/rssfeed.xml
//Sample Feed 2: https://economictimes.indiatimes.com/rssfeedsdefault.cms
$feed=$_GET['feed'];
$rss = simplexml_load_file($feed);
$namespaces = $rss->getNamespaces(true);
echo '<strong>'. $rss->channel->title . '</strong><br><br>';
foreach ($rss->channel->item as $item) {
$media_content = $item->children($namespaces['media']);
foreach($media_content as $i){
$imageAlt = (string)$i->attributes()->url;
}
echo "Link: " . $item->link ."<br>";
echo "Title: " . $item->title ."<br>";
echo "Description: " . $item->description ."<br>";
echo "PubDate: " . $item->pubDate ."<br>";
echo "Image: " . $item->image ."<br>";
echo "ImageAlt: " . $imageAlt ."<br>";
echo "<br><br>";
}

How to build a correct SOAP request with PHP

I need to format/build a request for this SOAP "service":
http://api.notificationmessaging.com/NMSOAP/NotificationService?wsdl
Ideally I would like to use the native PHP SOAP class, but I'm beginning to wonder if this class is not the cause of my problems.
The manual provides this example:
<soapenv:Body>
<api:sendObject>
<arg0>
<content>
<entry>
<key>1</key>
<value>
<![CDATA[
<table width="600">
<tr>
<td>
<font size="2" face="Arial">Our powerful algorithms already
found a matching profile that matches your criteria:
<br>Celina72 </font>
<img src="http://mypath/to/my/image.gif" width="50"
height="50" border="0" />
</td>]]></value>
</entry>
</content>
<dyn>
<entry>
<key>FIRSTNAME</key>
<value>john</value>
</entry>
</dyn>
<email>johnblum#flowerpower.com</email>
<encrypt>BdX7CqkmjSivyBgIcZoN4sPVLkx7FaXGiwsO</encrypt>
<notificationId>6464</notificationId>
<random>985A8B992601985A</random>
<senddate>2008-12-12T00:00:00</senddate>
<synchrotype>NOTHING</synchrotype>
<uidkey>EMAIL</uidkey>
</arg0>
</api:sendObject>
</soapenv:Body>
</soapenv:Envelope>
Here is the garbage that my PHP request produces (from __getLastRequest())
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.service.nsapi.emailvision.com/" xmlns:ns2="http://xml.apache.org/xml-soap">
<SOAP-ENV:Body>
<ns1:sendObject/>
<param1>AAAAAAAAAAAAAAAAAAAAAAAAAAA</param1>
<param2>123456789</param2>
<param3>BBBBBBBBBBBB</param3>
<param4>2013-09-09T00:00:00</param4>
<param5>NOTHING</param5>
<param6>EMAIL</param6>
<param7>
<ns2:Map>
<item>
<key>2</key>
<value>TEST</value>
</item>
</ns2:Map>
</param7>
<param8>
<ns2:Map>
<item>
<key>FIRSTNAME</key>
<value>John</value>
</item>
</ns2:Map>
<ns2:Map>
<item>
<key>LASTNAME</key>
<value>Smith</value>
</item>
</ns2:Map>
</param8>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
My call:
$client = new SoapClient('http://api.notificationmessaging.com/NMSOAP/NotificationService?wsdl', array( 'trace' => 1, 'exceptions' => 0 ) );
The params look like this (modified with dummy data):
$email = 'john.smith#example.com';
$encrypt = 'AAAAAAAAAAAAAAAAAAAAAAAAAAA';
$notification_id = 123456789;
$random = 'BBBBBBBBBBBB';
$senddate = '2013-09-09T00:00:00';
$synchrotype = 'NOTHING';
$uidkey = 'EMAIL';
$content = array();
$content[] = array(
2 => 'TEST'
);
$dyn = array();
$dyn[] = array(
'FIRSTNAME' => 'John'
);
$dyn[] = array(
'LASTNAME' => 'Smith'
);
$params = array(
'email' => $email,
'encrypt' => $encrypt,
'notificationId' => $notification_id,
'random' => $random,
'senddate' => $senddate,
'synchrotype' => $synchrotype,
'uidkey' => $uidkey,
'content' => $content,
'dyn' => $dyn
);
I then execute the request like this :
$res = $client->__soapCall( 'sendObject', array( $email, $encrypt, $notification_id, $random, $senddate, $synchrotype, $uidkey, $content, $dyn ) );
Why is PHP unable to format my request correctly? Is there a mor direct approach where I could write the XML "by hand" and then post it using cURL?
"Is there a more direct approach where I could write the XML?"
By using a SoapVar and setting the encode parameter of the constructor to XSD_ANYXML you can write the raw XML.
There should be a way where the WSDL helps build the XML though.
You could try something like this:
$wsdl = "http://api.notificationmessaging.com/NMSOAP/NotificationService?wsdl";
$client = new SoapClient($wsdl, array( 'soap_version' => SOAP_1_1,
'trace' => true,
));
try {
$xml = '<arg0>
<content>
<entry>
<key>1</key>
<value>
<![CDATA[
<table width="600">
<tr>
<td>
<font size="2" face="Arial">Our powerful algorithms already
found a matching profile that matches your criteria:
<br>Celina72 </font>
<img src="http://mypath/to/my/image.gif" width="50"
height="50" border="0" />
</td>]]></value>
</entry>
</content>
<dyn>
<entry>
<key>FIRSTNAME</key>
<value>john</value>
</entry>
</dyn>
<email>johnblum#flowerpower.com</email>
<encrypt>BdX7CqkmjSivyBgIcZoN4sPVLkx7FaXGiwsO</encrypt>
<notificationId>6464</notificationId>
<random>985A8B992601985A</random>
<senddate>2008-12-12T00:00:00</senddate>
<synchrotype>NOTHING</synchrotype>
<uidkey>EMAIL</uidkey>
</arg0>';
$args = array(new SoapVar($xml, XSD_ANYXML));
$res = $client->__soapCall('sendObject', $args);
return $res;
} catch (SoapFault $e) {
echo "Error: {$e}";
}
echo "<hr>Last Request";
echo "<pre>", htmlspecialchars($client->__getLastRequest()), "</pre>";
i know that the topic is about 1 year old,
but i find some good informations in it, that give me a very good help , and i finaly succed to make it work with the php method sendObject()
so i hope my contribution will help others people too...
at first the call of sendObject() this way : $client->__soapCall seems not working at all
you have to call it directly : $client->sendObject
in this topic, i think that it is a using of the API of emailvision (smartfocus, now...)
this method sendObject does not need a generated token by openApiConnection()
oki, now , it is the code to make it work
<?php
$email = 'johann.******#gmail.com';
$encrypt = '******************************';
$notification_id = '**************';
$random = '********************';
$senddate = '2013-09-09T00:00:00';
$synchrotype = 'NOTHING';
$uidkey = 'EMAIL';
$params = array(
'arg0' => array(
'content' => array( 1 => 'mon_test'),
'dyn' => array( 'FIRSTNAME' => 'yoyo'),
'email' => $email,
'encrypt' => $encrypt,
'notificationId' => $notification_id,
'random' => $random,
'senddate' => $senddate,
'synchrotype' => $synchrotype,
'uidkey' => $uidkey
)
);
$client = new SoapClient('http://api.notificationmessaging.com/NMSOAP/NotificationService?wsdl', array( 'trace' => 1, 'exceptions' => 0 ) );
$res = $client->sendObject( $params );
echo "<br /><br /><br />";
echo "REQUEST 1 :" . htmlspecialchars($client->__getLastRequest()) . "<br />";
echo "RESPONSE 1 :" . htmlspecialchars($client->__getLastResponse()) . "<br /><br /><br />";
?>
you have to know that $encrypt ,$notification_id , $random are generated by creating a transactionnal message, you can get this informations in the interface of campagn commander
take care of the schema of input xml, there is a node arg0, then you have to make a level arg0 in your array parameters
to make it work directly with xml :
<?php
$wsdl = "http://api.notificationmessaging.com/NMSOAP/NotificationService?wsdl";
$client = new SoapClient($wsdl, array( 'soap_version' => SOAP_1_1, 'trace' => true, ));
try {
$xml = '
<ns1:sendObject>
<arg0>
<content>
<entry>
<key>1</key>
<value>
<![CDATA[
<table width="600">
<tr>
<td>
<font size="2" face="Arial">Our powerful algorithms already found a matching profile that matches your criteria:
<br>Celina72 </font>
<img src="http://mypath/to/my/image.gif" width="50" height="50" border="0" />
</td>]]>
</value>
</entry>
</content>
<dyn>
<entry>
<key>FIRSTNAME</key>
<value>john</value>
</entry>
</dyn>
<email>johann*******#gmail.com</email>
<encrypt>*********************</encrypt>
<notificationId>**************</notificationId>
<random>**********************</random>
<senddate>2008-12-12T00:00:00</senddate>
<synchrotype>NOTHING</synchrotype>
<uidkey>EMAIL</uidkey>
</arg0>
</ns1:sendObject>
';
$args = array(new SoapVar($xml, XSD_ANYXML));
$res = $client->__soapCall('sendObject', $args);
//return $res;
}
catch (SoapFault $e) {
echo "Error: {$e}";
}
echo "<hr>Last Request";
echo "<pre>", htmlspecialchars($client->__getLastRequest()), "</pre>";
echo "<hr>Last Response";
echo "<pre>", htmlspecialchars($client->__getLastResponse()), "</pre>";
?>
it s important to write the first node like this : <ns1:sendObject>
<api:sendObject> does not work

how to access this child element - attribute in php simplexml

I want to access the 'url' attribute in the media:content element below. I am pretty sure this gives me the media:content, but I can not seem to get the url (see what I tried below):
$theContent = $item->children('media', true)->content;
xml:
<item>
<media:content type="image/jpeg" url="my url" />
</item>
I have tried variations:
$theURL = $item->children('media', true)->content['url'];
and
$mediaItem=$item->children('media', true)->content;
$contentItem=$mediaItem->children('content', true);
$url = $contentItem['url'];
No luck. ??
Complete working code with output:
<?php
$xml = '<item>
<media:content type="image/jpeg" url="my url" />
</item>';
$theContent = #new SimpleXMLElement($xml);
$attributes = $theContent->content->attributes();
echo $attributes['url']; //outputs: my url
?>
References: SimpleXML Attributes

Categories