PHP Flickr API - Retrieve Title from flickr.photosets.getInfo Response - php

I am getting the correct response from Flickr which is:
<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<photoset id="72157630469695108" owner="15823425#N00" primary="5110549866" secret="fd716fb5ee" server="1136" farm="2" photos="101" count_views="67" count_comments="0" count_photos="101" count_videos="0" can_comment="0" date_create="1341701808" date_update="1345054078">
<title>Montana</title>
<description />
</photoset>
</rsp>
For some reason I can't get the title, I've tried the following:
$album_info = simplexml_load_file($album_info_xml); // this is what the response is stored in
echo $album_info['photoset']['title'];
foreach($album_info->photoset as $ps) {
echo $ps['title'];
}
And a couple of other crazy things, I know it's something silly but don't know what it is I have missed.
Response can be seen here: http://www.flickr.com/services/api/explore/flickr.photosets.getInfo
Just use 72157630469695108 as the set id or alternatively use this url: http://api.flickr.com/services/rest/?method=flickr.photosets.getInfo&api_key=7ccedd2c89ca10303394b8085541d9de&photoset_id=72157630469695108

You should use SimpleXMLElement directly, then xpath to find your node.
$album_info = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?>
<rsp stat="ok">
<photoset id="72157630469695108" owner="15823425#N00" primary="5110549866" secret="fd716fb5ee" server="1136" farm="2" photos="101" count_views="67" count_comments="0" count_photos="101" count_videos="0" can_comment="1" date_create="1341701808" date_update="1345054078">
<title>Montana</title>
<description />
</photoset>
</rsp>'); // this is what the response is stored in
$result = $album_info->xpath('//title');
foreach ($result as $title)
{
echo $title . "\n";
}
Working example.

For anyone in the same position, this did the trick
$albumName = $album_info->photoset->title;

Related

Want to show XML view in html using php

I have oai.php page.Now when i type in url oai.php?verb=identify then want to show the contents in identify.xml.But i want the same url(oai.php?verb=identify) when i view the identify.xml.Below code showing only data of xml.
<?php
include "config.php";
$id= $_GET['verb'];
//echo $id;
if($id=="identify")
{
$date_mod = gmdate("Y-m-d\TH:i:s\Z");
$rss_txt ='<?xml version="1.0" encoding="utf-8"?>
<OAI-PMH xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.openarchives.org/OAI/2.0/" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>' .$date_mod. '</responseDate>
<request verb="Identify">http://mywebsite.org/oai</request>
<Identify>
<repositoryName>mydomain</repositoryName>
<baseURL>mywesite.com</baseURL>
<protocolVersion>2.0</protocolVersion>
<adminEmail>myemail</adminEmail>
<earliestDatestamp>'.$date_mod.'</earliestDatestamp>
<deletedRecord>transient</deletedRecord>
<granularity>YYYY-MM-DDThh:mm:ssZ</granularity>
</Identify>
</OAI-PMH>';
}
else if($id=="ListRecords")
{
include "config.php";
$date_mod = gmdate("Y-m-d\TH:i:s\Z");
$viewss1=$con->query("SELECT COUNT(upload_paper_id) FROM upload_papers");
$cview1=$viewss1->fetch_row();
$countt1=$cview1[0];
$rss_txt = '<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="http://localhost/oai-file/data/oai2.xsl" ?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>' .$date_mod. '</responseDate>
<request verb="ListRecords" metadataPrefix="oai_dc">http:mywebsite.org</request>
<ListRecords>';
$query = "SELECT * FROM upload_papers";
$result=$con->query($query);
while($values_query = mysqli_fetch_assoc($result))
{
$year= $values_query['year'];
$upload_paper_id= $values_query['upload_paper_id'];
$rss_txt = '<record>
<header>
<identifier>' .$upload_paper_id. '</identifier>
<datestamp>' .$date_mod. '</datestamp>
<setSpec>mydoc8:BIH</setSpec>
</header>
<metadata>
<oai_dc:dc
xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/
http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
<dc:title xml:lang="en-US">' .$values_query['paper_title']. '</dc:title>
<dc:creator>' . $values_query['author_name']. '</dc:creator>
<dc:creator>' . $values_query['co_author_name']. '</dc:creator>
<dc:description xml:lang="en-US">' .$values_query['abstract']. '</dc:description>
<dc:publisher xml:lang="en-US">' .$values_query['journal_nam']. '</dc:publisher>
<dc:contributor xml:lang="en-US">' .$values_query['author_name']. '</dc:contributor>
<dc:type xml:lang="en-US">Article</dc:type>
<dc:format>application/pdf</dc:format>
<dc:identifier>http://mywebsite'.$year.'/article.php?page='.$upload_paper_id.'</dc:identifier>
<dc:language>en</dc:language>
</oai_dc:dc>
</metadata>
</record>';
}
$rss_txt = '<resumptionToken
completeListSize="'.$countt1.'"
cursor="0">'.$date_mod.'</resumptionToken>
</ListRecords>
</OAI-PMH>';
}
else if($id=="ListSets")
{
$rss_txt =file_get_contents("ListSets.xml");
}
header('Content-type: application/xml');
echo $rss_txt;
?>
Above code shows only the data of xml.The output attached below
And i want the output like
.If i type verb=identify in url then it want to show identify.xsd or xml file any one.Then i type Listsets Want to show the content in that page.if verb equal to empty want to show the identify page.Please help me.Its really irritating me.Thank u
There are a few problems with the code. The main thing is to set your header to be XML content type. This will tell the browser what to expect. But there are a few other issues.
$id= $_GET['verb'];
//echo $id;
if($id=="identify")
{
$date_mod = gmdate("Y-m-d\TH:i:s\Z");
$rss_txt ='<?xml version="1.0" encoding="utf-8"?>
<OAI-PMH xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.openarchives.org/OAI/2.0/" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>' .$date_mod. '</responseDate>
<request verb="Identify">http://mywebsite.org/oai</request>
<Identify>
<repositoryName>mydomain</repositoryName>
<baseURL>mywesite.com</baseURL>
<protocolVersion>2.0</protocolVersion>
<adminEmail>myemail</adminEmail>
<earliestDatestamp>'.$date_mod.'</earliestDatestamp>
<deletedRecord>transient</deletedRecord>
<granularity>YYYY-MM-DDThh:mm:ssZ</granularity>
</Identify>
</OAI-PMH>';
}
else if($id=="ListRecords")
{
$rss_txt =file_get_contents("compend1.xml");
}
else if($id=="ListSets")
{
$rss_txt =file_get_contents("ListSets.xml");
}
header('Content-type: application/xml');
echo $rss_txt;
You had $rss_txt .= which with the . means append the string, which as you didn't declare it anywhere gives you the error about an undefined variable. Also with the content - XML needs to start right at the beginning of the string, so remove the whitespace.
The main one is having...
header('Content-type: application/xml');
But, for this to work properly, you need to ensure you comment out the echo, which I assume was some debugging code.
When doing an if - a single = is used to assign a value, so you need == to compare the values.
You can use $_GET[] to access the parameters rather than the way you were using, a lot simpler.
I've used file_get_contents() to fetch the data from those other files and moved the echo to the end so they all have the same logic.

