PHP get JSON content from url [duplicate] - php

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

Related

How to get a object from JSON to PHP [duplicate]

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

PHP JSON Encode or Decode? [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
So I have some relatively simple JSON I'm trying to display using PHP and I'm getting stuck, I think perhaps I'm not using decode or encode correctly. Maybe I simply overlooked something.
Here's the JSON...
{
"numFound": 43640,
"start": 0,
"maxScore": 0.7847167,
"docs": [],
"facets": {}
}
Here's my PHP...
<?php
$json_returned = file_get_contents("URL_OF_JSON_SOURCE");
$decoded_results = json_decode($array, true);
{
foreach($decoded_results as $results){
echo "Number Found:".$results['numFound'].";
echo "Start:".$results['start'].";
}
}
?>
I'm primarily just trying to get "numFound", "start", and "maxScore" to display. Thanks for any help, or even taking the time to read this post.
Here's the source JSON..
https://api.data.gov/gsa/fbopen/v0/opps?q=technology&data_source=FBO&limit=1&show_closed=true&api_key=CTrs3pcYimTdR4WKn50aI1GcUxyL9M4s1fyBbSer
You don't have any JSON Array in the given data. So firstly you don't have to loop returned data. Secondly you just forget double quotes in the loop. Third you don't have to join strings if you got any of them is null.
Here is the solution :
<?php
$result = json_decode( file_get_contents("sth"), true );
echo 'Number Found :'.$result["numFound"].'<br/>';
echo 'Start :'.$result["start"].'<br/>';
You php code is messed up
<?php
$json_returned = file_get_contents("https://api.data.gov/gsa/fbopen/v0/opps?q=technology&data_source=FBO&limit=1&show_closed=true&api_key=CTrs3pcYimTdR4WKn50aI1GcUxyL9M4s1fyBbSer");
$decoded_results = json_decode($json_returned, true);
echo "Number Found:".$decoded_results['numFound']." ";
echo "Start:".$decoded_results['start'];
?>

PHP: get parent node where child = 'value' [duplicate]

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];

PHP Display JSON [duplicate]

This question already has answers here:
Handling data in a PHP JSON Object
(3 answers)
Closed 8 years ago.
I want to do the following in PHP:
Get json data from http://domain.com/api/user/1
This provides:
{"name":"Joe","password":"something","email":"something#something.com"}
I want to be able to do a simple php echo to display the json data on a webpage
<html><?php echo $password ?></html>
<html><?php echo $name ?></html>
<html><?php echo $email ?></html>
So basically, creating a php for the "name" / "password" or "email" part of json results
You can do like this:
<?php
$json = '{"name":"Joe","password":"something","email":"something#something.com"}';
$obj = json_decode($json);
print $obj->{'name'}; // Joe
print $obj->{'password'}; // something
print $obj->{'email'}; // something#something.com
?>
for more info: http://in1.php.net/json_decode

PHP json decode with number tag [duplicate]

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;

Categories