Kullanımı: Kütüphane olarak simple_html_dom.php kullanılmaktadır. Bu dosyayı indirmeniz için link bırakacağım. https://www.wikidata.org/wiki/Q5152 adresin sonundaki Q ile başlayan değeri almanız yeterli olacaktır.
<?php
set_time_limit(0);
error_reporting(0);
ini_set('memory_limit', '-1');
include "simple_html_dom.php";
function clear_html($comment)
{
$allow_tags = "<b><strong><em><i><br><ul><li><ol><pre><code><blockquote><ins><p><del><h><h1><h2><h3><h4><h5><h6><table><tr><td><th><tbody><thead><tfoot><style>";
return strip_tags($comment, $allow_tags);
}
function wikiSearch($a, $b, $c)
{
return @preg_replace('/' . preg_quote($a, '/') . '(.*?)' . preg_quote($b, '/') . '/i', '', $c);
}
function wikiReplace($comment)
{
$newstr = wikiSearch("[", "]", $comment);
$newstr = str_replace(array("[", "]"), array("", ""), $newstr);
return $newstr;
}
function wikiData($wikidata, $lang)
{
$l = "";
$m = [];
$options = [
'http' => [
'method' => "GET",
'header' =>
"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n" . // check function.stream-context-create on php.net
"User-Agent: Mozilla/5.0 (Linux; Android 10; SM-G980F Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.96 Mobile Safari/537.36\r\n", // i.e. An iPad
],
];
$context = stream_context_create($options);
if ($wikidata == "") {
$m[] = "Henüz Bilgi Eklenmedi. En Kısa Sürede Ekleyeceğiz...";
return $m;
exit();
}
$url = "https://www.wikidata.org/wiki/" . $wikidata . "";
$html = file_get_html($url);
$wiki_data = $html->find('div[data-wb-sitelinks-group="wikipedia"]', 0);
$datas = $wiki_data->find('span[class="wikibase-sitelinkview-page"] a');
foreach ($datas as $data) {
if($data->hreflang == $lang) {
$l = $data->href;
}
if($l == "") {
if($data->hreflang == "en") {
$l = $data->href;
}
}
}
if ($l != "") {
$h2 = [];
$html2 = file_get_html($l, false, $context);
$wiki_data1 = $html2->find('h2[class="section-heading"]');
$h_num = 1;
foreach ($wiki_data1 as $h2_data) {
$h_num++;
$h2[$h_num] = $h2_data->find('span[class="mw-headline"]', 0)->innertext;
}
$wiki_data2 = $html2->find('section');
$i = 0;
foreach ($wiki_data2 as $section) {
$i++;
if($section->find('p', $i)->innertext != "") {
$m[] = '<section id="'.$i.'"><h5>'.$h2[$i].'</h5>';
foreach ($section->find('p') as $data2) {
// if ($i == 1) {
$m[] = clear_html(wikiReplace($data2));
// }
}
$m[] = "</section>";
}
}
if ($m[0] != "") {
return $m;
} else {
$m[] = "Henüz Bilgi Eklenmedi. En Kısa Sürede Ekleyeceğiz...";
return $m;
}
} else {
$m[] = "Henüz Bilgi Eklenmedi. En Kısa Sürede Ekleyeceğiz...";
return $m;
}
}
$datas = wikiData("Q43", "tr");
foreach($datas as $data) {
echo $data;
}
?>Demo: https://countryinfo.io/demo/wikidataÖrnek Kodu İndir: https://countryinfo.io/demo/wikidata/wikidata.zip