PHP GMAIL Contacts XML Parsing with DOMDocument and cURL - php

What I am trying to get currently is just the attribute of gd:email that is the "address=" only nothing else as of the moment. Which I can get to the xml portion, heck I can even get any given think per say as long as its within tags like but to get the attribute of any given one like in my case.. I am completely confused on. I used to know how to do it but its been so long since I did anything that wasn't simple for XML useage. so I done messed my own self up..
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'>
<id>xxxxxxxxxxxxxx#gmail.com</id>
<updated>2011-06-30T00:07:48.706Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'>Taco Bells's Contacts</title>
<link rel='alternate' type='text/html' href='http://www.google.com/'/>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full'/>
<link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full'/>
<link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/batch'/>
<link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full?max-results=5'/>
<link rel='next' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full?start-index=6&max-results=5'/>
<author>
<name>Taco Bell</name>
<email>xxxxxxxxxxxxxx#gmail.com</email>
</author>
<generator version='1.0' uri='http://www.google.com/mate/feeds'>Contacts</generator>
<openSearch:totalResults>90</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>5</openSearch:itemsPerPage>
<entry>
<id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/0</id>
<updated>2010-01-27T00:11:57.430Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'>Taco B</title>
<link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/0/8_XL_JoAPon1k7SNixI2iA'/>
<link rel='http://schemas.google.com/contacts/2008/rel#photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/0'/>
<link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/0'/>
<link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/0/1264551117430000'/>
<gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx#gmail.com' primary='true'/>
</entry>
<entry>
<id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/1</id>
<updated>2007-08-01T18:02:04.410Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'></title>
<link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/1/1B2M2Y8AsgTpgAmY7PhCfg'/>
<link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/1'/>
<link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/1/1185991324410001'/>
<gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx#sbcglobal.net' primary='true'/>
</entry>
<entry>
<id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/2</id>
<updated>2010-01-27T00:11:57.430Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'>Steve Sattler</title>
<link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/2/1B2M2Y8AsgTpgAmY7PhCfg'/>
<link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/2'/>
<link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/2/1264551117430000'/>
<gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx#yahoo.com' primary='true'/>
</entry>
<entry>
<id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/3</id>
<updated>2010-01-27T00:11:57.430Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'>Michael Montana</title>
<link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/3/1B2M2Y8AsgTpgAmY7PhCfg'/>
<link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/3'/>
<link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/3/1264551117430000'/>
<gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx#comcast.net' primary='true'/>
</entry>
<entry>
<id>http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/base/4</id>
<updated>2007-08-01T18:02:04.410Z</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'></title>
<link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/mate/feeds/photos/media/xxxxxxxxxxxxxx%40gmail.com/4/1B2M2Y8AsgTpgAmY7PhCfg'/>
<link rel='self' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/4'/>
<link rel='edit' type='application/atom+xml' href='http://www.google.com/mate/feeds/contacts/xxxxxxxxxxxxxx%40gmail.com/full/4/1185991324410001'/>
<gd:email rel='http://schemas.google.com/g/2005#other' address='xxxxxxxxxxxxxx#courant.com' primary='true'/>
</entry>
</feed>
with code that looks like (this is the whole script (currently))
$user = "xxxxxxxxxxxxxxxxxxx#gmail.com";
$password = "xxxxxxxxxxxx";
// ref: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html
// step 1: login
$login_url = "https://www.google.com/accounts/ClientLogin";
$fields = array(
'Email' => $user,
'Passwd' => $password,
'service' => 'cp', // <== contact list service code
'source' => 'test-google-contact-grabber',
'accountType' => 'GOOGLE',
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$login_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$fields);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
$returns = array();
foreach (explode("\n",$result) as $line)
{
$line = trim($line);
if (!$line) continue;
list($k,$v) = explode("=",$line,2);
$returns[$k] = $v;
}
curl_close($curl);
// step 2: grab the contact list
$feed_url = "http://www.google.com/m8/feeds/contacts/$user/full?&max-results=5";
$header = array(
'Authorization: GoogleLogin auth=' . $returns['Auth'],
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $feed_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
$doc = new DOMDocument();
$doc->load($result);
$arrFeeds = array();
foreach ($doc->getElementsByTagName('entry') as $node) {
$monkey = $node->getAttribute('{gd:email}');
$itemRSS = array($monkey->nodeValue);
array_push($arrFeeds, $itemRSS);
}
array_unique($arrFeeds);

Have a look at DOMDocument and DOMXPath. In order to get nodes that are part of a particular namespace, like the email node which is part of the gd namespace, you need to register the namespace with the DOMXPath object using DOMXPath::registerNamespace(). The namespace URI can usually be found at the top of the XML document.
Example:
$doc = new DOMDocument;
$doc->recover = true;
$doc->loadXML($result);
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('gd', 'http://schemas.google.com/g/2005');
$emails = $xpath->query('//gd:email');
foreach ( $emails as $email )
{
echo $email->getAttribute('address');
// To get the title.
// This could also be done using XPath.
// You can also use ->nodeValue instead of ->textContent.
echo $email->parentNode->getElementsByTagName('title')->item(0)->textContent;
}
In the example above, $result is the result of $result = curl_exec($ch);.

$url = 'https://www.google.com/m8/feeds/contacts//full?max-results='.$max_results.'&oauth_token='.$accesstoken.'&alt=json&updated-min=2007-03-16T00:00:00';
function curl_file_get_contents($url)
{
$curl = curl_init();
$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
//The URL to fetch. This can also be set when initializing a session with curl_init().
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_HTTPHEADER,array('GData-Version: 2.0'));
curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5); //The number of seconds to wait while trying to connect.
curl_setopt($curl,CURLOPT_HTTPGET,true);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //The contents of the "User-Agent: " header to be used in a HTTP request.
//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); //To follow any "Location: " header that the server sends as part of the HTTP header.
//curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect.
curl_setopt($curl, CURLOPT_TIMEOUT, 10); //The maximum number of seconds to allow cURL functions to execute.
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //To stop cURL from verifying the peer's certificate.
$contents = curl_exec($curl);
curl_close($curl);
return $contents;
}

