Accessing data from Wikidata JSON file

1.3k views Asked by At

I'm trying to access the following properties from the Wikidata API: id, url, aliases, description and label but so far have been unsuccessful. I'm sure I'm making basic mistakes and so far only have the following code. Any suggestions as to the best way to access this data is much appreciated.

<html>
<body>
<form method="post">
Search: <input type="text" name="q" value="Google"/>
<input type="submit" value="Submit">
</form>

<?php

if (isset($_POST['q'])) {
$search = $_POST['q']; 
$errors = libxml_use_internal_errors(true);    
$doc = new DOMDocument();    
$doc->loadHTMLFile("https://www.wikidata.org/w/api.php? 
action=wbsearchentities&search=Google&format=json&language=en");    
libxml_clear_errors();
libxml_use_internal_errors($errors);   
}
?>
</body>
</html>

Edit - I have managed to get a string ($jsonArr) containing particular data that I would like. However I would like to get the first instance of the particular elements from the string specifically id, url, alias, description and label i.e. specifically: variable1 - Q95, variable2 - //www.wikidata.org/wiki/Q95, variable3 - Google.Inc, varialbe4 - American multinational Internet and technology corporation, variable5 - Google/ Please see code below:

<HTML>
<body>
<form method="post">
Search: <input type="text" name="q" value="Google"/>
<input type="submit" value="Submit">
</form>

<?php
if (isset($_POST['q'])) {
$search = $_POST['q']; 
$errors = libxml_use_internal_errors(true);    
$doc = new DOMDocument();     
$doc->loadHTMLFile("https://www.wikidata.org/w/api.php?
action=wbsearchentities&search=$search&format=json&language=en");    
libxml_clear_errors();
libxml_use_internal_errors($errors); 

var_dump($doc);
echo "<p>";
$jsonArr = $doc->documentElement->nodeValue;

$jsonArr = (string)$jsonArr;
echo $jsonArr; 

}
?>
</body>
</HTML>
1

There are 1 answers

5
Dennis Weidmann On BEST ANSWER

You need to show the JSON you want to parse...

Basicly you can get values out of JSON in PHP like this...

If $doc is the JSON you want to parse

$jsonArr = json_decode($doc, true);
$myValue = $jsonArr["keyYouWant"];

Edit after understanding what you are doing there :-)

Hi, Im sorry I was completely confused of what you were doing there... So I have testet your code on my server and just get what you want...

Following the code you wanted... I took clear var names, so they are a little bit longer, but easier to understand...

$searchString = urlencode($_POST['q']); //Encode your Searchstring for url
$resultJSONString = file_get_contents("https://www.wikidata.org/w/api.php?action=wbsearchentities&search=".$searchString."&format=json&language=en"); //Get your Data from wiki
$resultArrayWithHeader = json_decode($resultJSONString, true); //Make an associative Array from respondet JSON
$resultArrayClean = $resultArrayWithHeader["search"]; //Get the search Array and ignore the header part

for ($i = 0; $i < count($resultArrayClean); $i++) { //Loop through the search results
    echo("<b>Entry: ".$i."</b>");
    echo("<br>");
    echo($resultArrayClean[$i]["id"]); //Search results value of key 'id' at position n
    echo("<br>");
    echo($resultArrayClean[$i]["url"]); //Search results value of key 'url' at position n
    echo("<br>");
    echo($resultArrayClean[$i]["aliases"]); //Search results value of key 'aliases' at position n
    echo("<br>");
    echo("<br>");
    echo("<br>");
}