PHP - Parse, Read XML

Not too sure what I'm doing wrong with parsing/reading an xml document.
My guess is that it's not standardized, and I'm going to need a different process to read anything from the string.
If that's the case, then I'm rather excited to learn how someone would read the xml.
Here's what I've got, and what I'm doing.
example.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="someurl.php"?>
<response>
<status>Error</status>
<error>The error message I need to extract, if the status says Error</error>
</response>
read_xml.php
<?php
$content = 'example.xml';
$string = file_get_contents($content);
$xml = simplexml_load_string($string);
print_r($xml);
?>
I'm getting no result back from the print_r.
I switched the xml to something more standard, like:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
...and it worked fine. So I'm sure it's due to a non-standard format, passed back from the source I'm getting it from.
How would I extract the <status> and <error> tags?
Tek has a good answer, but if you want to use SimpleXML, you can try something like this:
<?php
$xml = simplexml_load_file('example.xml');
echo $xml->asXML(); // this will print the whole string
echo $xml->status; // print status
echo $xml->error; // print error
?>
EDIT: If you have multiple <status> and <error> tags in your XML, have a look at this:
$xml = simplexml_load_file('example.xml');
foreach($xml->status as $status){
echo $status;
}
foreach($xml->error as $error){
echo $error;
}
I'm assuming <response> is your root. If it isn't, try $xml->response->status and $xml->response->error.
I prefer to use PHP's DOMDocument class better.
Try something like this:
<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="someurl.php"?>
<response>
<status>Error</status>
<error>The error message I need to extract, if the status says Error</error>
</response>';
$dom = new DOMDocument();
$dom->loadXML($xml);
$statuses = $dom->getElementsByTagName('status');
foreach ($statuses as $status) {
echo "The status tag says: " . $status->nodeValue, PHP_EOL;
}
?>
Demo: http://codepad.viper-7.com/mID6Hp

