get data from JSON file (with cURL) - php

I would like to trnsform some JSON data to a php array, this is my code:
<?php
$obj1=json_decode('http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json', true);
$championname = $obj1[data][aatrox][name];
echo $championname;
?>
The problem is that i don't know how to get the data from http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json
How can i make this code work?

Try with file_get_contents() like this:
$json = file_get_contents('http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json');
$obj1 = json_decode($json , true);
$championname = $obj1['data']['aatrox']['name'];
echo $championname;

Use file_get_contents to download a file.
$json = file_get_contents('http://ddragon.leagueoflegends.com/cdn/3.15.5/data/en_US/champion/Aatrox.json');
$obj1 = json_decode($json, true);

Related

Fetching data attributes from instagram hashtag

I want to fetch data(the images in each post) stored in https://www.instagram.com/explore/tags/selfie/?__a=1, but all I get when I decode and var_dump this is NULL.
$obj = json_decode("https://www.instagram.com/explore/tags/selfie/?__a=1", true);
var_dump($obj);
Before decoding json you have to first fetch api response.
$obj = json_decode(file_get_contents("https://www.instagram.com/explore/tags/selfie/?__a=1"), true);
You're trying to json_decode the STRING
https://www.instagram.com/explore/tags/selfie/?__a=1
What you need to do is to fetch the URL first. I suggest using file_get_contents, which takes a URL and returns the contents at the end of that URL.
Try this:
$json = file_get_contents("https://www.instagram.com/explore/tags/selfie/?__a=1");
$obj = json_decode($json, true);
var_dump($obj);
The argument to the function json_decode(), $html must be plaintext/string.
This should work.
$url = "https://www.instagram.com/explore/tags/selfie/?__a=1";
$html = file_get_contents($url);
$obj = json_decode($html,true);
var_dump($obj);
See this in action here

How to Json In PHP 'iyas'

http://simsimi.com/getRealtimeReq?uuid=VbQqwsQ3qiCw1T04VrrFQzBBlkfZibg3YFkbWz6VquZ&lc=id&ft=1&reqText=Eh+jelek&status=W
How to get value from respSentence
Im using php lang ^_^
" {"status":200,"respSentence" 11 \:"respSentence"}"` is not valid json. Use some online sites to validate your json and try like this,
$json = '{"status":200,"respSentence" :"iyas"}';
$array = json_decode($json, true);
echo $array ['respSentence'];
You can try using file_get_contents and json_decode PHP functions
$url = "http://simsimi.com/........";
$data = file_get_contents($url);
$json = json_decode($data, true);
echo $json ['respSentence'];
hope it helps

Parse Data from JSON URL

I'm trying to get data onto my website by parsing data from a JSON format. The URL is http://api.bfhstats.com/api/onlinePlayers and I'm trying to output for example the currently players online on PC.
Here's the current code I have:
<?$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data = json_decode($jsondata, true);
echo $data->pc->peak24;?>
I thought this would work, however it's not displaying anything. I am very new to parsing JSON data so if someone could explain what I'm doing wrong that would be brilliant.
change:
$data = json_decode($jsondata, true);
to
$data = json_decode($json, true);
Also, json_decode returns an array so use:
echo $data['pc']['peak24'];
to access the data.
You first call the variable $json but then use $jsondata in json_decode.
You are missing the foreach cycle to fetch the two dimensional array $data:
<?php
$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data=array();
$data = json_decode($json, true);
//print_r ($data);
foreach ($data as $pc) {
echo $pc["peak24"]."<br>";
}
?>
Check the $json and $jsondata that have different name but should be the same.

Using php for http requests

I am very new to PHP, and need to use an HTTP Request to download a URL that is external to my server. That URL is a PHP function that returns JSON code which I have decode. Any suggestions?
I have tried basic code:
<?php
//Code for forming the url (which I'm sure is correct)
$url = ...
$response = fopen($url,"x+");
$response = json_decode($response);
echo $response;
?>
//javascript in a seperate file that calls the php code
var response = xmlhttp.responseText;
alert(response);
Try this:
<?php
$url = 'YOUR_URL_HERE';
$data = file_get_contents( $url ); // it is a JSON response as per your statement.
$data= json_decode($data);
print_r($data); // now, it's a normal array.
?>
You may use fopen if config allows, or cURL or fsockopen functions to do that
You could use:
$json_str = file_get_contents($url);
$json = json_decode($json_str, true);
Couldn't you use file_get_contents? E.g.
<?php
$url = "YOUR_URL";
$json = file_get_contents($url);
// handle the data
$data = json_decode($json, TRUE);
var_dump($data); // example
?>
If you are doing a lot of requests you can try using a http class like Buzz which will Help clean up your code https://github.com/kriswallsmith/Buzz

Decoding JSON into an array for a xbox live image script?

I'm trying to turn a JSON file of Xbox Live data into variables that I can use in a PHP generated image. My JSON file is here: http://www.xboxgamercard.org/gamercard/test3/xbox.php
I have tried this:
$request_url = 'xbox.php';
$json = file_get_contents($request_url);
$decode = json_decode($json, true);
var_dump($decode['gamertag'][0]);
but it just returns NULL.
I would like to use the JSON as shown here:
$gamertag = $data['Gamertag'];
echo $gamertag;
You need to add the full url eg:
<?php
$request_url = 'http://www.xboxgamercard.org/gamercard/test3/xbox.php';
$json = file_get_contents($request_url);
$data = json_decode($json, true);
//Example output
echo $data['gamertag']; //Crylics
echo $data['gamerscore']; //7492
/* Too access the recent_games key you will need
to loop through it or access it like
*/
echo $data['recent_games'][1]['title']; //Call of Duty: WaW
?>

Categories