Retrieve JSON Object from REST Blogger Api Request with PHP - php

I'm still wondering how to get json object from REST Api request from this link https://developers.google.com/blogger/docs/3.0/using.
It says GET https://www.googleapis.com/blogger/v3/blogs/2399953?key=YOUR-API-KEY request will give informations about particular blog.
I'm also PHP noobs. Here's my code.
<?php
$blogID = 'xxx';
$apikey = 'yyy';
$requestURL = "https://www.googleapis.com/blogger/v3/blogs/{$blogID}?key={$apikey}";
$json = file_get_contents($requestURL);
print_r($json);
?>
I have no idea about this, so please help me to find where the problem is. Thanks in advance

Try this:
$json = json_decode(file_get_contents($requestURL));
You are fetching a JSON string and trying to print_r it, which only works on arrays.
json_decode, as its name implies, decodes the JSON string and transforms it into an array / object.

Related

How to read JSON file with PHP from instagram __a=1 page

I’m trying to call a URL and read its content using php.
when I place the url directly in the browser It works, but when I use php it returns empty string.
here’s my code:
$url = "https://www.instagram.com/instagram/?__a=1"
$json = file_get_contents($url);
$res= json_decode($json);
var_dump($res);
Does anyone have an idea why I can’t read the file with PHP?
Thank you,

How to run (api-request) a static URL in PHP and fetch response?

I have a static URL for a GET request, which is like - api.airtable.com/v0/<my-id>/VID?api_key=<api-key>. When I run this URL in chrome, I see a JSON response. But I want to fetch the response in PHP and work with it. How do I just run this simple api-request and get the JSON response in PHP?
I am a beginner to PHP, so this question might sound very basic to you :) Your help/advice is very appreciated - Thanks!
You can use the json_decode() function to convert your JSON into an array.
See documentation here: https://www.php.net/manual/en/function.json-decode.php
An example would be:
<?php
$json_data = file_get_contents("https://your-url-here");
$response_data = json_decode($json_data, true);
?>
Now your $response_data variable is an array, with which you can work normally.
Don't hesitate to ask if you need more help. :)
If JSON response is in array then you can use escapeJsonString() and json_decode() functions as below.
$response = escapeJsonString($response);
$response = json_decode($response,true);
print_r($response);

Problem decoding a JSON resopnse from an API service

i have a problem dedoding a JSON response from a webservice using Guzzle 6 under Laravel 6.
The response i'm trying to decode is here:
https://json.volotea.com/dist/stations/stations.json
In my php code i try in this way:
$client = new Client();
$response = $client->request('GET','https://json.volotea.com/dist/stations/stations.json');
To decode the response i tried several methods like:
$response->getBody(); // that returns a stream
$response->getBody()->getContents(); that returns an apparently correct answer but if I run the json_decode it returns a null as a result
I'm going crazy!
How can i decode the response from this service?
many thanks
it seems with method getContent you already manipulated Json structure
try json_decode on $response.

Overwriting Json File with new Json Data

So I have this json file (json.json) stored on a server, I would like to overwrite it with new json Data ('refresh_token' object to be exact) everytime I call on this php class:
$file = file_get_contents('/var/json.json');
$json = json_decode($file);
**$REFRESH_TOKEN** = $json->refresh_token;
$json = file_get_contents("https://xxxxxxxxxxx&refresh_token=**$REFRESH_TOKEN**");
$obj = json_decode($json, true);
$AccessToken = $obj->access_token;
$RefreshToken = $obj->refresh_token;
**file_put_contents('/var/json.json', json_encode($RefreshToken));**
The json file looks like this:
{"token_type":"bearer",
"expires_in":3600,
"scope":"xxxxxxx",
"access_token":"xxxx",
**"refresh_token":"xxxxx",**
"user_id":"xxxxxx"}
I repeatedly get the error: Trying to get property of non-object in whenever I call the class for a second time. I looked inside the json file after the first call and realized that it did not contain anything. This indicates that the file_put_contents('/var/json.json', json_encode($RefreshToken)) did not work.
Can anyone give me a hint as to what i'm doing wrong here?
Make sure you have permission to write on the file. Change file permission to 777.
From your code, it is not obvious what you are trying to do. I guess it is the following:
fetch json object from file
modify refresh token
write json object back to file
This would work as follows:
$REFRESHTOKEN = $_GET['REFRESH_TOKEN']; // Calculate refresh token
$json = json_decode(file_get_contents('/var/json.json')); // fetch json object from file
$json->refresh_token = $REFRESHTOKEN // modify refresh token
file_put_contents('/var/json.json', json_encode($json)); // write json object back to file
What you are doing is writing ONLY the refresh token into the file, not the whole object. This is why on the second attempt, the object is not there...
If you set the 2nd parameter of json_decode to true you get an associative array instead of an object.
Documentation for json_decode: http://php.net//manual/en/function.json-decode.php

Unable to get data Posted by webhook in JSON String

I am using web hook for get user details from our company's signup page.
Data posted from signup page in JSON format. Posted JSON String is below
{"FirstName":"dharampal","EmailAddress":"er.dpyadav#gmail.com","Mobile":"123456789","Company":"Instasafe","LandingPageURL":"http://instasafe.com/SignUp","LandingPageId":"11e2-b071-123141050daa"}
In receiving end, we are using PHP codeIgNiter, I tried below methods but unable to get data.
$postedData = $_POST;
print_r($postedData);
it gives : empty array
other method i tried is :
$postedData = json_decode(file_get_contents('php://input'), TRUE);
print_r($postedData);
it gives : NULL
Seems like the problem is a typo with $ostedData and $postedData
$ostedData = json_decode(file_get_contents('php://input'), TRUE);
print_r($postedData);

Categories