Echo XML Elements with file_get_contents - php

i'm successfully managing to pull in the information in this feed:
http://api.zoopla.co.uk/api/v1/zed_index?area=yo1&output_type=outcode&api_key=XXXXMYAPIKEYGOESHEREXXXXX
I'm doing this with the following code:
<p><?php $postcode = get_the_title(); $str = urlencode($postcode); $url = "http://api.zoopla.co.uk/api/v1/zed_index?area=$str&output_type=outcode&api_key=5dj2d5x8kd2z2vnk9g52gpap"; $string = file_get_contents($url); echo $string;?></p>
However, this just echos the following output:
DE45 http://www.zoopla.co.uk/home-values/de45 53.258037 53.138911 -1.580861 -1.79776 England Derbyshire 53.198474 -1.6893105 DE45 368929 375424 362103 372926 333441 329349 322644 368056
How could i adapt my existing code to successfully echo individual elements from the feed, for example just the following fields wrapped in tags:
zed_index
zed_index_1year
zed_index_2year
Thanks for your help!

You could use simplexml_load_file() to get an array which will contains every of your XML tags :
<p>
<?php
$XML_url = 'http://api.zoopla.co.uk/api/v1/zed_index?area=yo1&output_type=outcode&api_key=XXXXMYAPIKEYGOESHEREXXXXX';
$XML_parsed = simple_xml_load($XML_url);
// print for debug
echo '<pre>';
print_r( $XML_parsed );
echo '</pre>';
// Access one of the tag
$tagName = $XML_parsed['tagName'];
// Access a nested tag
$nestedTag = $XML_parsed['first_tag']['second_tag'];
?>
</p>

Related

PHP XML Foreach shows 1 review

Iam trying to loop a XML file with Foreach but doesn't work. There are like 30 reviews in the XML File but only shows one. It shows the first person in the list but then on the bottom.
Iam trying to get better at PHP so dont know allot about it for now.
This is the code that i use.
<?php
$url = 'https://mobiliteit.klantenvertellen.nl/xml/autorijschool-
wezemer%20' or die ('Niet verbonden');
$xml = simplexml_load_file($url);
foreach ($xml as $rijschool){
echo 'Voornaam: '.$rijschool->beoordeling->voornaam.'<br>';
echo 'Achternaam: '.$rijschool->beoordeling->achternaam.'<br>';
echo 'Woonplaats: '.$rijschool->beoordeling->woonplaats.'<br>';
echo 'Beschrijving: '.$rijschool->beoordeling->beschrijving.'<br>';
echo 'Aanbeveling: '.$rijschool->beoordeling->aanbeveling.'<br>';
echo 'Service: '.$rijschool->beoordeling->service.'<br>';
echo 'Deskundigheid: '.$rijschool->beoordeling->deskundigheid.'<br>';
echo 'Prijskwaliteit: '.$rijschool->beoordeling-
>prijskwaliteit.'<br>';
echo 'Gemiddelde: '.$rijschool->beoordeling->gemiddelde.'<br>'.'<br>';
}
?>
Edit: here is the XML file Link https://mobiliteit.klantenvertellen.nl/xml/autorijschool-wezemer%20
And here is what iam getting what current code shows
I think this is what you're trying to do:
<?php
$url = 'https://mobiliteit.klantenvertellen.nl/xml/autorijschool-wezemer%20';
$xml = simplexml_load_file($url);
foreach ($xml->beoordelingen->beoordeling as $rijschool){
echo 'Voornaam: '.$rijschool->voornaam.'<br>';
echo 'Achternaam: '.$rijschool->achternaam.'<br>';
echo 'Woonplaats: '.$rijschool->woonplaats.'<br>';
echo 'Beschrijving: '.$rijschool->beschrijving.'<br>';
echo 'Aanbeveling: '.$rijschool->aanbeveling.'<br>';
echo 'Service: '.$rijschool->service.'<br>';
echo 'Deskundigheid: '.$rijschool->deskundigheid.'<br>';
echo 'Prijskwaliteit: '.$rijschool->prijskwaliteit.'<br>';
echo 'Gemiddelde: '.$rijschool->gemiddelde.'<br>'.'<br>';
}
?>
The problem you are having is that your foreach is iterating over the topmost node, but you want to iterate over a node lower down in the tree.

PHP - Get the contents of an post from instagram page using simple php

is there a way to get instagram page the contents of an post using simple php
i did some search and i found this script
$url = 'https://www.instagram.com/pagename/';
$str = file_get_contents($url);
$count = 0;
if(preg_match('#followed_by": {"count": (.*?)}#', $str, $match)) {
$count = $match[1]; // get the count from Regex pattern
}
echo $count;
but it is getting only number of follower is there a way
to get the contents of an Instagram post using same concept ?
Here's a code that works (today). But as #Andy said, it's not reliable and it's dirty af :)
<?php
$source = file_get_contents("https://www.instagram.com/p/POST_ID/");
preg_match('/<script type="text\/javascript">window\._sharedData =([^;]+);<\/script>/', $source, $matches);
if (!isset($matches[1]))
return false;
$r = json_decode($matches[1]);
print_r($r);
// Example to get the likes count
// $r->entry_data->PostPage[0]->graphql->shortcode_media->edge_media_preview_like->count
i took #Andy advice as he it's not reliable and it's dirty af
So this is what i found to go over the html
Instagram change their page markup, your application will break.
is this
$username = 'username';
$instaResult=
file_get_contents('https://www.instagram.com/'.$username.'/media/');
//decode json string into array
$data = json_decode($instaResult);
foreach ($data as $posts) {
foreach($posts as $post){
$postit = (array) json_decode(json_encode($post), True);
/* get post text and image */
echo '<p>' .$postit["caption"]["text"].'</p>';
echo '<img src="'.$postit["images"]["standard_resolution"]["url"].'" />';
echo "</br>-----------</br>";
}
}

