v2.5.2
Giriş yap

Wikipedia verilerini PHP ile çekmek ve echo ile göstermek

erdal42
465 defa görüntülendi ve 1 kişi tarafından değerlendirildi

Merhabalar
Wikipedia : https://www.mediawiki.org/wiki/API:Main_page
Bu api ile nasıl php ile çekerim. Resimli ve başlıklı almak istiyorum.
Nasıl yaparız ?

Cevap yaz
Cevaplar (11)
erdal42
847 gün önce

Pardon hocam resim demişim. Sadece açıklaması diyecektim.

erdal42
847 gün önce

@etukenmez
Hocam siz ne güzel insansınız. Mükemmel çalışıyor :)

echo $data['title'];

Bu kod ile wiki'den yazıyı çekiyoruz. Birde bunun sadece açıklaması (yani kısa özeti) ve resmini nasıl yaparız. Yani resmi çekiyor ama bazen alakasın resim çıkabiliyor ki genellikle de çıkıyor.
Yani hocam title data gibi açıklama ve resmi nasıl yapabiliriz.

etukenmez
847 gün önce
$searchParam = htmlspecialchars($_GET['q']);

...

Şeklinde düzenleyebilirsiniz.
Diğer soruların için sanırım gönderilen parametreleri değiştirmek gerekiyor. Tam olarak nasıl çalıştığını anlamak için dökümanlarını okumak gerekiyor.

$searchTitle = [
	"action"=> "query",
	"format"=> "json",
	"prop"=> "imageinfo|images|info|links|categories|description",
	"list"=> "search",
	"titles"=> $searchParam,
	"srsearch"=> $searchParam
]

Bu parametlerelerle gönderildiğinde şöyle bir response geliyor.

