0

I'm messing with the GND API which uses JSON which seems to be making it a bit more difficult to extract specific data (at least for me).

I try to get single values from the GND Website. I already have code to receive the whole data entry.

<?php

            // Setting the HTTP Request Headers
                $User_Agent = 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0';

                $request_headers = array();
                $request_headers[] = 'Accept: application/json';
            $request_headers[] = 'Content-Length: ' . strlen($data_string);


            //PHP Infos anzeigen lassen
            //phpinfo();

        $id = $_POST["id"];             
                // URL to fetch
                $url = "http://hub.culturegraph.org/entityfacts/$id";
                $curl = curl_init($url);
                curl_setopt($curl, CURLOPT_URL, $url);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_HEADER, false);
                $json = curl_exec($curl);

                $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
                curl_close($curl);


                // Performing the HTTP request
                //  Initiate curl
              $ch = curl_init($url);
              // Set the url
                curl_setopt($curl, CURLOPT_URL, $url);
                // Will return the response, if false it print the response
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
                curl_setopt($curl, CURLOPT_POST, true);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
                // Execute
                $result = curl_exec($ch); // Performs the Request, with specified curl_setopt() options (if any).
                // Closing
        curl_close($ch);    

        curl_error
?>

But i just want specific values like "@type "person" variantName from the data entry. A part of the Json-Code looks like this:

@context    "http://hub.culturegraph.org/entityfacts/context/v1/entityfacts.jsonld"
@id "http://d-nb.info/gnd/118640445"
preferredName   "Leonardo, da Vinci"
surname "Leonardo, da Vinci"
describedBy 
valid   "2018-09-14T10:46:49+0200"
license "https://creativecommons.org/publicdomain/zero/1.0/"
@id "http://hub.culturegraph.org/entityfacts/118640445"
@type   "person"
variantName 
0   "Leonard, de Vinci"
1   "Léonard, de Vinci"
2   "Léonard"
3   "Leonardus, Vincius"
4   "Lionardo, da Vinci"
5   "Lionardo"
6   "Leonardo"
7   "Leonardo da Vinči"
8   "Leonardo de Vinci"
9   "Leonard da Vinci"
10  "Léonard de Vinci"
11  "Leonhard von Vinci"
12  "Lionardo da Vinci"
13  "Lionardo de Vinci"
14  "Leonard, da Vinci"
15  "Reonarŭdo, ta-Pinch'i-ŭi"
16  "Leonardo, da Vinči"
17  "Leonardo, de Vinci"
18  "Leonarnto, sta Bintsi"
19  "Leonardo, da Vinchi"
20  "Leonaďo, da Vinchi"
21  "Leonardo da Vinci"
22  "Leonardo Da Vinci"
23  "Leonardo da Vinci, Maler, Architekt, Naturforscher"
24  "Leonardo da Vinci, Peintre, Naturaliste, Architecte"
25  "Leonardo da Vinci, Painter, Architect, Naturalist"
26  "Léonard, de Vinci, Maler, Architekt, Naturforscher"
27  "Léonard, de Vinci, Peintre, Naturaliste, Architecte"
28  "Léonard, de Vinci, Painter, Architect, Naturalist"
29  "Léonard de Vinci, Maler, Architekt, Naturforscher"
30  "Léonard de Vinci, Peintre, Naturaliste, Architecte"
31  "Léonard de Vinci, Painter, Architect, Naturalist"
32  "Lionardo da Vinci, Maler, Architekt, Naturforscher"
33  "Lionardo da Vinci, Peintre, Naturaliste, Architecte"
34  "Lionardo da Vinci, Painter, Architect, Naturalist"
35  "Lionardo, da Vinci, Maler, Architekt, Naturforscher"
36  "Lionardo, da Vinci, Peintre, Naturaliste, Architecte"
37  "Lionardo, da Vinci, Painter, Architect, Naturalist"
38  "Leonardo, Maler, Architekt, Naturforscher"
39  "Leonardo, Peintre, Naturaliste, Architecte"
40  "Leonardo, Painter, Architect, Naturalist"
41  "Leonardo Da Vinci, Maler, Architekt, Naturforscher"
42  "Leonardo, da Vinci, Painter, Architect, Naturalist"
43  "Leonardo, da Vinci, Maler, Architekt, Naturforscher"
44  "Leonardo, da Vinci, Peintre, Naturaliste, Architecte"
45  "Leonardo DaVinci"
dateOfBirth "15. April 1452"
dateOfDeath "2. Mai 1519"

How can i do that?

SIB
  • 23
  • 1
  • 10
  • Are you sure that this is JSON-Code? It doesn't look like JSON. – steven Sep 14 '18 at 15:23
  • Yes it is. Here is the link: http://hub.culturegraph.org/entityfacts/118640445 – SIB Sep 14 '18 at 15:24
  • Hope it helps https://stackoverflow.com/questions/4343596/how-can-i-parse-a-json-file-with-php – toor Sep 14 '18 at 15:28
  • Ok, thats true. But thats not what you posted... However if it is json then just `$data = json_decode($result, true)` and you will be able to `echo $data['@type'];` and you will get the string: `person` – steven Sep 14 '18 at 15:28
  • Thanks steve. Where do I have to insert this code or replace which code with it? – SIB Sep 14 '18 at 15:35
  • When i put the Code "$data = json_decode($result, true)" and "echo $data['@type'];" at the end after "curl_close($ch);" the same as before happens. It only returns the entire record. – SIB Sep 17 '18 at 13:39
  • Now it works. The option "curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);" was missing. But how can I extract the variable "variantName" from 0 to 45? – SIB Sep 17 '18 at 15:36

0 Answers0