Related

How to search XML in PHP got by restful URL

I have got XML response from curl and trying to search some tag value but I am failing. Kindly somebody help me
Please bare as I am new to the PHP
<?php
$DynamicValue = 'KWI';
$url='https://www.example.com/_api/web/lists/getbytitle(\'Stations\')/items?$select=OfficeId&$filter='.urlencode("CityCode eq '" . $DynamicValue . "'");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/xml"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$XML = new SimpleXMLElement($result);
foreach($XML->entry as $value)
{
echo $value->OfficeId
}
Output:
<?xml version="1.0" encoding="utf-8"?><feed xml:base="https://www.example.com/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"><id>a9f073a2-387e-4ac5-81b9-16b3ae81dba3</id><title /><updated>2017-03-20T08:15:47Z</updated><entry m:etag=""61""><id>Web/Lists(guid'5344b09b-d3f7-44ec-bd52-a8f5a99f23f9')/Items(23)</id><category term="SP.Data.StationsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" href="Web/Lists(guid'5344b09b-d3f7-44ec-bd52-a8f5a99f23f9')/Items(23)" /><title /><updated>2017-03-20T08:15:47Z</updated><author><name /></author><content type="application/xml"><m:properties><d:OfficeId>KWIKU08AA</d:OfficeId></m:properties></content></entry></feed>
Hope this will work.
$string='<?xml version="1.0" encoding="utf-8"?><feed xml:base="https://www.example.com/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"><id>a9f073a2-387e-4ac5-81b9-16b3ae81dba3</id><title /><updated>2017-03-20T08:15:47Z</updated><entry m:etag=""61""><id>Web/Lists(guid\'5344b09b-d3f7-44ec-bd52-a8f5a99f23f9\')/Items(23)</id><category term="SP.Data.StationsListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" href="Web/Lists(guid\'5344b09b-d3f7-44ec-bd52-a8f5a99f23f9\')/Items(23)" /><title /><updated>2017-03-20T08:15:47Z</updated><author><name /></author><content type="application/xml"><m:properties><d:OfficeId>KWIKU08AA</d:OfficeId></m:properties></content></entry></feed>';
echo explode("</d:OfficeId>",explode("<d:OfficeId>", $string)[1])[0];
PHP Code demo