{
    "continue": {
        "sroffset": 10,
        "plcontinue": "11089416|0|Test_match_(rugby_union)",
        "continue": "||imageinfo|images|info|categories|description"
    },
    "query": {
        "pages": {
            "11089416": {
                "pageid": 11089416,
                "ns": 0,
                "title": "Test",
                "images": [
                    {
                        "ns": 6,
                        "title": "File:Disambig gray.svg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Semi-protection-shackle.svg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Vector search icon.svg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Wiktionary-logo-en-v2.svg"
                    }
                ],
                "contentmodel": "wikitext",
                "pagelanguage": "en",
                "pagelanguagehtmlcode": "en",
                "pagelanguagedir": "ltr",
                "touched": "2021-12-10T17:03:27Z",
                "lastrevid": 1024744141,
                "length": 2608,
                "links": [
                    {
                        "ns": 0,
                        "title": "Test (Unix)"
                    },
                    {
                        "ns": 0,
                        "title": "Test (assessment)"
                    },
                    {
                        "ns": 0,
                        "title": "Test (biology)"
                    },
                    {
                        "ns": 0,
                        "title": "Test (greyhound competition)"
                    },
                    {
                        "ns": 0,
                        "title": "Test (group)"
                    },
                    {
                        "ns": 0,
                        "title": "Test (law)"
                    },
                    {
                        "ns": 0,
                        "title": "Test (wrestler)"
                    },
                    {
                        "ns": 0,
                        "title": "Test cricket"
                    },
                    {
                        "ns": 0,
                        "title": "Test equipment"
                    },
                    {
                        "ns": 0,
                        "title": "Test match (rugby league)"
                    }
                ],
                "categories": [
                    {
                        "ns": 14,
                        "title": "Category:All article disambiguation pages"
                    },
                    {
                        "ns": 14,
                        "title": "Category:All disambiguation pages"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Disambiguation pages"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Disambiguation pages with short descriptions"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Disambiguation pages with surname-holder lists"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Short description is different from Wikidata"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Wikipedia indefinitely move-protected pages"
                    },
                    {
                        "ns": 14,
                        "title": "Category:Wikipedia pages semi-protected against vandalism"
                    }
                ],
                "description": "Topics referred to by the same term",
                "descriptionsource": "local"
            }
        },
        "searchinfo": {
            "totalhits": 282902,
            "suggestion": "text",
            "suggestionsnippet": "text"
        },
        "search": [
            {
                "ns": 0,
                "title": "Test",
                "pageid": 11089416,
                "size": 2608,
                "wordcount": 264,
                "snippet": "<span class=\"searchmatch\">Test</span>(s), <span class=\"searchmatch\">testing</span>, or <span class=\"searchmatch\">TEST</span> may refer to: <span class=\"searchmatch\">Test</span> (assessment), an educational assessment intended to measure the respondents' knowledge or other abilities",
                "timestamp": "2021-05-23T21:53:34Z"
            },
            {
                "ns": 0,
                "title": "TeST Gliders",
                "pageid": 24884283,
                "size": 2602,
                "wordcount": 164,
                "snippet": "<span class=\"searchmatch\">TeST</span> Gliders is a manufacturer of ultralight sailplanes and motorgliders based in Brno, Czech Republic. <span class=\"searchmatch\">TeST</span> TST-1 Alpin <span class=\"searchmatch\">TeST</span> TST-3 Alpin T <span class=\"searchmatch\">TeST</span> TST-5",
                "timestamp": "2018-02-22T16:19:10Z"
            },
            {
                "ns": 0,
                "title": ".test",
                "pageid": 2249147,
                "size": 2470,
                "wordcount": 246,
                "snippet": "The name <span class=\"searchmatch\">test</span> is a top-level domain (TLD) that is intended for use in the <span class=\"searchmatch\">testing</span> of software. The name was reserved by the Internet Engineering Task Force",
                "timestamp": "2021-12-24T22:05:27Z"
            },
            {
                "ns": 0,
                "title": "Software testing",
                "pageid": 29090,
                "size": 94953,
                "wordcount": 11447,
                "snippet": "Software <span class=\"searchmatch\">testing</span> is the act of examining the artifacts and the behavior of the software under <span class=\"searchmatch\">test</span> by validation and verification. Software <span class=\"searchmatch\">testing</span> can also",
                "timestamp": "2021-12-29T20:28:18Z"
            },
            {
                "ns": 0,
                "title": "This Is Not a Test!",
                "pageid": 1858823,
                "size": 19480,
                "wordcount": 1300,
                "snippet": "This Is Not a <span class=\"searchmatch\">Test</span>! is the fifth studio album by American rapper Missy Elliott, released by The Goldmind Inc. and Elektra Records on November 25, 2003",
                "timestamp": "2021-12-08T23:57:20Z"
            },
            {
                "ns": 0,
                "title": "COVID-19 testing",
                "pageid": 63204759,
                "size": 178054,
                "wordcount": 20226,
                "snippet": "COVID-19 <span class=\"searchmatch\">testing</span> involves analyzing samples to assess the current or past presence of SARS-CoV-2. The two main branches detect either the presence of",
                "timestamp": "2021-12-30T11:30:31Z"
            },
            {
                "ns": 0,
                "title": "This is a test",
                "pageid": 5069303,
                "size": 240,
                "wordcount": 56,
                "snippet": "This is a <span class=\"searchmatch\">test</span> may refer to: The <span class=\"searchmatch\">testing</span> of the Emergency Alert System The <span class=\"searchmatch\">testing</span> of the former Emergency Broadcast System This disambiguation page lists",
                "timestamp": "2021-11-26T17:39:12Z"
            },
            {
                "ns": 0,
                "title": "The Test",
                "pageid": 23262214,
                "size": 1389,
                "wordcount": 180,
                "snippet": "The <span class=\"searchmatch\">Test</span> may refer to: &quot;The <span class=\"searchmatch\">Test</span>&quot; (short story), short story by Franz Kafka The <span class=\"searchmatch\">Test</span> (Wright novel), novel by Mary Tappan Wright The <span class=\"searchmatch\">Test</span> (Applegate novel)",
                "timestamp": "2020-04-04T23:45:04Z"
            },
            {
                "ns": 0,
                "title": "Bechdel test",
                "pageid": 32255505,
                "size": 62268,
                "wordcount": 6092,
                "snippet": "The Bechdel <span class=\"searchmatch\">test</span> (/\u02c8b\u025bkd\u0259l/ BEK-d\u0259l) is a measure of the representation of women in fiction. It asks whether a work features at least two women who talk",
                "timestamp": "2021-12-31T04:57:17Z"
            },
            {
                "ns": 0,
                "title": "Test cricket",
                "pageid": 30863,
                "size": 39657,
                "wordcount": 4028,
                "snippet": "<span class=\"searchmatch\">Test</span> cricket is the form of the sport of cricket with the longest match duration and is considered the game's highest standard. Rotary <span class=\"searchmatch\">Test</span> matches are",
                "timestamp": "2021-12-30T14:00:33Z"
            }
        ]
    }
}

