How to read a json file into html table php? - php

Okay so this is my first time posting on this website. I need a little help, I am trying to add data from a json API into a html table. This is what I have so far.
<?php
$gamertag = 'x0--ii';
$jsonurl = 'https://tusticles.com/psn/api?psn='.$gamertag;
$json = file_get_contents($jsonurl);
var_dump(json_decode($json));
<?
Here is my Output:
object(stdClass)#1 (2) { ["status"]=> string(3) "200" ["response"]=> object(stdClass)#2 (5) { ["onlineId"]=> string(6) "x0--II" ["avatar"]=> string(77) "http://static-resource.np.community.playstation.net/avatar_m/SCEI/I0053_m.png" ["plus"]=> int(0) ["aboutMe"]=> string(0) "" ["trophies"]=> object(stdClass)#3 (6) { ["trophyLevel"]=> int(3) ["progress"]=> int(7) ["platinum"]=> int(0) ["gold"]=> int(2) ["silver"]=> int(6) ["bronze"]=> int(19) } } }
I want to display the image as well as all the values. Thanks for any help!

You would put the json_decode data into a PHP variable, and access it like a normal PHP object array
Here's a quick example:
<?php
$gamertag = 'x0--ii';
$jsonurl = 'https://tusticles.com/psn/api?psn='.$gamertag;
$json = file_get_contents($jsonurl);
$content = json_decode($json);
echo "ID: ".$content->response->onlineId."<br />";
echo '<img src="'.$content->response->avatar.'" />';
echo "<table border=1>";
foreach ($content->response->trophies as $key => $value){
echo "<tr>
<td>".$key."</td>
<td>".$value."</td>
</tr>";
}
echo "</table>";
?>
That would loop through your trophies put it into an HTML table.

<?php
$gamertag = 'x0--ii';
$jsonurl = 'https://tusticles.com/psn/api?psn='.$gamertag;
$json = file_get_contents($jsonurl);
#var_dump(json_decode($json, true));
/* json formatted for readability
object(stdClass)#1 (2) {
["status"]=> string(3) "200"
["response"]=> object(stdClass)#2 (5) { // response is an array()
["onlineId"]=> string(6) "x0--II"
["avatar"]=> string(77) "http://static-resource.np.community.playstation.net/avatar_m/SCEI/I0053_m.png"
["plus"]=> int(0)
["aboutMe"]=> string(0) ""
["trophies"]=> object(stdClass)#3 (6) { // trophies is an array()
["trophyLevel"]=> int(3)
["progress"]=> int(7)
["platinum"]=> int(0)
["gold"]=> int(2)
["silver"]=> int(6)
["bronze"]=> int(19)
}
}
}
*/
$json = json_decode($json, true);
$rsp = $json['response']; // response array
$trophies = $rsp['trophies']; // trophy array
// examples
$avatar = $rsp['avatar'];
$onlineId = $rsp['onlineId'];
$trophylevel = $trophies['trophyLevel'];
// inline html example
echo("<div><img src=\"".$rsp['avatar']."\" /></div>");
// or
echo("<div><img src=\"".$json['response']['avatar']."\" /></div>");
echo("<div>".$json['trophies']['progress']."</div>");
// or
echo("<div>".$json['response']['trophies']['progress']."</div>");
?>

Related

issue with array and json decode

I'm a little confused I have the following;
array(4) { ["id"]=> string(1) "2" ["result"]=> array(5) { ["account"]=> string(34) "rf2DKsfbZuCa2WwQAg49cRXqYuRCTszUTp" ["assets"]=> array(1) { ["rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ"]=> array(1) { [0]=> array(2) { ["currency"]=> string(3) "FLX" ["value"]=> string(9) "999999999" } } } ["ledger_hash"]=> string(64) "E44084AAC1F1AE2C7147675BF8F763ED7620DC82E1B1A5BD6DC6F640652F1B63" ["ledger_index"]=> int(17773072) ["validated"]=> bool(true) } ["status"]=> string(7) "success" ["type"]=> string(8) "response" }
I am trying to echo the currency;
I have the following but it's not working.
$response = websocket_read($sp,$errstr);
$decode = json_decode($response, true);
echo'<br><br>';
echo $decode['result']['assets']['currency'];
echo'<br><br>';
echo "Server responed with: '" . $response ."'\n";
The line that is not working is echo $decode['result']['assets']['currency'];
Thankyou
Your rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ has the value as an array, either remove the square brackets from this part to treat it as value
"rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ":[{"currency":"FLX","value":"999999999"}]
to
"rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ":{"currency":"FLX","value":"999999999"}
or use index to get the value
echo $decode['result']['assets']['rDroJrYXN7vRzLbH6tFXTViVtXGv5ZJGeZ'][0]['currency'];

