Json deserialization return null - php

I'm trying to deserialize this json.
Actual I stay using the simple html dom library for get the web content, so the next step that I do is using the json_decode() function. But when I'll print the value returned by the function I get NULL. This is the code:
<?php
require_once("simplehtmldom_1_5/simple_html_dom.php");
$html = file_get_html('http://it.soccerway.com/a/block_competition_tables?block_id=page_competition_1_block_competition_tables_8&callback_params=%7B%22season_id%22%3A11663%2C%22round_id%22%3A31554%2C%22outgroup%22%3Afalse%7D&action=changeTable&params=%7B%22type%22%3A%22competition_league_table%22%7D');
$decoded = json_decode($html,true);
var_dump($decoded);
?>
What's wrong in my code? Maybe this isn't the best way for doing this? Hint me.

It seems that your file_get_html function is not working properly, you can get the content of a web with file_get_contents
<?php
$html = file_get_contents('http://it.soccerway.com/a/block_competition_tables?block_id=page_competition_1_block_competition_tables_8&callback_params=%7B%22season_id%22%3A11663%2C%22round_id%22%3A31554%2C%22outgroup%22%3Afalse%7D&action=changeTable&params=%7B%22type%22%3A%22competition_league_table%22%7D');
$decoded = json_decode($html,true);
var_dump($decoded);
?>

Related

PHP function to call JSON files