buradaki değerlere inceleyip işine yarayanları kullabilirsin.
Kendisinin yayınlamış olduğu bir APISandBox mevcur burayı incelemeni tavsiye ederim çokça açıklama var kurcalayarak istediğin sonucu elde edebileceğini düşünüyorum.

erdal42
848 gün önce

@etukenmez hocam çok acile ediyorum ya. Sayfa işlemini yaptım hallettim. Class ları nasıl vericez. Birde tıklanması için.

erdal42
848 gün önce

Ve ayrıca hocam wiki hakkında özet bölümü çıkmıyor.

erdal42
848 gün önce

@etukenmez Hocam çok ama çok teşekkür ederim. Birkaç soru aklımda kaldı;
$searchParam = "Test"; bölümü yani ne aranacak sa varsa çıksın istiyoruz.
Oraya '<?php echo htmlspecialchars($_GET['q']); ?>' tarzı ekleyebilirmiyiz.
Yani form'a yazıldığında wikiler çıksın ne yazıldıysa.
Ve sadece 1 wiki sonucun çıkmasını istiyorum.
Sizi gerçekten rahatsız ediyorum ama benim için çok büyük tecrübe oluyor hocam, yeniden teşekkürler.

etukenmez
848 gün önce

Şöyle bir kod yazdım açıklamaları ekledim liste şeklinde verileri çekiyor.


//Aramak istediğimiz değer
$searchParam = "Test";

$searchTitle = [
    "action" => "query",
    "list" => "search",
    "srsearch" => $searchParam,
    "format" => "json",
    /* Bu değerler kullanılarak sayfalama yapılabilir */
//    "srlimit" => 10,
//    "sroffset" => 9
];

/* verileri çekecek olan metodumuz */
function getData($params){
    $endPoint = "https://en.wikipedia.org/w/api.php";
    $url = $endPoint . "?" . http_build_query( $params );

    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    $output = curl_exec( $ch );
    curl_close( $ch );

    return json_decode( $output, true );
}

echo '<ul>';
$result = getData($searchTitle);

$query = $result['query'];
$total = $query['searchinfo']['totalhits'];
$datas = $query['search'];
/* Sonuç var mı kontrolü */
if ($total){
    foreach ($datas as $data){
        /* görselleri çekmek için belirteceğimiz parametreler */
        $imagesParam = [
            "action" => "query",
            "prop" => "images",
            "titles" => $data['title'],
            "format" => "json",
        ];

        echo '<li>';
        $images = getData($imagesParam);
        $firstPage = reset($images['query']['pages']);
        /* Konuya ait görsel değeri döndümü kontrolü yapılıyor */
        if ($firstPage && count($firstPage['images']) > 0){
            $imageUrlParams = [
                "action" => "query",
                "format" => "json",
                "prop" => "imageinfo",
                // İlk görseli alıyoruz
                "titles" => $firstPage['images'][0]['title'],
                'iiprop' => 'url'
            ];
            /* Görselin detayını çekerek url değerini bulmak için istek atıyoruz */
            $imageUrlResponse = getData($imageUrlParams);
            $imageUrlResponseFirstPage = reset($imageUrlResponse['query']['pages']);
            /* Dönen değer var mı kontrolü */
            if ($imageUrlResponseFirstPage && count($imageUrlResponseFirstPage['imageinfo']) > 0)
                echo '<img width="32" src="'. $imageUrlResponseFirstPage['imageinfo'][0]['url'] .'">';
        }

        echo $data['title'];
        echo '</li>';
    }
}else{
    echo $searchParam . ' hakkında hiç içerik bulunamadı';
}
echo '</ul>';

HTML tarafında bir input ve button koyarak yazılan değeri bu kod parçasıyla kullanırsan aramalar görselleriyle beraber liste şeklinde görünür.

erdal42
848 gün önce

@etukenmez yani form'a yazılıp enter'a basıldığında eğer veri varsa yazı başlık özet ve resmi ile birlikte gelmesini istiyorum. Google tarzı

erdal42
848 gün önce

@etukenmez yani Google bing tarzı. Örnek php kodu atabilirmisin ? Yani ne yazılırsa eğer viki de varsa gözüksün yoksa gözükmesin

etukenmez
848 gün önce

Merhaba tam anlatmak istediğinizi anlamadım ama bakınırken şöyle bir sayfaya denk geldim API:Images örnek php kodu ve geri dönüş değeri verilmiş.

erdal42
848 gün önce

@abdullahx Fikriniz var mı hocam ? Hayatımı kurtardınız :D