I apologize in advance if there was already a similar question. I tried to find a solution but unfortunately I still can't.
I have a JSON response from influxdb
$output2 = '{"results": [{"statement_id": 0,"series": [{"name": "dbinfluxdb","columns": ["time","count"],"values": [["2020-07-02T00:00:00Z",1],["2020-07-03T00:00:00Z",0],["2020-07-04T00:00:00Z",0],["2020-07-05T00:00:00Z",0],["2020-07-06T00:00:00Z",1],["2020-07-07T00:00:00Z",0]]}]}]}';
and I need to print out the name of columns time and count and values to be able generate a new JSON for Google chart.
What am I able to do now is only get a statement_id=0.
$decode = json_decode($output2,true);
foreach($decode['results'] as $val){
//Actual statement_id is 0
echo "statement_id is ";
echo $val['statement_id'];
echo "<br/>";
}
But I need to print:
time, count<br/>
2020-07-02T00:00:00Z,1<br/>
2020-07-03T00:00:00Z,0<br/>
2020-07-04T00:00:00Z,0<br/>
2020-07-05T00:00:00Z,0<br/>
2020-07-06T00:00:00Z,1<br/>
or put it into the variable to be able to work with it. I already tried without converting into array just with
$decode = json_decode($output2); //without ",true"
Can you please help me how to solve it.
I've spent almost the whole day looking for a solution and I can't find a way to solve the problem.
If you need to print it out on html, just change the \n to <br>
<?php
$output2 = '{"results": [{"statement_id": 0,"series": [{"name": "dbinfluxdb","columns": ["time","count"],"values": [["2020-07-02T00:00:00Z",1],["2020-07-03T00:00:00Z",0],["2020-07-04T00:00:00Z",0],["2020-07-05T00:00:00Z",0],["2020-07-06T00:00:00Z",1],["2020-07-07T00:00:00Z",0]]}]}]}';
$decode = json_decode($output2,true);
echo $decode["results"][0]["series"][0]["columns"][0].",".$decode["results"][0]["series"][0]["columns"][1]."\n";
foreach($decode["results"][0]["series"][0]["values"] AS $el)
{
echo $el[0].",".$el[1];
echo "\n";
}
?>
If i understand you write this would be the code to output the JSON for the Google Chart:
<?php
$output2 = '{"results": [{"statement_id": 0,"series": [{"name": "dbinfluxdb","columns": ["time","count"],"values": [["2020-07-02T00:00:00Z",1],["2020-07-03T00:00:00Z",0],["2020-07-04T00:00:00Z",0],["2020-07-05T00:00:00Z",0],["2020-07-06T00:00:00Z",1],["2020-07-07T00:00:00Z",0]]}]}]}';
$decode = json_decode($output2,true);
$googlechart = array();
$googlechart[] = array($decode["results"][0]["series"][0]["columns"][0],$decode["results"][0]["series"][0]["columns"][1]);
foreach($decode["results"][0]["series"][0]["values"] AS $el)
{
$googlechart[] = array($el[0],$el[1]);
}
echo json_encode($googlechart);
?>
I've been recently building a plugin for wordpress which basically uses the instagram API to get an image URL and then place it in a short code.
And I've come to a problem.
I get this error:
E_WARNING : type 2 -- Invalid argument supplied for foreach() -- at
line 22
and I have no idea what am I doing wrong.
My code for the foreach:
//define Access token
$accesst= "ACCESS_TOKEN_GOES_HERE";
//userid
$userid=USERID_GOES_HERE;
//image count to get
$count=20;
//get api contents
$content = file_get_contents('https://api.instagram.com/v1/users/self/media/recent/?access_token='.$accesst.'&count='.$count);
//converting JSON to object
$standardres = json_decode($content, true);
//array method
foreach($standardres['data'][0]['images']['standard_resolution']['url'] as $photo)
{
print $photo['url'][0];
echo "<br>";
}
My JSON var_dump got me this:
https://pastebin.com/3RaL6EUA
The access codes, were of course deleted before posting this.
Does anyone have a clue what I'm doing wrong?
EDIT:
Thanks, everyone, got it figured out in the comments.
Your $standardres['data'] have items which have images, so you must use $standardres['data'] in the foreach loop and then parse the image url from the item data.
foreach($standardres['data'] as $item) {
print $item['images']['standard_resolution']['url'];
echo "<br>";
}
I don't know exactly the hierarchy of the instagram API, but I suggest you to try :
foreach($standardres['data']['images'] as $photo) {
print_r($photo); // to see what the array contains!
echo $photo['standard_resolution']['url'];
echo "<br>";
}
I have a working api call to get basic user info and I am trying to return just the "followed_by" portion of the json data from "counts".
This link should return just the json page on your browser
https://api.instagram.com/v1/users/self/?access_token=3514554632.a691e29.3f773f5f335a4ba98fc9609d1d405fb0
And here is my code to try and parse the result, but all I am getting is a blank screen.
$jsondata = file_get_contents("https://api.instagram.com/v1/users/self/?access_token=3514554632.a691e29.3f773f5f335a4ba98fc9609d1d405fb0");
$json = json_decode($jsondata, true);
$json2 = json_decode($jsondata);
//method one to parse, not working
$parsedvalue = $json['counts'][1]['followed_by'];
echo $parsedvalue;
//method two to parse, not working
$followercount = $json2->counts->followed_by;
echo $followercount;
Here is the fix I got figured out for //method 2
$jsondata = file_get_contents("https://api.instagram.com/v1/users/self/?access_token=3514554632.a691e29.3f773f5f335a4ba98fc9609d1d405fb0");
$json2 = json_decode($jsondata);
$followercount = $json2->data->counts->followed_by;
This properly returns your follower count, but I still need help understanding how to fix method one
I am saving my images directly in the database (Yes I have read about that and know it's better to save the path to the image).
The problem is selecting the image field which is a blob returns NULLto my JSON. I have alot read quite a few stackoverflow links of the same query but still not a clear explanation/ answer received has been able to help me.
I have encoded using base_64 but when doing so nothing is showed on the webpage.
Would appreciate if I could be pointed in the correct direction.
This is part of my code:
header("Content-Type:application/json");
//select query
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
if (!empty($rows))
{
$encode = array("StudentsList" => $rows);
//$json_response=json_encode($encode);
$json_response=base64_encode($encode);
echo $json_response;
echo json_last_error(); //Returns 0
}
if you need the json data
please use json_encode
example
echo json_encode($json_response);
and also read php manual
http://php.net/manual/en/function.json-encode.php
I'm trying to create a very simple php script that can pull data from a JSON file (array?) and echo it to the page. Sadly, I'm a complete newbie when it comes to PHP.
My goal is to dump all IP addresses and the corresponding client version into an output like this...
"127.0.0.1" "/Satoshi:0.9.1/"
"127.0.0.2" "/Satoshi:0.9.0/"
"127.0.0.3" "/Satoshi:0.9.0/"
"127.0.0.4" "/Satoshi:0.9.1/"
I can get the code to dump all data, but I'm not sure how to pull the ip and version without the ip and client version being named.. If that even makes sense?
Here is the code. What do I need to make it dump the correct data?
<?php
$url = 'https://getaddr.bitnodes.io/nodes/1407675714.json';
$JSON = file_get_contents($url);
echo $JSON;
$data = json_decode($JSON);
var_dump($data);
?>
Thanks for your help!
Something like
<?php
$url = 'https://getaddr.bitnodes.io/nodes/1407675714.json';
$JSON = file_get_contents($url);
$data = json_decode($JSON);
$data = (array)$data; // cast from stdClass to array, as results have int keys
foreach($data['nodes'] as $results){
echo $results[0]. " ". $results[3]."<br />";
}
?>
It doesn't makes much sense to convert an array of hashes into the one of some non-named entities. I guess that all you need is to dump it in the following way:
foreach($data as $t)
{
echo("ip=".$t->ip." ua=".$t->ua);
}
I don't think there is any reason to cast to an array. Just process the returned object. This one has the double quotes as requested as well.
<?php
$url = 'https://getaddr.bitnodes.io/nodes/1407675714.json';
$json = file_get_contents($url);
$data = json_decode($json);
foreach($data->nodes as $results){
echo "\"".$results[0]."\" \"".$results[3]."\"<br />";
}
?>