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.
Related
I am trying to use echo in php for populating data twice as per user different action using ajax.
But it seems that I am unable to use echo twice to populate data from php server to my html page.
here is the code for php:
<?php
// Receive the Data from Client
$data = $_REQUEST;
$fileName = $data['fileList'];
$files = glob( "*.txt");
//it will pass data while the html page load (and working fine)
echo json_encode($files);
//set the file path for text files
$file_Content = file_get_contents("temp.text");
//this will pass while user click a button, but while using it is not working, even the first echo also
echo json_encode($file_Content);
?>
Is there any way to do it? or any suggestion pleas..Thanks
To form and respond with a valid json object, you can only call json_encode once. You're currently responding with two encoded json objects, which is invalid, so I'm guessing only the first one is being interpreted.
Remove the first json_encode echo, and encode an array of both properties in your second (now only) echo. I've taken the liberty to guess at what might be sensical properties for your response object here:
echo json_encode(['files' => $files, 'content' => $file_Content]);
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.
I want to send data to PHP API that reads data like this:
$id = $_POST['id'];
$data = $_POST['data'];
$name = $data['name'];
$foo = $data['sub']['foo'];
to send the ID, the query would look like id=123
but I'm not sure how the data parameter should be encoded - is it simply URL encoding of id=123&data['name']=name -> id=123&data%5B%27name%27%5D=name and similarly for the deeper nested array?
(I'm doing the request from Android, but I'm interested in the body of the POST request)
Yes you can definitely do that by making sure the parameter are being encoded properly
For example if you encode the following
ser[name]=Bob Smith&user[age]=47&user[sex]=M&
user[dob]=5/12/1956&pastimes[0]=golf&pastimes[1]=opera&
pastimes[2]=poker&pastimes[3]=rap&children[bobby][age]=12&
children[bobby][sex]=M&children[sally][age]=8&
children[sally][sex]=F&flags_0=CEO
Go to this site and paste either your code or the code that I provided
http://meyerweb.com/eric/tools/dencoder/ then click encode
and pass it to your URL as parameters.
If you need to generate those via PHP, maybe you should take a look at this
http://php.net/manual/en/function.http-build-query.php
Just use JSON. You send JSON from your app and do a simple json_decode in the Server.
In your case where you have to follow an API use this format:
data[name]=User&data[sub][foo]=Hithere
Source:
http://homakov.blogspot.de/2012/06/x-www-form-urlencoded-vs-json-pros-and.html?m=1
I have lots of facebook pages (we're a club) and would like to display them in order of their fan count. I'm new to php and know nothing about API. All the code I can find to return the fan count refers to apps, and needs their IDs and secrets, which I don't have with a business page.
I have this:
http://api.facebook.com/method/fql.query?format=json&query=select+fan_count+from+page+where+page_id%3D355692061120689
When I put this into a browser it gives me what I need. How do I write this in php so it gives me a variable to work with?
Thanks.
This data is called a JSON string. There is a function in php called json_decode(). Using this in tandem with file_get_contents(), we can retrieve the value, and echo it in php:
<?php
$json = file_get_contents('http://api.facebook.com/method/fql.query?format=json&query=select+fan_count+from+page+where+page_id%3D355692061120689');
$decode = json_decode($json);
echo $decode[0]->fan_count;
?>
To be clear, $decode is an array, whose first value is a php object, whose variable fan_count holds the data.
In regards to the graph url, just change..
echo $decode[0]->fan_count;
to..
echo $decode->likes;
of course assuming you've changed the file_get_contents() url to that of your graph url.
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.