Retrieving Contact photo with Zend Gdata with PHP - php

When querying for contacts I use the code below to retrieve all my contacts
$gdata = new Zend_Gdata($client);
$gdata->setMajorProtocolVersion(3);
$query = new Zend_Gdata_Query(
"http://www.google.com/m8/feeds/contacts/default/full");
$feed = $gdata->getFeed($query);
As I look through each entry of the $feed I can get access to the contactId and according to the Contacts API I should be able to retrieve the picture by doing a GET on the following URL:
http://www.google.com/m8/feeds/photos/media/default/contactId
So I use the same mechanism to retrieve contacts and try to get a photo after setting $id:
$query = new Zend_Gdata_Query(
"http://www.google.com/m8/feeds/photos/media/default/$id");
$entryFeed = $gdata->getFeed($query);
But I get an error "DOMDocument cannot parse XML". Am I doing something wrong? Are there any example docs?

To get the photo use DOMXpath and search for the "//atom:link" tag, then use $gdata->get(href) to grab the photo. Check for the etag attribute for each link, this tells you whether or not there is a profile photo associated with this contact.
$doc = new DOMDocument;
$doc->recover = true;
$doc->loadXML($entry->getXML());
$xpath = new DOMXPath($doc);
$links = $xpath->query('//atom:link');
foreach($links as $link) {
if($link->getAttribute('etag') != "") {
$http_response = $gdata->get($link->getAttribute('href'));
$rawImage = $http_response->getBody();
$fp = fopen("/var/www/profile/$id.jpg", "w");
fwrite($fp, $rawImage);
fclose($fp);
break;
}
}

Related

Webscraping with Goutte and Guzzle

I have the following method from my controller that gets the data from the site:
$goutteClient = new Client();
$guzzleClient = new GuzzleClient([
'timeout' => 60,
]);
$goutteClient->setClient($guzzleClient);
$crawler = $goutteClient->request('GET', 'https://html.duckduckgo.com/html/?q=Laravel');
$crawler->filter('.result__title .result__a')->each(function ($node) {
dump($node->text());
});
The above code gives me the title of contents from the search results. I also want to get the link of the corresponding search result. That resides in class result__extras__url.
How do I filter the link in and the title at once? Or do I have to run another method for that?
Try to inspect the attributes of the nodes. Once you get the href attribute, parse it to get the URL.
$crawler->filter('.result__title .result__a')->each(function ($node) {
$parts = parse_url(urldecode($node->attr('href')));
parse_str($parts['query'], $params);
$url = $params['uddg']; // DDG puts their masked URL and places the actual URL as a query param.
$title = $node->text();
});
For parsing, I usually do the following:
$doc = new DOMDocument();
$doc->loadHTML((string)$crawler->getBody());
from then on, you can access using getElementsByTagName functions on your DOMDocument.
for example:
$rows = $doc->getElementsByTagName('tr');
foreach ($rows as $row) {
$cols = $row->getElementsByTagName('td');
$value = trim($cols->item(0)->nodeValue);
}
You can find more information in
https://www.php.net/manual/en/class.domdocument.php

xml generated not opening in browser

I wrote php code to create an xml but the xml file is not opening in browser
The following is the code:
$dom = new DOMDocument('1.0','UTF-8');
$dom->formatOutput = true;
$root = $dom->createElement('journal');
$dom->appendChild($root);
$journal_metadata = $dom->createElement('journal_metadata');
$dom->appendChild($journal_metadata);
$journal_metadata->appendChild($dom->createElement('full_title', 'Economics'));
$journal_metadata->appendChild($dom->createElement('abbrev_title', 'JJD'));
$issn = $dom->createElement('issn','2142');
$journal_metadata->appendChild($issn);
$issn->setAttribute('media_type', 'electronic');
$journal_metadata->appendChild($dom->createElement('doi_data', 'JJD'));
$doi_data = $dom->createElement('doi_data');
$journal_metadata->appendChild($doi_data);
$doi = $dom->createElement('doi', '10.E');
$doi_data->appendChild($doi);
$resource = $dom->createElement('resource', 'http://localhost/fo/journal.php?jid=1');
$doi_data->appendChild($resource);
// journal issue
$journal_issue = $dom->createElement('journal_issue');
$dom->appendChild($journal_issue);
// journal article
$journal_article = $dom->createElement('journal_article');
$dom->appendChild($journal_article);
$journal_article->setAttribute('publication_type', 'full_text');
$pages = $dom->createElement('pages');
$journal_article->appendChild($pages);
$resource = $dom->createElement('resource', htmlspecialchars('http://localhost/fo/abstracts.php?artID=472&jid=1'));
$doi_data->appendChild($resource);
echo '<xmp>'. $dom->saveXML() .'</xmp>';
$dom->save('result.xml') or die('XML Create Error');

Error while loading xml file?