WSDL to Array php

A web service that I want to use to put data into an array in the project
I did not see a good answer.
This web service returns the info of a blog post, how data is in loop?
By writing these codes, I got the arrays in a string:
<?php
$client = new
SoapClient('http://service.test.com/test_information.asmx?wsdl');
$param = array('username' => 'admin','password' => 'admin','feature' =>
'SOAP_SINGLE_ELEMENT_ARRAYS');
$result = $client->GetInfoWeblog($param);
$outterArray = ((array)$result);
$innerArray = ((array)$outterArray['GetInfoWeblogResult']);
$dataArray = ((array)$innerArray['listObject']);
$array = json_decode(json_encode($dataArray), True);
echo '<pre>';
var_dump($array);
output
array(1) {
["OPMWebBlog"]=>
array(2) {
[0]=>
array(9) {
["WebVcCode"]=>
int(1)
["WebTitle"]=>
string(9) "Webtitle1"
["WebBody"]=>
string(8) "WebBody1"
["WebUrl"]=>
string(7) "Weburl1"
["WebDesc"]=>
string(8) "WebDesc1"
["WebDatetimeInsert"]=>
string(19) "2007-05-08T12:35:00"
["WebDatetimeUpdate"]=>
string(19) "2018-11-06T14:56:00"
["WebTag"]=>
string(8) "Web Tag1"
["WebPublishActive"]=>
bool(true)
}
[1]=>
array(9) {
["WebVcCode"]=>
int(2)
["WebTitle"]=>
string(9) "Webtitle2"
["WebBody"]=>
string(8) "WebBody2"
["WebUrl"]=>
string(7) "Weburl2"
["WebDesc"]=>
string(8) "WebDesc2"
["WebDatetimeInsert"]=>
string(19) "2018-11-06T14:56:00"
["WebDatetimeUpdate"]=>
string(23) "2007-05-08T12:35:29.123"
["WebTag"]=>
string(8) "Web Tag2"
["WebPublishActive"]=>
bool(true)
}
I want to get these objects in a loop
WebTitle
WebBody
WebUrl
WebDesc
WebDatetimeInsert
WebTag
WebPublishActive
I think if you loop through $array['OPMWebBlog'] you will get what you want :
foreach ($array['OPMWebBlog'] as $item) {
echo $item['WebTitle'];
echo $item['WebBody'];
...
}

How to get facebook album photos id and time using php?

I'm new in php. I make below two code to get facebook album photos id and create time. But its not working and don't showing me result/ID, only showing me blank page.
Here is my two code with $json and $array output.
Code 1;
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token");
$json = json_decode($data);
echo $json->id;
echo $json->created_time;
?>
Code 1 Output: using var_dump($json);
object(stdClass)#1 (2) {
["data"]=>
array(3) {
[0]=>
object(stdClass)#2 (2) {
["id"]=>
string(15) "160246594547781"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
[1]=>
object(stdClass)#3 (2) {
["id"]=>
string(15) "160246581214449"
["created_time"]=>
string(24) "2017-08-04T18:09:12+0000"
}
[2]=>
object(stdClass)#4 (2) {
["id"]=>
string(15) "160246587881115"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
}
["paging"]=>
object(stdClass)#5 (1) {
["cursors"]=>
object(stdClass)#6 (2) {
["before"]=>
string(20) "MTYwMjQ2NTk0NTQ3Nzgx"
["after"]=>
string(20) "MTYwMjQ2NTg3ODgxMTE1"
}
}
}
Code 2:
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token");
$array = json_decode($data, true);
echo $array['data']['id'];
echo $array['data']['created_time'];
?>
Code 2 Output: Using var_dump($array);
array(2) {
["data"]=>
array(3) {
[0]=>
array(2) {
["id"]=>
string(15) "160246594547781"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
[1]=>
array(2) {
["id"]=>
string(15) "160246581214449"
["created_time"]=>
string(24) "2017-08-04T18:09:12+0000"
}
[2]=>
array(2) {
["id"]=>
string(15) "160246587881115"
["created_time"]=>
string(24) "2017-08-04T18:09:13+0000"
}
}
["paging"]=>
array(1) {
["cursors"]=>
array(2) {
["before"]=>
string(20) "MTYwMjQ2NTk0NTQ3Nzgx"
["after"]=>
string(20) "MTYwMjQ2NTg3ODgxMTE1"
}
}
}
Please help me to solve this issue.thanks
For first one you need to do:-
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?fields=id&access_token=$token");
$json = json_decode($data);
foreach($array.data as $arr){
echo $arr.id; echo PHP_EOL; // you can use `echo "<br/>";`
echo $arr.created_time;echo PHP_EOL;// you can use `echo "<br/>";`
}
?>
For second-one you can use:-
<?php
$token="<token>";
$data = file_get_contents("https://graph.facebook.com/106097030098624/photos?
fields=id&access_token=$token");
$array = json_decode($data, true);
foreach($array['data'] as $arr){
echo $arr['id']; echo PHP_EOL; // you can use `echo "<br/>";`
echo $arr['created_time'];echo PHP_EOL;// you can use `echo "<br/>";`
}
?>
Output:- https://eval.in/841721

Sending data from Mysql with Json using PHP

I am trying to send data from a Mysql database using Json. As long as it is only 1 post in the database it works, but when its more rows its nor working anymore.
Here is the PHP code:
// array for JSON response
$response = array();
$data = new CDatabaseInfo();
$result = $data ->selectAll();
$data->closeDatabase();
// check for empty result
if (isset($result)) {
$response["products"] = array();
foreach($result as $value) {
$product = null;
$product = array();
$product["pid"] = $value["pid"];
$product["name"] = $value["name"];
$product["price"] = $value["price"];
$product["description"] = $value["description"];
$product["created_at"] = $value["created_at"];
$product["updated_at"] = $value["updated_at"];
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
echo json_encode($response, TRUE);
echo "var dump <br>";
var_dump($response);
}
Here comes the result of var_dump:
array(2) { ["products"]=> array(2) {[0]=> array(6) { ["pid"]=> string(1) "1" ["name"]=> string(6) "Festis" ["price"]=> string(5) "20.00" ["description"]=> string(9) "God dryck" ["created_at"]=> string(19) "2015-07-09 20:31:30" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } [1]=> array(6) { ["pid"]=> string(1) "4" ["name"]=> string(4) "Cola" ["price"]=> string(5) "12.00" ["description"]=> string(11) "En fin läsk" ["created_at"]=> string(19) "2015-07-20 20:29:03" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } } ["success"]=> int(1) }
Thanks!
There is no such thing as json_encode($response, TRUE);
Be sure you get the right data from the database. Beside the little error, your code should run correctly.

Converting Json String into PHP array then using php array

Im new to json & php and I'm having some issues with json into php string
My json string looks like this
{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}
So Far i have created my string
$json_raw = '{"status":"OK","cards": [{"id":100001,"name": .....
Decoded the json
$arr = json_decode($json_raw, TRUE);
I var_dump($arr);
then it returns
array(2) { ["status"]=> string(2) "OK" ["cards"]=> array(4) { [0]=> array(8) { ["id"]=> int(100001) ["name"]=> string(6) "batman" ["image"]=> int(11111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T11:37:07Z" } [1]=> array(8) { ["id"]=> int(100002) ["name"]=> string(8) "superman" ["image"]=> int(111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:30:09Z" } [2]=> array(8) { ["id"]=> int(100003) ["name"]=> string(8) "catwoman" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:39:42Z" } [3]=> array(8) { ["id"]=> int(100004) ["name"]=> string(4) "bane" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-09-08T12:56:04Z" } } }
Now all I want to do is be able to use this data
e.g if name = batman then
I know this is a stupid question but I am struggling :(
Thank in Advance
json_decode() with TRUE as second parameter gives you an associative array. You need to access the correct index to do what you want.
To list the complete associative array with nice formatting, you can do:
echo '<pre>', print_r($arr), '</pre>';
Now, to access the name in your array:
$man = $arr['cards'][0]['name'];
To check if it's Batman (yay!):
if( isset($man) && $man == 'batman' ) {
# code ...
}
For getting the name of all similar names:
$man = $json['cards']['0']['name'];
for ($i=0; $i < count($json['cards']); $i++) {
echo $json['cards'][$i]['name']."\n";
}
See it live!
when you got the array
$arr = json_decode($json_raw, TRUE);
then check if cards key exist
if(array_key_exists('cards', $arr)){
foreach($arr['cards'] as $key=>$val){
echo $key; ///name, id..
echo $val; /// batman,...
if($key == 'name' && $val =='batman'){
//-------do your stuff
}
}
}
Try with:
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
EDIT:
Ok, so this worked for me using code above, try it yourself if you want:
<?php
$json_raw = '{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}';
$arr = json_decode($json_raw, TRUE);
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
?>

Categories