get particular attr from a html string in php

i have a punch of links in a string like following
<link type="image/png" href="http://infinitewp.com/wp-content/themes/mystile-child/images/favicon.png" />
<link rel="shortcut icon" type="image/png" href="http://infinitewp.com/wp-content/themes/mystile-child/images/favicon.png" />
<link type="image/png" href="http://infinitewp.com/wp-content/themes/mystile-child/images/favicon.png" />
i need to get href for which link has the rel="shortcut icon"
i can find shortcut icon link using
strpos($content,"shortcut icon")
this code but i need to get the href of this link? how to do this in easy way?
Try this :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "Your URL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec ($ch);
curl_close($ch);
$xml = new DOMDocument();
$xml->strictErrorChecking = FALSE;
$xml->loadHTML($html);
$xpath = new DOMXPath($xml);
$arr = $xpath->query('//link[#rel="shortcut icon"]');
echo $arr[0]['href']; // This is the content of your href.

generating an RSS Feed from remote xml data

I am trying to make an RSS feed which feeds live data from a remote XML file provided to me for promoting a webcam network.
I want to run a feed within my official website. My problem is, I want to output the data into an RSS feed. I am not too familiar with RSS and php so there is a lot of guess work going on.
Here is the code I have so far which will not work, it incorporates php to retrieve the xml data. Also I am unsure as to what filename/type I save this as? XML or php? Both don't seem to work.
<?php
$curlData = '<?xml version="1.0" encoding="UTF-8"?>
<SMLQuery>
<Options MaxResults="20"/>
<AvailablePerformers Exact="false" PageNum="1" CountTotalResults="true">
<Include>
<Country/>
<Headline/>
<Descriptions></Descriptions>
<Rating/>
<Age/>
<Gender/>
<Media>biopic</Media>
<FreeChatSort/>
<PerformanceSort/>
</Include>
<Constraints> <StreamType>live,recorded,offline</StreamType>
<PublicProfile/>
<Name></Name><NoKeywords/>
<RelativeURLs/>
<NoKeywords/>
</Constraints>
</AvailablePerformers>
</SMLQuery>';
$url='http://affiliate.streamate.com/?????/SMLResult.xml';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_TIMEOUT,120);
curl_setopt($curl,CURLOPT_HTTPHEADER,array (
'Content-Type: application/xml; charset=utf-8',
));
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $curlData);
$result = curl_exec($curl);
curl_close ($curl);
$xml = simplexml_load_string ($result);
// $json = json_encode($xml);
// $array = json_decode($json,TRUE);
?>
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Website Feed</title>
<description>Website Feed coded manually</description>
<link>yourdomain.com</link>;
<?php
foreach ($xml->AvailablePerformers->Performer as $perf) {
<item>
<title><php? echo ".$perf->attributes()->Name ."?></title>
<description><php? echo " .$perf->Descriptions->About ."?></description>
<link><php? echo "<a href='http://www.example.com /cam/".$perf->attributes()->Name." /?AFNO=X-0-XXXXXX-XXXXXX&UHNSMTY=XXX?></link>
</item>
</channel>
</rss>
?>
As you already have an XML source and want another XML result, maybe you could try XSLT to directly convert it.
http://www.w3schools.com/xsl/

Making a dynamic RSS feed from external XML file

I am trying to generate an RSS feed for a streamate whitelabel site. The network support have provided me with the bare minimum information on how to generate anything.
This is all they have provided to generate info about live performers:
"This query is used to retrieve information about performers.
You will receive the information packaged into an XML document.
Please send your XML request by HTTP POST to:
http://affliate.streamate.com/??????/SMLResult.xml
Be sure to send a “Content-type: text/xml” header as part of the HTTP request. You
may replace streamate.com with a branded domain, if one is available to you, to have
URLs returned with that same branded domain instead of streamate.com"
So, I have managed to find out how to pull live info from their XML feed into a php page, by searching for a snippet of code, but this is not an RSS feed that I can utilise (to feed into social networks, etc).
If anyone can please give me some pointers on how to utilise their XML file into an RSS feed which links to my own whitelabe domain (as they have half instructed above).
Below is the code I have found and used to generate the live info on a php page (I just wish it was an RSS feed).
<?php
$curlData = '<?xml version="1.0" encoding="UTF-8"?>
<SMLQuery>
<Options MaxResults="20"/>
<AvailablePerformers Exact="false" PageNum="1" CountTotalResults="true">
<Include>
<Country/>
<Headline/>
<Descriptions></Descriptions>
<Rating/>
<Age/>
<Gender/>
<Media>biopic</Media>
<FreeChatSort/>
<PerformanceSort/>
</Include>
<Constraints> <StreamType>live,recorded,offline</StreamType>
<PublicProfile/>
<Name></Name><NoKeywords/>
<RelativeURLs/>
<NoKeywords/>
</Constraints>
</AvailablePerformers>
</SMLQuery>';
$url='http://affiliate.streamate.com/????/SMLResult.xml';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_TIMEOUT,120);
curl_setopt($curl,CURLOPT_HTTPHEADER,array ('Content-Type: application/xml; charset=utf-8',));
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $curlData);
$result = curl_exec($curl);
curl_close ($curl);
$xml = simplexml_load_string ($result);
// $json = json_encode($xml);
// $array = json_decode($json,TRUE);
print_r ($result);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Cam Testing Going On :)</title>
<link rel="stylesheet" type="text/css" href="streamstyle.css" />
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body id="home">
<?php
foreach ($xml->AvailablePerformers->Performer as $perf) {
echo "<br />";
echo "<p>";
echo "<strong>Name: </strong>" .$perf->attributes()->Name ."<br />";
echo "<a href='http://www.mywhitelabeldomain.com/cam/".$perf->attributes()->Name."/?AFNO=X-0-XXXXXX-XXXXXX&UHNSMTY=XXX' target='_blank'><img src='http://mywhitelabeldomain.com".$perf->Media->Pic->Thumb->attributes()->Src."' /></a><br />";
echo "<strong>Age: </strong>" .$perf->attributes()->Age ."<br />";
echo "<strong>About: </strong>" .$perf->attributes()->Headline ."<br />";
echo "<strong>Description: </strong>" .$perf->Descriptions->About ."<br />";
echo "<strong>StreamType: </strong>" .$perf->attributes()->StreamType ."<br />";
echo "<p>";
echo "<br />";
}
?>
</body>
</html>
and here is one of the example snippets that the affiliate network provided, which I can see is part of the above code I found:
XML Request Structure
Example, Real World Generic
<?xml version="1.0" encoding="UTF-8"?>
<SMLQuery>
<Options MaxResults="50" />
<AvailablePerformers QueryId=”MyGenericQuery”>
<Include>
<Descriptions />
<Media>staticbiopic</Media>
</Include>
<Constraints>
<PublicProfile />
<StreamType>live</StreamType>
</Constraints>
</AvailablePerformers>
</SMLQuery>
Thanks for any advice anyone can give me. I really want to learn how to do this.
I am aware that an RSS feed xml file looks like this:
<?xml version="1.0" encoding="utf-8"?> <rss version="2.0"> <channel> <title>Website Feed</title> <description>Website Feed coded manually</description> <link>yourdomain.com</link>; <item> <title>A Special Event</title> <description>A Special Teleconference for our customers about our products</description> <link>yourdomain.com/events.htm</link>; </item> </channel> </rss>
But I am wanting to combine the data from the external xml file (from my first code snippet at the begining of this post) into an rss feed xml file, but retaining the use of my whitelabel url in the links.
UPDATE
Ok, I have tried merging some of the php code into an xml doc using an rss feed code structure, but obviously I am failing as I do not quite understand what I am doing here. I have searched google endlessly on this. Here is my attempt at merging the codes:
<?php
$curlData = '<?xml version="1.0" encoding="UTF-8"?>
<SMLQuery>
<Options MaxResults="20"/>
<AvailablePerformers Exact="false" PageNum="1" CountTotalResults="true">
<Include>
<Country/>
<Headline/>
<Descriptions></Descriptions>
<Rating/>
<Age/>
<Gender/>
<Media>biopic</Media>
<FreeChatSort/>
<PerformanceSort/>
</Include>
<Constraints> <StreamType>live,recorded,offline</StreamType>
<PublicProfile/>
<Name></Name><NoKeywords/>
<RelativeURLs/>
<NoKeywords/>
</Constraints>
</AvailablePerformers>
</SMLQuery>';
$url='http://affiliate.streamate.com/?????/SMLResult.xml';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_TIMEOUT,120);
curl_setopt($curl,CURLOPT_HTTPHEADER,array (
'Content-Type: application/xml; charset=utf-8',
));
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $curlData);
$result = curl_exec($curl);
curl_close ($curl);
$xml = simplexml_load_string ($result);
// $json = json_encode($xml);
// $array = json_decode($json,TRUE);
print_r ($result);
?>
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Website Feed</title>
<description>Website Feed coded manually</description>
<link>yourdomain.com</link>;
<?php
foreach ($xml->AvailablePerformers->Performer as $perf) {
<item>
<title><php? echo ".$perf->attributes()->Name ."?></title>
<description><php? echo " .$perf->Descriptions->About ."?></description>
<link><php? echo "<a href='http://www.mydomain.com/cam/".$perf->attributes()->Name." /?AFNO=X-0-XXXXXX-XXXXXX&UHNSMTY=XXX?></link>;
</item>
</channel>
</rss>
?>
UPDATE 2
I have removed the print_r() as instructed and saved the following code as a php file, uploaded it, but all I am seeing is a blank white page. I dont know what to change re the header tag so can you explain where the header code should be and what I need to add/change please?
This is the code I now have, saved as a php file (or should it be an xml file? as I want it as an RSS feed, not a php webpage).
<?php
$curlData = '<?xml version="1.0" encoding="UTF-8"?>
<SMLQuery>
<Options MaxResults="20"/>
<AvailablePerformers Exact="false" PageNum="1" CountTotalResults="true">
<Include>
<Country/>
<Headline/>
<Descriptions></Descriptions>
<Rating/>
<Age/>
<Gender/>
<Media>biopic</Media>
<FreeChatSort/>
<PerformanceSort/>
</Include>
<Constraints> <StreamType>live,recorded,offline</StreamType>
<PublicProfile/>
<Name></Name><NoKeywords/>
<RelativeURLs/>
<NoKeywords/>
</Constraints>
</AvailablePerformers>
</SMLQuery>';
$url='http://affiliate.streamate.com/?????/SMLResult.xml';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_TIMEOUT,120);
curl_setopt($curl,CURLOPT_HTTPHEADER,array (
'Content-Type: application/xml; charset=utf-8',
));
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $curlData);
$result = curl_exec($curl);
curl_close ($curl);
$xml = simplexml_load_string ($result);
// $json = json_encode($xml);
// $array = json_decode($json,TRUE);
?>
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Website Feed</title>
<description>Website Feed coded manually</description>
<link>yourdomain.com</link>;
<?php
foreach ($xml->AvailablePerformers->Performer as $perf) {
<item>
<title><php? echo ".$perf->attributes()->Name ."?></title>
<description><php? echo " .$perf->Descriptions->About ."?></description>
<link><php? echo "<a href='http://www.mydomain.com/cam/".$perf->attributes()->Name." /?AFNO=X-0-XXXXXX-XXXXXX&UHNSMTY=XXX?></link>
</item>
</channel>
</rss>
?>
Just bumping this thread in hope someone can assist? Thanks.
UPDATE
ok I understand that I need to set the header content type. I have searched the web and found this:
<?php
header("Content-type:rss/xml");
?>
Is this correct for an rss feed? Where exactly in the page do I put this?
Also yet in my original code pasted above there is already a bit of code relating to content type stating:
'Content-Type: application/xml; charset=utf-8',
));
All I want is my original code to be an RSS Feed. I can't believe how difficult this is proving to find information on.
You want to turn your example into a usable RSS feed you can plug into your WL and other types of sites. This is very easy with what you had previously. As an example lets pretend you create a PHP page on the example domain below:
hxxp://www.example.com/pull_xml.php
Put inside it this content:
<?php
$curlData = '<?xml version="1.0" encoding="UTF-8"?>
<SMLQuery>
<Options MaxResults="20"/>
<AvailablePerformers Exact="false" PageNum="1" CountTotalResults="true">
<Include>
<Country/>
<Headline/>
<Descriptions></Descriptions>
<Rating/>
<Age/>
<Gender/>
<Media>biopic</Media>
<FreeChatSort/>
<PerformanceSort/>
</Include>
<Constraints> <StreamType>live,recorded,offline</StreamType>
<PublicProfile/>
<Name></Name><NoKeywords/>
<RelativeURLs/>
<NoKeywords/>
</Constraints>
</AvailablePerformers>
</SMLQuery>';
$url='http://affiliate.streamate.com/????/SMLResult.xml';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_TIMEOUT,120);
curl_setopt($curl,CURLOPT_HTTPHEADER,array ('Content-Type: application/xml; charset=utf-8',));
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $curlData);
$result = curl_exec($curl);
curl_close ($curl);
echo $result;
?>
When you load that page into your browser you will see that it is an XML feed suitable for filling up your WL. To create a specific RSS feed from the data to work with a proprietary format expected by a certain service you will have to update your question with what format the feed should take. You can give an example of a feed you wish to emulate.
Also, you can change all URL's to your WL's URL structure by asking for the stream and substituting streamate.com with your whitelabel's domain.

