I have this code but I can't make the items to show properly, I can only show the data from echo $data sentence but not $item FirstName or Item Bio
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID=17965'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$items = json_decode($data, true);
foreach ($items as $item)
{
$name = $item = ["FirstName"];
$bio = $item = ["Bio"];
}
echo $data
if you see the json response it has 2 elements.
{
message : ...,
department : ....
}
so you have to loop throw $items['department'] rather than $items
and you can access the data like $item['Bio']
So you can change the code like this to get the information
<?php
$url = 'https://jdublu.com/api/wrsc/json_employee.php?RID=17965';
$data = file_get_contents($url); // into a variable
$items = json_decode($data, true); // decode the JSON feed
foreach ($items['department'] as $item)
{
$name = $item["FirstName"];
$bio = $item["Bio"];
echo $name . ' ' . $bio . PHP_EOL;
}
Related
when i type in a movie name in the search field i want to display all the movies that is the json file but with this code i can only get one of the movies can you please help me with this.
<?php
if (isset($_POST['submit-search'])) {
$txtresult = $_POST['search'];
function getImdbRecord($title, $ApiKey)
{
$path = "http://www.omdbapi.com/?s=$title&apikey=$ApiKey";
$json = file_get_contents($path);
return json_decode($json, TRUE);
}
$data = getImdbRecord($txtresult, "f3d054e8");
echo "<div class = 'info-box'><img src =".$data['Poster']."</img><h3> Name :".$data['Title']."</h3><h3> Year : ".$data['Year']."</h3><h3> Duration : ".$data['Runtime'],"</h3></div>";
}
?>
you have need to use foreach loop to get all the search result.
like.
$data = getImdbRecord($txtresult, "f3d054e8");
foreach($data['Search'] as $value){
echo "<div class = 'info-box'><img src =".$value['Poster']."</img><h3> Name :".$value['Title']."</h3><h3> Year : ".$value['Year']."</h3><h3> Duration : ".$value['Runtime'],"</h3></div>";
}
Complete code will be.
<?php
//add function out side the if condition
function getImdbRecord($title, $ApiKey){
$path = "http://www.omdbapi.com/?s=$title&apikey=$ApiKey";
$json = file_get_contents($path);
return json_decode($json, TRUE);
}
if (isset($_POST['submit-search'])) {
$txtresult = $_POST['search'];
$data = getImdbRecord($txtresult, "f3d054e8");
//use loop to get all the seacrh result.
foreach($data['Search'] as $value){
echo "<div class = 'info-box'><img src =".$value['Poster']."</img><h3> Name :".$value['Title']."</h3><h3> Year : ".$value['Year']."</h3><h3> Duration : ".$value['Runtime'],"</h3></div>";
}
}
?>
I am trying to return a json array after i parse an rss feed.
this is my code :
<?php
header('Content-Type: application/json');
$feed = new DOMDocument();
//http://www.espnfc.com/rss
//http://www.football365.com/topical-top-10/rss
$feed->load('http://www.espnfc.com/rss');
$json = array();
$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');
$json['item'] = array();
$i = 0;
foreach($items as $item) {
$i = $i+1;
$title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$link = $item->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
$img = $item->getElementsByTagName('enclosure')->item(0)->attributes->getNamedItem('url')->value;
//$img = $item;echo($url);
$json['item'][] = array("title"=>str_replace(array("\n", "\r", "\t","'"), ' ', $title),"link"=>str_replace(array("\n", "\r", "\t","'"), ' ', $link),"img"=>str_replace(array("\n", "\r", "\t","'"), ' ', $img));
}
print_r($json['item'][0]);
//echo json_encode($json['item']);
?>
after iterating all items i finally would like to echo them as a result:
echo json_encode($json['item']);
the problem that's not showing any thing in browser. but when i moved this line into the foreach bloc it show result (of course with redundancy).
Some of the items don't have an <enclosure> tag, so the script gets an error when it tries to access the url attribute. You need to check for this.
$enclosures = $item->getElementsByTagName('enclosure');
if ($enclosures->length) {
$img = $item->getElementsByTagName('enclosure')->item(0)->attributes->getNamedItem('url')->value;
} else {
$img = '';
}
Your code returns request status "Status Code:500 Internal Server Error"
You can easy see it by browsing the network tab of your browser's web tools.
This is because on the 3rd post there is no image.
<?php
// Json Header
header('Content-Type: application/json');
// Get Feed
$feed = new DOMDocument();
$feed->load('http://www.espnfc.com/rss');
// Get Items
$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');
// My json object
$json = array();
$json['item'] = array();
// For each item
foreach($items as $item){
// Get title
$title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
// Get link
$link = $item->getElementsByTagName('link')->item(0)->firstChild->nodeValue;
// Get image if it exist
$img = $item->getElementsByTagName('enclosure');
if($img->length>0){
$img = $img->item(0)->attributes->getNamedItem('url')->value;
} else {
$img = "";
}
array_push($json['item'], array(
"title" => preg_replace('/(\n|\r|\t|\')/', ' ', $title),
"link" => preg_replace('/(\n|\r|\t|\')/', ' ', $link),
"img" => preg_replace('/(\n|\r|\t|\')/', ' ', $img)
));
}
echo json_encode($json['item']);
?>
I need help with fetching only attraction data,
i have this php script on my website:
<?php $json_string = 'http://framecreators.nl/efteling/data.php'; $jsondata file_get_content($json_string); $json_o=json_decode(utf8_encode($data)); $attractie = $json_o['Id']['Type']; echo $attractie ?>
and this json data:
http://framecreators.nl/efteling/data.php
I need to convert only a Id and type, ff somebody can help me.
To access the data you're looking for, you'll need to do something like this:
foreach ($json_o['AttractionInfo'] as $a) {
echo $a['Id']; //or whatever you're planning to do with this data
ech0 $a['Type'];
}
<?php
$json_data = file_get_contents('http://framecreators.nl/efteling/data.php');
$data = json_decode($json_data);
$array = [];
foreach($data->AttractionInfo as $item) {
$array['id'][] = $item->Id;
$array['type'][] = $item->Type;
}
echo "<pre>";
print_r($array);
echo "</pre>";
$url = "http://framecreators.nl/efteling/data.php";
$content = file_get_contents($url);
$data = json_decode($content,TRUE);
$array = $data['AttractionInfo'];
foreach($array as $r)
{
echo "ID->".$r['Id'];
echo '';
echo "Type->".$r['Type'];
echo '';
}
$url = "http://framecreators.nl/efteling/data.php";
$content = file_get_contents($url);
$data = json_decode($content,TRUE);
$array = $data['AttractionInfo'];
if(is_array($array))
{
foreach($array as $r)
{
echo $r['Id'];
echo $r['Type'];
}
}
I'm at a 24 hour hackathon trying to solve this so, excuse if it's a little rushed.
1st for each loop works fine, I'm getting a list of categories from this url
https://dev.xola.com/api/categories
I grab the list with this
$fullurl = "https://dev.xola.com/api/categories";
$string .= file_get_contents($fullurl); // get json content
$json_a = json_decode($string, true); //json decoder
then loop it with this
<?
foreach($json_a as $v)
{?>
echo $v ?}>
Now with the second for each look, I want to grab the items from this url
https://dev.xola.com/api/experiences
that match the category from the last url
so samething
$fullurl = "https://dev.xola.com/api/categories";
$string .= file_get_contents($fullurl); // get json content
$json_b = json_decode($string, true); //json decoder
here's the complete loop I tried
<?
$i=0;
foreach($json_a as $v)
$i++
{?>
echo $v ?
foreach($json_b as $x){?>
if($v==$x):
echo $v
endif;
?>
}?>
This will create a $result array with only the data that had the categories early acquired:
<?php
$categories_url = "https://dev.xola.com/api/categories";
$data = file_get_contents($categories_url);
$categories = json_decode($data, true);
$experiences_url = "https://dev.xola.com/api/experiences";
$data = file_get_contents($experiences_url);
$experiences = json_decode($data, true);
$result = array();
foreach ($experiences['data'] as $experience)
{
if (in_array($experience['category'], $categories))
{
$result[] = $experience;
}
}
print_r($result);
And you can easily read the result with:
foreach ($result as $item)
{
echo $item['category'], "\n";
echo $item['desc'], "\n";
//... other data available ...
}
The data structure of the experiences JSON is not the same as the categories JSON, therefore if($v==$x) will never match. If you want to find all results in experiences with a category from the categories url you can do the following:
<?
$BASE_URL = 'https://dev.xola.com/api/';
$categories = json_decode(file_get_contents($BASE_URL . 'categories'));
$experiences = json_decode(file_get_contents($BASE_URL . 'experiences'));
$matches = array();
foreach( $categories as $category ) {
foreach( $experiences->data as $experience ) {
if( $experience->category === $category ) {
$matches[] = $experience;
}
}
}
?>
<? foreach( $matches as $match ) : ?>
<? echo $match->category; ?><br>
<? endforeach; ?>
I want to turn the variable into an array so I can store more than one feed?
<?php
error_reporting(0);
$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');
$xml = $feed_lifehacker_full;
//print_r($xml);
foreach ($xml->channel->item as $node){
$title = $node->title;
$link = $node->link;
$link = explode('/', $link);
$link = $link[8];
$url = $node->url;
$description = $node->description;
$pubDate = $node->pubDate;
preg_match_all('#(http://img[^\s]+(?=\.(jpe?g|png|gif)))#i', $description[0], $images);
$images = $images[0][1] . '.jpg';
if($images == '.jpg'){
//uncomment to show youtube articles
//$images = "http://placehold.it/640x360";
//echo "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
} else {
//article image
$images . '<br>';
echo "<a href='page2.php?a=$link' title='$title'><img src='$images' /></a><br>";
}
}
How can I change this to load to arrays,
$feed_lifehacker_full = simplexml_load_file('http://feeds.gawker.com/lifehacker/full');
$xml = $feed_lifehacker_full;
The script is just gathering the image of an rss feed and linking to a page, if you see how it can be done more efficiently feel free to say
it is possible to encode the result given as json and by decoding it it will return you an array
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json, TRUE);