Idea
get information from wikidata - need help for programming
- hermann
-
Topic Author
- Offline
- Elite Member
-
Hermann
Designer of the custom module "Extended Family"
webtrees 2.2.1 (all custom modules installed, PHP 8.3.12, MariaDB 10.6) @ ahnen.hartenthaler.eu
Please Log in or Create an account to join the conversation.
- ekdahl
-
- Offline
- New Member
-
- Posts: 53
There is a WikiData project that has data about all Swedish church parishes (the lowest level in my place hierarchy), including higher level entities and coordinates. Link to project
I would like use this data to either
* (Query to) get suggestions when entering places on events (and save only the used places)
or
* Populate the place hierarchy by a WikiData query and store all places in webtrees
Since there should be lots of data useful for webtrees in WikiData, maybe some common code can be used to simplify fetching data to avoid reinventing the wheel.
Sorry for being slightly off-topic and not being able to offer help, just wanted to say that fetching data from WikiData can be useful for many things in webtrees.
Please Log in or Create an account to join the conversation.
- Franz Frese
-
- Away
- Platinum Member
-
or do you mean the response is: <html> ?
If there is no json, your question is not understood (or may be not available)?!
Please Log in or Create an account to join the conversation.
- bertkoor
-
- Away
- Platinum Member
-
- Greetings from Utrecht, Holland
The German chancellors & presidents... 22 lines is not much data. Since 1949, so for my site it does nothing to my public data.
Is it really better to fetch it from WikiData than having the data in csv yourself? Are you fetching it for each page render, or caching?
In programming there are only two hard problems:
1. Cache invalidation
2. Naming things
3. Off-by-one errors
The Swedish churches... There already is a method to import geographical data in CSV format. If you use that, then nothing else needs to be done. Doing the calls will be far more complicated and has a bigger impact on webtrees code.
stamboom.BertKoor.nl runs on webtrees v2.2.1
Please Log in or Create an account to join the conversation.
- ekdahl
-
- Offline
- New Member
-
- Posts: 53
Good point. Then I only need to produce a query that outputs the results as CSV in the format webtrees expects.The Swedish churches... There already is a method to import geographical data in CSV format. If you use that, then nothing else needs to be done. Doing the calls will be far more complicated and has a bigger impact on webtrees code.
Please Log in or Create an account to join the conversation.
- hermann
-
Topic Author
- Offline
- Elite Member
-
I got the tag <html> instead of the expected JSON object as an answer. The SPARQL query is ok.
I too have more ideas in how to use wikidata for webtrees. Sometimes it would be better to use the information in wikidata and build up a cache available in webtrees (for example export, convert to csv, and use it as location data in webtrees). Sometimes it is better to query wikidata like it is done in a similar case in the GOV Vesta-module using the GOV web service, because the volume of GOV or wikidata is too large and it makes no sense to copy all that information to your webtrees server.
Hermann
Designer of the custom module "Extended Family"
webtrees 2.2.1 (all custom modules installed, PHP 8.3.12, MariaDB 10.6) @ ahnen.hartenthaler.eu
Please Log in or Create an account to join the conversation.
- bertkoor
-
- Away
- Platinum Member
-
- Greetings from Utrecht, Holland
When I click on the link you gave of "all the heads of the former state GDR" etc, I am getting this result:
I do know a thing or two about doing http calls with curl, but I'm clueless about WikiData and SPARQL.
stamboom.BertKoor.nl runs on webtrees v2.2.1
Please Log in or Create an account to join the conversation.
- hermann
-
Topic Author
- Offline
- Elite Member
-
When I click in my browser on the similar link heads of the former state GDR as JSON object with the URL-encoded query I get a response like
I do not understand why you, Bert, got a java error when clicking on the same link.
And I would expect to receive such a JSON object using the code in my first post. But something is wrong with my code. Maybe,
Hermann
Designer of the custom module "Extended Family"
webtrees 2.2.1 (all custom modules installed, PHP 8.3.12, MariaDB 10.6) @ ahnen.hartenthaler.eu
Please Log in or Create an account to join the conversation.
- bertkoor
-
- Away
- Platinum Member
-
- Greetings from Utrecht, Holland
For me simply clicking this link does not work. With the Safari browser at least.A SPARQL query shows all the heads of the former state GDR as JSON object . But ...
Now I have the full url in this window, pasting that in a browser does give results. Likely something with encoding accolades or whatever. The response looks like json in my browser, but the underlying document seems html.
When I copy-paste the url to use with the cli-version of CURL, it says "nested brace in URL position 195".
That I could solve with replacing { by %7B and } by %7D.
I'm getting a header "content-type: application/sparql-results+json;charset=utf-8" so the response is definitely json.
stamboom.BertKoor.nl runs on webtrees v2.2.1
Please Log in or Create an account to join the conversation.
- bertkoor
-
- Away
- Platinum Member
-
- Greetings from Utrecht, Holland
_
How to send a GET request from PHP?
Unless you need more than just the contents of the file, you could use file_get_contents.
Code:$xml = file_get_contents("http://www.example.com/file.xml");
And indeed, `file_get_contents` is being used at multiple places in webtrees. It seems wikidata does not really care about the "Accept" header, you have a query parameter "format=json" for that.
Putting it all together:
But there might be a reason why in webtrees code Greg chose to use Guzzle .
Example here
stamboom.BertKoor.nl runs on webtrees v2.2.1
Please Log in or Create an account to join the conversation.
- hermann
-
Topic Author
- Offline
- Elite Member
-
I first checked that urlencode() replaces the opening and closing braces. That is ok.
Then I tried to use the function file_get_contents(), but that resulted in an error:
Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
Then I used the following code
It is working!!! Thanks ou so much! You made my last day that year!
best regards Hermann
Hermann
Designer of the custom module "Extended Family"
webtrees 2.2.1 (all custom modules installed, PHP 8.3.12, MariaDB 10.6) @ ahnen.hartenthaler.eu
Please Log in or Create an account to join the conversation.
- fisharebest
-
- Offline
- Administrator
-
There is a PHP.INI setting which prevents/allows file_get_contents() to work with URLs. It was frequently blocked by web-hosts for "security reasons". I'm not sure if it is widely blocked today. Probably not.
The guzzle library makes the everything a lot easier. For example, it is possible to set timeouts for file_get_contents() but it is not well documented.
But we only use a tiny part of this library, so it might be worth replacing it with our own code. I'll investigate...
Greg Roach - greg@subaqua.co.uk - @fisharebest@phpc.social - fisharebest.webtrees.net
Please Log in or Create an account to join the conversation.
- hermann
-
Topic Author
- Offline
- Elite Member
-
Try it, please. And give me feedback. And send me more ideas what can be done using wikidata. There are several small and a few large genealogical trees in wikidata. How could they be presented or imported to webtrees?
Hermann
Designer of the custom module "Extended Family"
webtrees 2.2.1 (all custom modules installed, PHP 8.3.12, MariaDB 10.6) @ ahnen.hartenthaler.eu
Please Log in or Create an account to join the conversation.