gmail unread email count using curl

I created a script that can get the unread email list as a feed from the gmail here is my code
<?php
//function to get unread emails taking username and password as parameters
function check_email($username, $password)
{
//url to connect to
$url = "https://mail.google.com/mail/feed/atom";
// sendRequest
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
curl_close($curl);
//returning retrieved feed
return $curlData;
}
//making page to behave like xml document to show feeds
header('Content-Type:text/xml; charset=UTF-8');
//calling function
$feed = check_email("username", "password");
echo $feed;
?>
the output getting like this
<?xml version="1.0" encoding="UTF-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#">
<title>Gmail - Inbox for suneth2#gmail.com</title>
<tagline>New messages in your Gmail Inbox</tagline>
<fullcount>1282</fullcount>
<link rel="alternate" href="http://mail.google.com/mail" type="text/html" />
<modified>2012-08-01T12:33:48Z</modified>
<entry>
<title>eBCS Pro 1 August 2012</title>
<summary>bcs logo eBCS Pro 1 August 2012 Video interview Olympic IT The Met Police's director of IT, Steve</summary>
<link rel="alternate" href="http://mail.google.com/mail?account_id=test#gmail.com&message_id=138e21a3404cc7b2&view=conv&extsrc=atom" type="text/html" />
<modified>2012-08-01T12:12:44Z</modified>
<issued>2012-08-01T12:12:44Z</issued>
<id>tag:gmail.google.com,2004:1409100718455703474</id>
<author>
<name>eBCS Newsletter</name>
<email>e-bulletin#lists.bcs.org.uk</email>
</author>
</entry>
<entry>
so want to read the
<fullcount>1282</fullcount>
tag
when you pass the username and password to this function it can show the email list,
I need to get only the message count, is there any way to catch or count the items
this is the answer, i read the xml tag using php
$xmlobjc = new SimpleXMLElement($feed);
echo $xmlobjc->fullcount[0];
and replace the header with
header('Content-Type:text/html; charset=UTF-8');
This works too:
curl -u $(cat username):$(cat password) --silent 'https://mail.google.com/mail/feed/atom' | sed -n 's:.*<fullcount>\(.*\)</fullcount>.*:\1:p'

Categories