This question already has answers here:
SimpleXML: Selecting Elements Which Have A Certain Attribute Value
(2 answers)
Closed 7 years ago.
I have this xml file:
<friends>
<friend>
<name>xxx</name>
<pays>France</pays>
</friend>
<friend>
<name>yyy</name>
<country>France</country>
</friend>
<friend>
<name>zzz</name>
<country>USA</country>
</friend>
</friends>
To get my data, I am using this php code:
$xml = simplexml_load_file('friends.xml');
$friendsXML = $xml->friend;
Which works fine, but returns all of the friends.
Now I want to retrieve only friends who are from France:
country = 'france'.
Can anyone help me doing that?
I'd use XPath for things like this. Try:
$res = $xml->xpath('friend[country = "france"]');
echo $res[0];
Related
This question already has answers here:
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 2 years ago.
I need to take a img link (https://upload.wikimedia.org/wikipedia/en/5/51/Minecraft_cover.png) from this api https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles=Minecraft&pilicense=any. How to do it?
I wrote code like this, but I can print :
$img_url = "https://en.wikipedia.org/w/api.php?action=query&prop=pageimages&format=json&piprop=original&titles=Minecraft&pilicense=any";
$img_url = str_replace(" ", "%20", $img_url);
$img = json_decode(file_get_contents($img_url));
print_r ($img);
But how to print only img source?
The simplest way would be to use the following.
echo $img->query->pages->{'27815578'}->original->source;
Where 27815578 is the Page ID
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
I'm using picasaweb(google) API to get the profile picture by email address..
I get the output from the URL:
http://picasaweb.google.com/data/entry/api/user/adircohen#gmail.com?alt=json
How can I get the variable with the content:
https://lh3.googleusercontent.com/-9GSeL43L-A4/AAAAAAAAAAI/AAAAAAAAAAA/x8Uy6PTaS1o/s64-c/112285456748585606724.jpg
I've tried this, but it's not working:
$json = file_get_contents('http://picasaweb.google.com/data/entry/api/user/adircohen#gmail.com?alt=json');
$obj = json_decode($json);
echo $obj->gphoto->$thumbnail;
Getting content from URL using this function file_get_contents();
$page = file_get_contents("http://picasaweb.google.com/data/entry/api/user/adircohen#gmail.com?alt=json");
$ap = json_decode($page); // decode the json record
$ap = (array)$ap->entry; // convert object to array for further process
$ap = (array)$ap['gphoto$thumbnail']; // convert object to array for
getting exact output as image url
echo $ap['$t']; // display the output
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
DOMDocument::load - PHP - Getting attribute value
I have many div tags pulled from a string through php, each of them having a unique id and a subjective class. I am trying to get the id and class of each of the divs but am not too sure how I would do this.
HTML:
<div id='x1y1' class = 'classname'></div><div id = 'x2y1' class = 'classname1'>
so far I have tried
$html = new DOMDocument();
$html->loadHTML($boardDataStripSlashes);
$elements = $html->getElementsByTagName('div');
but have not been able to find anything on how to get the actual id's and classes of the selected elements.
You need to use DOMElement::getAttribute to retrieve attributes of elements.
foreach($elements as $element) {
$id = $element->getAttribute('id');
$className = $element->getAttribute('class');
// ...
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to access object properties with names like integers?
I tried to decode the json result from youtube data api by the following code:
$url="http://gdata.youtube.com/feeds/api/videos/$id?v=2&alt=jsonc";
echo "$url".'<BR>';
$json = file_get_contents($url,0,null,null);
$json_output = json_decode($json);
$items=$json_output -> data;
$content = "{$items->content->1}";
echo $content.'<BR>';
Everything works fine but the last two lines. Could someone please help?
And Here is the json result:
{"apiVersion":"2.1","data":{"id":"9jDg3Dh28rE","uploaded":"2012-10-04T03:45:49.000Z",
........
"content":{"5":"http://www.youtube.com/v/9jDg3Dh28rE?version=3&f=videos&app=youtube_gdata",
"1":"rtsp://v5.cache8.c.youtube.com/CiILENy73wIaGQmx8nY43OAw9hMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp",
"6":"rtsp://v5.cache4.c.youtube.com/CiILENy73wIaGQmx8nY43OAw9hMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"},"duration":2403,......}}
You need to wrap the numeric property with {} to access it.
$content = $items->content->{1};
And you also don't need to use double quotes like below:
$thumbnail = "{$items->thumbnail->sqDefault}";
This should just be
$thumbnail = $items->thumbnail->sqDefault;
This question already has answers here:
How to generate XML file dynamically using PHP?
(8 answers)
Closed 8 years ago.
I want to now if there is some inbuilt function that will create an XML directly from MYSQL result-set after executing SELECT query ?
I just wrote this and then thought id search to see if anyone else had written it. It looks simpler than the accepted answers tutorials. My $results comes from $result = mysql_query($query,$link);
$xmlDom = new DOMDocument();
$xmlDom->appendChild($xmlDom->createElement('results'));
$xmlRoot = $xmlDom->documentElement;
while ( $row = mysql_fetch_row($result) )
{
$xmlRowElementNode = $xmlDom->createElement('row');
$i=0;
for($i=0;$i<mysql_num_fields($result);$i++)
{
$xmlRowElement = $xmlDom->createElement(mysql_field_name($result,$i));
$xmlText = $xmlDom->createTextNode($row[$i]);
$xmlRowElement->appendChild($xmlText);
$xmlRowElementNode->appendChild($xmlRowElement);
}
$xmlRoot->appendChild($xmlRowElementNode);
}
header('Content-type: text/xml');
echo $xmlDom->saveXML();
This will procude XML in the form of
<results>
<row1>
<fieldname1>value</fieldname1>
<fieldname2>value</fieldname2>
<fieldname3>value</fieldname3>
<fieldname4...>value</fieldname4...>
</row1>
<row2>
<fieldname1>value</fieldname1>
<fieldname2>value</fieldname2>
<fieldname3>value</fieldname3>
<fieldname4...>value</fieldname4...>
</row2>
<row3...>
<fieldname1>value</fieldname1>
<fieldname2>value</fieldname2>
<fieldname3>value</fieldname3>
<fieldname4...>value</fieldname4...>
</row3...>
</results>
For any SELECT query.
There is no inbuilt function that will create XML directly from MYSQL result-set after executing SELECT query.
You have to write code for this
Some nice tutorials are this..
http://www.codediesel.com/php/converting-mysql-queries-to-xml/
http://www.mightywebdeveloper.com/coding/mysql-to-xml-php/
Generally your MySQL result will be returned as an array or an object, which you can then convert to XML or another format. You can use SimpleXML, which has been explained here: How to convert array to SimpleXML