json_decode help [duplicate] - php

This question already has answers here:
Get information from JSON
(2 answers)
Closed 11 months ago.
I have been trying to use some API's from various services, At the moment im trying to retrieve data about a video on vimeo,
I have successfully got the file and read the contents, however, i do not know how to access each part of the file that is returned as a .json.
Basically, how do i access the data in the json file using PHP
Thanks

$file_contents = file_get_contents("http://vimeo.com/api/v2/group/awesome/videos.json"), true);
Read it into a variable using:
$data = json_decode($file_contents);
then you can access the parts using:
echo $data->id;
echo $data->title;
etc.
Just use print_r($data); to see all available fields.

Did you try the json_decode function?

If the file is just JSON, you can use json_decode:
$data = json_decode(file_get_contents("url here"), true);
and then access the data like a normal array. Of course you have to know the structure of the data you get in order to access it correctly. You can always use loops to iterate over it as well.
If the data is delivered as JSONP, you have to process it before, to remove the padding. I created a function for that.
Update:
In one of the comments you posted a link to the JSON file:
$data->video
does not work because
$data is an array. $data[0]->something will work
video does not exist as property of the object.
Do a print_r($data) to see the structure.

Related

Parsing Wikipedia API with PHP

I have a PHP script that retrieves the JSON result from the Wikipedia API and stores it in $json variable, then I json decode it into $data:
<?php
$q = htmlspecialchars(($_GET["q"]));
$url = 'https://en.wikipedia.org/w/api.php?action=query&list=search&srnamespace=0&srprop=snippet&format=json&callback=json&origin=*&prop=links|extracts|categories|images&srsearch=test';
$json = file_get_contents($url);
/*
print "<pre>";print_r($json);print "</pre>";
*/
$data = json_decode($json,true);
echo $data['query']['search'][0]['title'];
This retrieves the JSON file, but I am not able to work with it. I need to extract the Title tag and echoing it like this doesn't do anything.
echo $data['query']['search'][0]['title'];
Any idea how to correct my code to retrieve the following title tag:
Remove &callback=json from your URL. That's making a request for JSONP (ironic link to wikipedia). It wraps the response with a JavaScript callback function, so instead of just JSON you need in PHP, you're getting
/**/json(THE JSON HERE)
You can see it in the page source, even if it displays as JSON on the page. Those extra characters are making json_decode fail. That parameter is intended more for cross-domain requests from JS.
It looks like you're already accessing the resulting array properly with
echo $data['query']['search'][0]['title'];
You might think it would give you some kind of warning or notice when you try to access those array keys when $data is null, but it won't.

Fetch JSON object with API link by PHP

I'm struggling to get JSON object with API link by PHP. (http://hots.adspreemedia.com/api/characters). This link should allow me to fetch a list of characters as JSON object.
What I tried was the following code.
<?php
$list = file_get_contents('http://hots.adspreemedia.com/api/characters');
echo $list;
?>
But it just shows only {"status":"200","message":"OK","data":[1,2,3,4]} in my localhost. My final goal is to create a list of characters in PHP file.
Follow the link you posted. it matches exactly what you are getting back.
To get more data, you will need to follow their api docs for retrieving more info from them
Is this a site you are building? there is no sigup form, and no documentation I can find without logging in to help you with
If you want to fetch [1,2,3,4] then do following
$list=json_decode($list,true);
$chars=$list['data']; //-- The array of characters

Take variables from a PHP script

I am making a website to display the data at https://api.captcoin.com/address/top/100. I need to be able to make the website take variables("address", "percent", "balance", and "rank") from this script and make them local variables in my site so I can display them. How can I take these variables and use them in my site?
First you need to get the remote page contents:
$remote = file_get_contents('link');
Then, since the data is in json format, you need to decode it using json_decode function.
$data = json_decode($remote, true);
true means that $remote should be decoded as associative array.
And finally you can access the data like an ordinary php array:
echo $data['top'][0]['address'];
Also, you should add some logic to handle situations when remote server is not accessible.
Use json_decode to convert the content of that url into an array and then search through it like you would through any array.
To get the actual content of the site please refer to this post Get file content from a URL?
You can either do it with javascript or php.
With javascript use this:
http://api.jquery.com/jquery.getjson/
You take the pages output and push them as variables to the php.
With php you can use
http://php.net/manual/en/function.json-decode.php
You make an array to push the json data into:
$object = array();
$json = file_get_contents('https://api.captcoin.com/address/top/100');
$object = json_decode ( string $json , true)
Be aware this is untested and read the json_decode api to customize the function-call.

Traverse array structure with string

Can I store a pre-made array traversal?
I want to store several API calls, and also how I get to the relevant information from their response.
For example:
$url = 'http://maps.googleapis.com/maps/api/elevation/json?locations='.$location->$latitude.','.$location->$longitude.'&sensor=true';
$response = json_decode(file_get_contents($url), true);
$result = $response['results'][0]['elevation'];
Can I save this part as a string, for storage in my DB or a variable:
$elevation = "['results'][0]['elevation']";
Then later somehow use it to parse the response, ie.
$result = $response[$elevation];
The answer is no, sorry ! you will need to store your $response as it is and call it later on using the correct format $response['results'][0]['elevation']
You may however want to use serialize() if the problem is about how to persist the array into your database:
$db->insert(serialize($reponse));
then when you retrieve the response from your db use unserialize:
$response=unserialize($db->fetchReponse());
$elevation=$response['results'][0]['elevation'];
EDIT
Based on your comment below it seems what you need is a Cache. Whereby prior to sending the request to the web service API, your application checks in a cache to see if you already have the data available locally. As above example you would most likely want to serialize the PHP array or simply cache the raw response, given that it is in JSON format (PHP serialization will create something very similar anyway).
You would create the Cache key from the query params : location, etc.
Your cached object can be stored in a DB if you choose or on the file system, or even in Memory.
Check out ZF2 Cache component :
http://framework.zend.com/manual/2.0/en/modules/zend.cache.storage.adapter.html

virustotal.com API example in PHP?

Does anybody have a PHP example of using the VirusTotal.com public API for URL scanning?
The API is available here:
http://www.virustotal.com/advanced.html
There's a Python/JSON example, but I'm not experienced with either :(
All you need is to retreive the report json using file_get_contents this way :
$json = file_get_contents('https://www.virustotal.com/api/get_url_report.json');
Then use json_decode to convert your json into a php array:
$array = json_decode($json);
to see results :
var_dump($array);
to post data use curl, related question.
A PHP example is available on that page (maybe it's new).

Categories