Am dynamically loading an xml file and sending request to the api but getting
Warning: DOMDocument::loadXML(): Empty string supplied as input in /home/spotrech/public_html/ but this error is very inconsistent sometime appear sometime don't! I really no idea how to solve this. below is code
$rechargeApiUrl = "http://allrechargeapi.com/apirecharge.ashx?uid=$uid&apikey=$apike&number=$mobileNo&opcode=$opId&amount=$amount&ukey=$uniId&format=xml";
$url = file_get_contents($rechargeApiUrl);
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML(preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $url));
$itemInfo = $xmlDoc->getElementsByTagName('Result'); //returns an object.
$itemCount = $itemInfo->length;
foreach ($itemInfo as $userInfo) {
//Assigning node values to its specified variables.
$ukey = strtolower($userInfo->getElementsByTagName('ukey')->item(0)->childNodes->item(0)->nodeValue);
$status = $userInfo->getElementsByTagName('status')->item(0)->childNodes->item(0)->nodeValue;
$resultCode = $userInfo->getElementsByTagName('resultcode')->item(0)->childNodes->item(0)->nodeValue;
}
$strStatus = strtolower(trim($status));
$strResultCode = trim($resultCode);
$strCode = trim($ukey);
any response will be appreciated.Thank you

Call asp.net web service from php

Hi im calling an aps.net web service from a php service. The service searches both databases with a search parameter. Im not sure how to pass a search parameter to the asp.net service. Code is below. ( there is no search paramter currently but im just interested in how it would be passed to the asp.net service)
$link = mysql_connect($host, $user, $passwd);
mysql_select_db($dbName);
$query = 'SELECT firstname, surname, phone, location FROM staff ORDER BY surname';
$result = mysql_query($query,$link);
// if there is a result
if ( mysql_num_rows($result) > 0 ) {
// set up a DOM object
$xmlDom1 = new DOMDocument();
$xmlDom1->appendChild($xmlDom1->createElement('directory'));
$xmlRoot = $xmlDom1->documentElement;
// loop over the rows in the result
while ( $row = mysql_fetch_row($result) ) {
$xmlPerson = $xmlDom1->createElement('staff');
$xmlFname = $xmlDom1->createElement('fname');
$xmlText = $xmlDom1->createTextNode($row[0]);
$xmlFname->appendChild($xmlText);
$xmlPerson->appendChild($xmlFname);
$xmlSname = $xmlDom1->createElement('sname');
$xmlText = $xmlDom1->createTextNode($row[1]);
$xmlSname->appendChild($xmlText);
$xmlPerson->appendChild($xmlSname);
$xmlTel = $xmlDom1->createElement('phone');
$xmlText = $xmlDom1->createTextNode($row[2]);
$xmlTel->appendChild($xmlText);
$xmlPerson->appendChild($xmlTel);
$xmlLoc = $xmlDom1->createElement('loc');
$xmlText = $xmlDom1->createTextNode($row[3]);
$xmlLoc->appendChild($xmlText);
$xmlPerson->appendChild($xmlLoc);
$xmlRoot->appendChild($xmlPerson);
}
}
//
// instance a SOAP client to the dotnet web service and read it into a DOM object
// (this really should have an exception handler)
//
$client = new SoapClient('http://stuiis.cms.gre.ac.uk/mk05/dotnet/dataBind01/phoneBook.asmx?WSDL');
$xmlString = $client->getDirectoryDom()->getDirectoryDomResult->any;
$xmlDom2 = new DOMDocument();
$xmlDom2->loadXML($xmlString);
// merge the second DOM object into the first
foreach ( $xmlDom2->documentElement->childNodes as $staffNode ) {
$xmlPerson = $xmlDom1->createElement($staffNode->nodeName);
foreach ( $staffNode->childNodes as $xmlNode ) {
$xmlElement = $xmlDom1->createElement($xmlNode->nodeName);
$xmlText = $xmlDom1->createTextNode($xmlNode->nodeValue);
$xmlElement->appendChild($xmlText);
$xmlPerson->appendChild($xmlElement);
}
$xmlRoot->appendChild($xmlPerson);
}
// return result
echo $xmlDom
you might want to look at the Apache Stonehenge project, http://www.interoperabilitybridges.com/projects/apache-stonehenge it talks about mixing and matching PHP clients and webservices including those from ASP.NET
jas

SimpleXML - "Node no longer exists"

I'm trying to get the video data from this youtube playlist feed and add the interesting data to an array and use that later, but as you can see from the feed some videolinks are "dead" and that results in problems for my code.
The error I get is "Node no longer exists" when I try to access $attrs['url']. I've tried for hours to find a way to check if the node exists before I access it but I have no luck.
If anyone could help me to either parse the feed some other way with the same result or create a if-node-exists check that works I would be most happy. Thank you in advance
$url = 'http://gdata.youtube.com/feeds/api/playlists/18A7E36C33EF4B5D?v=2';
$sxml = simplexml_load_file($url);
$i = 0;
$videoobj;
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$videoobj[$i]['url'] = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = $attrs['url'];
$videoobj[$i]['title'] = $media->group->title;
$i++;
}
if ($media->group->thumbnail && $media->group->thumbnail[0]->attributes()) {
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = strval($attrs['url']);
$videoobj[$i]['title'] = strval($media->group->title);
}
SimpleXML's methods always return objects, which are themselves linked to the original document (some internal thingy related to libxml.) If you want to store that data for later use, cast it as a string, like this:
$videoobj[$i]['url'] = (string) $attrs['url'];
$videoobj[$i]['thumb'] = (string) $attrs['url'];
$videoobj[$i]['title'] = (string) $media->group->title;

Categories