Httpful JSON inside an array - php

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

Related

How get data from url to array?

I use php pretty badly. I'm not a programmer just doing something for my private things.
I have such a problem I would like to download data from my PV production. The data is in the form as below. How to download data from a url in php, respectively, to group them and send them to the array ??
Thank you in advance for all your help.
{"sid":62923,"dataunit":"kWh","data":[{"time":"2019-08-01","no":"1","value":"27.7"},{"time":"2019-08-02","no":"2","value":"24.0"},{"time":"2019-08-03","no":"3","value":"19.9"},{"time":"2019-08-04","no":"4","value":"25.3"},{"time":"2019-08-05","no":"5","value":"0.0"},{"time":"2019-08-06","no":"6","value":"0.0"},{"time":"2019-08-07","no":"7","value":"0.0"},{"time":"2019-08-08","no":"8","value":"0.0"},{"time":"2019-08-09","no":"9","value":"0.0"},{"time":"2019-08-10","no":"10","value":"0.0"},{"time":"2019-08-11","no":"11","value":"0.0"},{"time":"2019-08-12","no":"12","value":"0.0"},{"time":"2019-08-13","no":"13","value":"0.0"},{"time":"2019-08-14","no":"14","value":"0.0"},{"time":"2019-08-15","no":"15","value":"0.0"},{"time":"2019-08-16","no":"16","value":"0.0"},{"time":"2019-08-17","no":"17","value":"0.0"},{"time":"2019-08-18","no":"18","value":"0.0"},{"time":"2019-08-19","no":"19","value":"0.0"},{"time":"2019-08-20","no":"20","value":"0.0"},{"time":"2019-08-21","no":"21","value":"0.0"},{"time":"2019-08-22","no":"22","value":"0.0"},{"time":"2019-08-23","no":"23","value":"0.0"},{"time":"2019-08-24","no":"24","value":"0.0"},{"time":"2019-08-25","no":"25","value":"0.0"},{"time":"2019-08-26","no":"26","value":"0.0"},{"time":"2019-08-27","no":"27","value":"0.0"},{"time":"2019-08-28","no":"28","value":"0.0"},{"time":"2019-08-29","no":"29","value":"0.0"},{"time":"2019-08-30","no":"30","value":"0.0"},{"time":"2019-08-31","no":"31","value":"0.0"}]}
Try this
<?php
$json_url = file_get_contents('Your url goes here');
$data = json_decode($json_url,true);
var_dump($data);
// You can print all of your data here to see if it works
foreach ($data as $items) {
// You can loop trough the data and get it like $items->sid etc
var_dump($items);
}
?>
You need to grab the URL with 'file_get_contents' and than you need to decode it with json_decode
and then you print it out with the for each function

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

Get a JSON from url with PHP

Im new to php and tried to get a json object from the twitch API to retrieve one of its values and output it. i.e
i need to get the information from this link: https://api.twitch.tv/kraken/users/USERNAME/follows/channels/CHANNELSNAME
plus i need to to something so i can modify the urls USERNAME and CHANNELSUSERNAME. I want it to be a api to call for howlong user XY is following channelXY and this will be called using nightbots $customapi function.
the date i need from the json is "created_at"
Since we were able to clear out the errorsheres the final PHP file that works if anyone encounters similiar errors:
<?php
$url = "https://api.twitch.tv/kraken/users/" . $_GET['username'] . "/follows/channels/" . $_GET['channel'];
$result = file_get_contents($url);
$result = json_decode($result, true);
echo $result["created_at"];
?>
You have a typo in your code on the first line and you're not storing the result of your json_decode anywhere.
<?php
$url = "https://api.twitch.tv/kraken/users/" . $_GET['username'] . "/follows/channels/" . $_GET['channel'];
$result = file_get_contents($url);
$result = json_decode($result, true);
echo $result["created_at"];
You have to call the page this way page.php?username=yeroise&channel=ceratia in order to output the created_at value for this user and this channel.
In your code you're using 2 different ways to get the content of the page and you only need one (either file_get_contents or using CURL), I chose file_get_contents here as the other method adds complexity for no reason in this case.

Json deserialization return null

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

Instagram API getting JSON data with PHP

Ok so I am pretty new to APIs, I've been diving in brute-force breaking things.
I am trying to get hashtag stats into a php variable I can manipulate in JSON format.
$key = "SOMEKEYVALUEIDIDNOTINCLUDEHERE" ;
$hashtag = "als" ;
$url = 'https://api.instagram.com/v1/tags/'.$hashtag.'?access_token='.$key ;
$content = file_get_contents(url);
$data = json_decode($content) ;
echo $data->{'meta'} ;
Here is the JSON data that pops up in the browser when I type the url in manually:
{"meta":{"code":200},"data":{"media_count":1237544,"name":"als"}}
The echo I included is the the only attempt I made at displaying the "What I thought would be returned JSON Code". The echo is always empty.
How can I go about solving this? I've heard of cURL and read about it, but don't have a really good grasp of what it is.
Thanks!
You should access your data like this:
echo $data->meta->code // 200
echo $data->data->meta_count // 1237544
echo $data->data->name // als

Categories