Why can't I embed php in the xml file? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Escaping <? on php shorthand enabled server when using require
What I want is that when I make a Ajax get request to domain/xml.php. It return a XML file, so I can use httpRequest.responseXML to parse the XML file.
what I did is:
<?php
$name = 'Just a tester';
?>
<?xml version='1.0' ?>
<name><?php echo $name ?></name>
But the parser gives me an error of the line <?xml version='1.0' ?>, I thought it might be syntax conflict with the php delimiter <?php.
How can I request a url and get xml generated by php?
You have shorttags enabled. This is the default, and as of PHP 5.4, tags are supported everywhere, regardless of shorttags settings.
The problem is that <?xml version='1.0' ?> starts and ends with <? ?>, just like PHP with shorttags.
To get round this use:
echo "<?xml version='1.0' ?>";
on that line.
Why are you trying to embed PHP variables into XML instead of generating the XML with PHP?
Example (xml.php):
<?php
header('Content-type: text/xml; charset=utf-8');
//Your Data
$persons = array(array('name'=>'bob','age'=>20,'sex'=>'M'),
array('name'=>'steve','age'=>26,'sex'=>'M'),
array('name'=>'jen','age'=>33,'sex'=>'F'),
);
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><persons/>');
foreach ($persons as $person) {
$node = $xml->addChild('person');
foreach($person as $key=>$value){
$node->addChild($key, $value);
}
}
//DOMDocument to format code output
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
echo $dom->saveXML();
/* OUTPUT
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person>
<name>bob</name>
<age>20</age>
<sex>M</sex>
</person>
<person>
<name>steve</name>
<age>26</age>
<sex>M</sex>
</person>
<person>
<name>jen</name>
<age>33</age>
<sex>F</sex>
</person>
</persons>
*/
?>
You could try:
<?php
header('Content-type: text/xml; charset=utf-8');
$name = 'Just a tester';
echo "<?xml version='1.0' ?>";
?>
<name><?php echo $name; ?></name>
Change
echo "<?xml...?>";
to
echo '<'."?...?".'>';
Or use Lawerence solution
You should set up your webserver to process not only PHP but XML files also through the PHP preprocessor.
You have short tags enabled. These should be disabled in your php.ini, search for "short_open_tag" and "asp_tag".
If you have an earlier version than PHP 5.3.0 this will probably work (if you only want this for the current document):
<?php ini_set('short_open_tag', 0); ?>

get Contents of the child tag using simeplxml_load_string in php

I have this XML
<parent>
<sms>
<response>
<message>message</message>
<reponse>text<response>
</response>
</sms>
<sms>
<response>
<message>message</message>
<reponse>text2<response>
</response>
</sms>
</parent>
I want to get the whole contents of response tag i.e <reponse1>text<response1> , <reponse2>text<response2> .which i will strore in an array. what i tried id
function xmlSplitUpResponseXML($xmlvalue)
{
$returnSplit = array();
$r = 0;
if(simplexml_load_string($xmlvalue))
{
$xml = simplexml_load_string($xmlvalue);
foreach($xml->children() as $child)
{
if(strtolower($child->getName())=='sms')
{
foreach ($child as $fields):
if(strtolower($fields->getName())=='response')
{
echo $fields;
}
}
} }
}
But the text is not echoed.
how can i do this.
Your XML isn't completly correct, but when fixed - try this:
echo $fields->message."<br>";
echo $fields->response."<hr>";
Works on your given example, with this fixed xml:
$string = "<parent><sms><response><message>message</message><response>text</response></response></sms><sms><response><message>message</message><response>text2</response></response></sms></parent>";
Hmm, seems like you need some XPath to the rescue...
Given the following fixed-up XML (note nested response nodes)
<parent>
<sms>
<response>
<message>message</message>
<response>text</response>
</response>
</sms>
<sms>
<response>
<message>message</message>
<response>text2</response>
</response>
</sms>
</parent>
The following PHP will do it for you.
<?php
$xml = simplexml_load_file( "sms.xml" );
// Find the sms response (note responses are nested in a parent response for some reason
$result = $xml->xpath('/parent/sms/response/response');
while(list( , $node) = each($result)) {
// to just output the value of the node
// echo $node;
// to wrap value in tags *bad idea*
echo $node->asXML();
}
?>
Not sure why you'd want to return an XML string, seems like a bad idea to me. What are you
doing with the output? Probably better to construct an XML document properly, just guessing...

XML file to PHP array (w/attributes)

i haven't really worked with xml files before, but now i'm trying to get an xml file into a php array or object.
the xml file looks like this: (it's for translating a web app)
<?xml version="1.0" ?>
<content language="de">
<string name="login">Login</string>
<string name="username">Benutzername</string>
<string name="password">Passwort</string>
</content>
i tried the following:
$xml = new SimpleXMLElement("de.xml", 0, 1);
print_r($xml);
unfortunately, the values of the 'name' attribute are for some reason not in the php object. i'm looking for a way that allows me to retrieve the xml values by the name attribute.
for instance:
$xml['username'] //returns "Benutzername"
how can this be done?
appreciate your help :) cheers!
This one should explain the function to you:
<?php
$xml = simplexml_load_file('de.xml');
foreach($xml->string as $string) {
echo 'attributes: '. $string->attributes() .'<br />';
}
?>
The attributes() method from SimpleXMLElement class will help you - http://de.php.net/manual/en/simplexmlelement.attributes.php
$xmlStr = <<<XML
<?xml version="1.0" ?>
<content language="de">
<string name="login">Login</string>
<string name="username">Benutzername</string>
<string name="password">Passwort</string>
</content>
XML;
$doc = new DomDocument();
$doc->loadXML($xmlStr);
$strings = $doc->getElementsByTagName('string');
foreach ($strings as $node) {
echo $node->getAttribute('name') . ' = ' . $node->nodeValue . PHP_EOL;
}
You can use an xpath expression to get the element with the name you are looking for:
(string)current($xml->xpath('/content/string[#name="username"]'))

Categories