1

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>
Termininja
  • 6,620
  • 12
  • 48
  • 49
Oroku
  • 443
  • 1
  • 3
  • 15

1 Answers1

1

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>");
}
Dennis Weidmann
  • 1,942
  • 1
  • 14
  • 16
  • thanks however I'm getting an error with the json_decode function as $doc is an object and not a string. I'm not sure how to proceed? – Oroku Dec 15 '14 at 08:52
  • well could you provide me which kind of object $doc is? maybe it is already an php array, so you don't need to json_decode it... could you echo it to your site and post it here? – Dennis Weidmann Dec 15 '14 at 14:31
  • Hi @Neo I've made some progress with this by drilling down into the object and get a string ($jsonArr) and what I'm really interested in as mentioned is getting the values id, url, aliases, description and label. However specifically I'd like to get the first instance in the string of each as there are several instances. I'm going to amend my code so that you can see what I can currently get out. Any help is greatly appreciated – Oroku Dec 16 '14 at 08:18
  • Hi @Oroku Sorry I was confused of your code... Above in my edit you find working code, that is much easier to read and maintain I think... Have fun with it and leave me an upvote :-) – Dennis Weidmann Dec 16 '14 at 21:36
  • thank you very much! That's exactly what I needed you've helped me out big time. I really appreciate it! – Oroku Dec 17 '14 at 10:46