I am trying to make a basic PHP function in order to call json files repeatedly throughout the app. Every time I want to call a json file I use:
<? $site = json_decode(file_get_contents('views/partials/site.json')); ?>
Then I use echo to use data from json file like this:
<? echo $site[0]->title; ?>
But instead of repeating part one I want to write a function in the header and call it where I want to call a json file. After that i was planning to use the function like this:
$site = jsonCall('site');
by using the function below;
function jsonCall($jsonurl){
// this is one line code. no difference from 3 lines below-> $jsonCalled = json_decode(file_get_contents($homepage . 'views/partials/' . $jsonurl . '.json'));
$url = $homepage . 'views/partials/' . $jsonurl . '.json';
$data = file_get_contents($url); // put the contents of the file into a variable
$jsonCalled = json_decode($data); // decode the JSON feed
echo $jsonCalled;
};
but instead of what i want i got an array as a response from server. i think my function turns json file to an array and that way i can't call it properly.
anyone knows how to solve this simple issue? show me proper way to write this function so my code might look a bit easier to read. Thank you.
by changing echo in function with return and using jsonCall('site')[0]->title; everything worked fine.
Of course you are getting an array. Otherwise $site[0] (which is an array access at key zero) would not have worked.
From the PHP docs (http://php.net/manual/en/function.json-decode.php):
Returns the value encoded in json in appropriate PHP type. Values
true, false and null are returned as TRUE, FALSE and NULL
respectively. NULL is returned if the json cannot be decoded or if the
encoded data is deeper than the recursion limit.
Your appropriate PHP type is array.
The following should work:
jsonCall('site')[0]->title;
Therefore I can not see a problem with your code?
The server is responding with Array because that is how PHP represents an array when you are echo'ing it. Your function should be returning the result.
Try:
function jsonCall($jsonurl){
// this is one line code. no difference from 3 lines below-> $jsonCalled = json_decode(file_get_contents($homepage . 'views/partials/' . $jsonurl . '.json'));
$url = $homepage . 'views/partials/' . $jsonurl . '.json';
$data = file_get_contents($url); // put the contents of the file into a variable
$jsonCalled = json_decode($data); // decode the JSON feed
// echo $jsonCalled;
return $jsonCalled; // <- this should work
};

Httpful JSON inside an array

I've been trying for 3 days to get PHP to show the ID from this test JSON using PHP and httpful.
Anyone have any ideas as i've tried loads of different combinations and even tried created a handler to decode as an array... I just suck at PHP ?
// Make a request to the GitHub API with a custom
// header of "X-Trvial-Header: Just as a demo".
<?php
include('\httpful.phar');
$url = "https://jsonplaceholder.typicode.com/posts";
$response = Httpful\Request::get($url)
->expectsJson()
->send();
echo "{$response[0]['id']}";
?>
My output... still nothing
You are not properly indexing the return value.
The response is a mixed value. So you should do something like below to get what you are looking for.
<?php
include('\httpful.phar');
$url = "https://jsonplaceholder.typicode.com/posts";
$response = Httpful\Request::get($url)
->expectsJson()
->send();
echo $response->body[0]->id;
?>

JSON - PHP - Reading Key

I have the following code below, which I am trying to run to save the key in the json to a PHP varible. However, for some reason it's just not doing anything - although it seems my code is right, any ideas where I am going wrong?
<?php
$json = file_get_contents('url');
$data = json_decode($json,true);
$id = $data['s_id'];
echo($id);
?>
and the json from the external URL looks like this
[{"s_id":"20063"}]
You have an array wrapping the object
$id = $data[0]['s_id'];

Retrieving info from JSON array with PHP

i have this public JSON https://raw.github.com/currencybot/open-exchange-rates/master/latest.json and i need to extract data from it, right now i tried something like this:
$input = file_get_contents("https://raw.github.com/currencybot/open-exchange-rates/master/latest.json");
$json = json_decode($input);
echo $json[rates]->EUR;
but i obtain a blank page, any suggestion? Thanks !! Ste
json_decode either returns an object, or an array (when you use the second param). You are trying to use both.
Try:
$json = json_decode($input);
echo $json->rates->EUR;
OR
$json = json_decode($input, true);
echo $json['rates']['EUR'];
As to the blank page, please add the following to the top of your script:
error_reporting(E_ALL);
init_set('display_errors', true);
A 500 Error indicates you are unable to resolve that url using file_get_contents.
Check here for more information.
Read the help for json_decode ... without the second parameter it returns and object not an array ...
Either :
$json = json_decode($input);
echo $json->rates->EUR;
or
$json = json_decode($input,true);
echo $json['rates']['EUR'];
Try:
$arr = json_decode($input, true);
var_dump($arr);
echo $arr['rates']['EUR'];
Further Reading:
http://de.php.net/manual/de/function.json-encode.php
For anexample with cURL and SSL read this:
http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
The link looks like starting with https, while file_get_contents can't deal with SSL. My suggestion is using curl.

How to transfer json data to html with php?

How to transfer json data to html with php?
$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey"
when I type the url in browser, it return
{"offset" : "0" , "results" : [{"body" : "A guide to cultural and recreational goings-on in and around the Hudson Valley. ...}]}
how to put the json body data into html? I mean like this echo '<div class="body"></div>';
You first need to get the file. You should use curl for this. In the example below I used the file_get_contents() function, you might want to replace it. Use json_decode() to parse the JSON for you.
$json = file_get_contents($url);
$data = json_decode($json);
echo $data->results[0]->body;
This will echo A guide to cultural....
Use json_decode() on the content of the file, which you can retrieve with file_get_contents($url), then you have an array you can use to build the HTML.
$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey";
$dataRaw = file_get_contents($url);
if ($dataRaw) {
$data = json_decode($dataRaw, true);
foreach ($data['results'] as $cEntry) {
?>
<div class="body">
<?php echo $cEntry['body']; ?>
</div>
<?php
}
}
I'm not sure why you would, but assuming fopen() URL opening is enabled, you could do...
echo file_get_contents($url);
like this ?
<?php
$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey";
$json = file_get_contents($url);
echo $json;
Once you've loaded a JSON string into PHP, you can then convert it into a PHP array by using the function json_decode(). You can then print the appropriate array element into your HTML output as with any other PHP variable.
Try this:
$jsonDecoded = json_decode($yourJsonEncodedData);
echo $jsonDecoded->results->body;

Categories