PHP Display JSON [duplicate] - php

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

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 get JSON content from url [duplicate]

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

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

Handling $_POST array [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Getting an array result from json_decode
JSON Decode in PHP
I need help to store some selected $_POST data into mysql. I don't know variables names, it is instant notification function from cartloom like paypal ipn.
ipn.php
<?php
$data = print_r($_POST);
$to = "hello065-65#yahoo.com";
$subject = "My subject";
$txt = $data;
mail($to,$subject,$txt,$headers);
?>
When i used print_r($_POST);
I received below content in my email..
order: {"billingFirstName":"sachin","billingLastName":"gupta","billingCompany":"df","billingAddress1":"33","billingAddress2":"","billingCity":"Chandigarh","billingState":"CT","billingPostalCode":"44444","billingCountry":"US","billingEmail":"hello065-65#yahoo.com","billingPhone":"","shippingFirstName":"","shippingLastName":"","shippingCompany":"","shippingAddress1":"","shippingAddress2":"","shippingCity":"","shippingState":null,"shippingPostalCode":null,"shippingCountry":null,"date":"Wed Apr 11 15:18:40 EDT 2012","transid":497070762,"invoice":1012,"grandTotal":"$0.00","payMethod":"none","comments":"","cartContents":[{"id":"48401","title":"Alex & Me - 10% Coupon","options":"","qty":"1","price":"0.00"}],"subTotal":"$0.00","discountAmount":"$0.00","discountTitle":"Complimentary Order","salesTaxTitle":"CT Sales Tax","salesTaxAmount":"$0.00","shippingTitle":null,"shippingAmount":"$0.00"}<br>
Can anyone help me, how to store billingFirstName sachin data into mysql..
It looks like JSON to me
you could try..
$order_details = json_decode($_POST['order']);
$the_variable_you_want = $order_details['billingFirstName'];
Edit: Updated the example to more clearly show you how to get the variable you want.

Categories