Passing content of web page as string argument in shell_exec()

If I pass string content then it works fine and on submit it shows me result.
But when I pass the content of webpage received using escapeshellarg(strip_tags($text));. It shows nothing on the screen.
<?php
//sent has value "http://www.paulgraham.com/herd.html"
$url=$_POST['sent'];
$text = file_get_contents($url);
$temp=escapeshellarg(strip_tags($text));
//$temp="one two two"; If I pass $temp with string content it gives result
echo $temp; //Echo $temp shows content of webpage
$output=shell_exec("/home/technoworld/Videos/LinSocket/Modular/x '$temp'");
echo $output;
?>
Thanks for comming here:
I got the answer:
<?php
//sent has value "http://www.paulgraham.com/herd.html"
$url=$_POST['sent'];
$text = file_get_contents($url);
$temp=escapeshellarg(strip_tags($text));
$output=shell_exec("/home/technoworld/Videos/LinSocket/Modular/x " . $temp);
echo $output;
?>

extracting multiple tags from xml using PHP

Here is my address.xml
<?xml version="1.0" ?>
<!--Sample XML document -->
<AddressBook>
<Addressentry>
<firstName>jack</firstName>
<lastName>S</lastName>
<Address>2899,Ray Road</Address>
<Email>jkjsvsdka#ghu.edu</Email>
</Addressentry>
<Addressentry>
<firstName>Sid</firstName>
<lastName>K</lastName>
<Address>238,Baseline Road,TX</Address>
<Email>sk#ghu.edu</Email>
<Email>sk#gmail.com</Email>
</Addressentry>
<Addressentry>
<firstName>Satya</firstName>
<lastName>Yar</lastName>
<Address>6,Rural Road,Tempe,AZ</Address>
<Email>syarlag#ghu.edu</Email>
<Email>ssya#gmail.com</Email>
<Email>satag#yahoo.com</Email>
</Addressentry>
</AddressBook>
I am trying to load all the entries using PHP code as below. Each addressentry can have one or more tags. Right now from the code below I am able to extract only one tag. My question is how do I extract all tags associated with particular Addressentry. that is I want to print all emails on the same line.
<?php
$theData = simplexml_load_File("address.xml");
foreach($theData->Addressentry as $theAddress) {
$theFirstName = $theAddress->firstName;
$theLastName = $theAddress->lastName;
$theAdd = $theAddress->Address;
echo "<p>".$theFirstName."
".$theLastName."<br/>
".$theAdd."<br/>
".$theAddress->Email."<br/>
</p>";
unset($theFirstName);
unset($theLastName);
unset($theAdd);
unset($theEmail);
}
?>
Any help would be appreciated
$emails = array();
foreach ($theAddress->Email as $email) {
$emails[] = $email;
}
echo "<p>".$theFirstName."
".$theLastName."<br/>
".$theAdd."<br/>
".implode(', ', $emails)."<br/>
</p>";

PHP SimpleXML Breaking when trying to traverse nodes

I'm trying to read the xml information that tumblr provides to create a kind of news feed off the tumblr, but I'm very stuck.
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if (!$xml)
{
exit('Failed to retrieve data.');
}
else
{
foreach ($xml->posts[0] AS $post)
{
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo .$title.;
echo '<p>'.$small_post.'</p>';
}
}
?>
Which always breaks as soon as it tries to go through the nodes. So basically "tumblr->posts;....ect" is displayed on my html page.
I've tried saving the information as a local xml file. I've tried using different ways to create the simplexml object, like loading it as a string (probably a silly idea). I double checked that my webhosting was running PHP5. So basically, I'm stuck on why this wouldn't be working.
EDIT: Ok I tried changing from where I started (back to the original way it was, starting from tumblr was just another (actually silly) way to try to fix it. It still breaks right after the first ->, so displays "posts[0] AS $post....ect" on screen.
This is the first thing I've ever done in PHP so there might be something obvious that I should have set up beforehand or something. I don't know and couldn't find anything like that though.
This should work :
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if ( !$xml ){
exit('Failed to retrieve data.');
}else{
foreach ( $xml->posts[0] AS $post){
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo $title;
echo '<p>'.$small_post.'</p>';
echo '<hr>';
}
}
First thing in you code is that you used root element that should not be used.
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if (!$xml)
{
exit('Failed to retrieve data.');
}
else
{
foreach ($xml->posts->post as $post)
{
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo .$title.;
echo '<p>'.$small_post.'</p>';
}
}
?>
$xml->posts returns you the posts nodes, so if you want to iterate the post nodes you should try $xml->posts->post, which gives you the ability to iterate through the post nodes inside the first posts node.
Also as Needhi pointed out you shouldn't pass through the root node (tumblr), because $xml represents itself the root node. (So I fixed my answer).

Categories