如何从我的英文wiki链接获取日语wiki链接?

时间:2014-05-26 13:00:50

标签: php wikipedia-api

任务:我们有维基百科英文页面,需要用日语检索相同的页面地址。

建议解析http://en.wikipedia.org/wiki/Mini 4wd?action = raw results(底部还有其他语言链接),但这种方式效率太低。还有其他方法是真正的选择吗?

我们在Wiki中发现了一些单词似乎很好的API。但对于两个词 - 像假面骑士,迷你4wd ......它不起作用。

我的代码无效

    $url = 'https://en.wikipedia.org/w/api.php?action=query&prop=langlinks&format=json&lllimit=100&llprop=url&lllang=ja&titles=Kamen rider';
    $url = rawurldecode(urlencode($url));
    echo $url;
    // outputs: https://en.wikipedia.org/w/api.php?action=query&prop=langlinks&format=json&lllimit=100&llprop=url&lllang=ru&titles=Mini+4wd

    // and then the rest your logic whatever it is the rest
    $header[] = "Accept: application/json";
    $header[] = "Accept-Encoding: gzip";
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
    curl_setopt($ch,CURLOPT_ENCODING , "gzip");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    $response = json_decode(curl_exec($ch));
     /* echo '<pre>';
     print_r($response);
     echo '</pre>'; */

    exit;

2 个答案:

答案 0 :(得分:1)

两个单词不起作用,因为它格式不正确。 Kamen<space>ridermini<space>4wd有空格。你需要先转换它。考虑这个例子:

$url = 'https://en.wikipedia.org/w/api.php?action=query&prop=langlinks&format=json&lllimit=100&llprop=url&lllang=ru&titles=Mini 4wd';
$url = rawurldecode(urlencode($url));
echo $url;
// outputs: https://en.wikipedia.org/w/api.php?action=query&prop=langlinks&format=json&lllimit=100&llprop=url&lllang=ru&titles=Mini+4wd

// and then the rest your logic whatever it is the rest
$contents = file_get_contents($url);
$contents = json_decode($contents, true);
// echo '<pre>';
// print_r($contents);
// echo '</pre>';

Sample Fiddle

答案 1 :(得分:0)

请尝试使用此代码。您的 $ keywords =&#39;迷你4wd&#39;;

$url = 'https://en.wikipedia.org/w/api.php?action=query&prop=langlinks&format=json&lllimit=100&llprop=url&lllang=ja&titles='.$keywords.'&redirects=';

$url1 = rawurldecode(urlencode($url));
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $url1);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// $output contains the output string
$output = curl_exec($ch);
$info = curl_getinfo($ch);
$exec = explode('"url":"',$output);
$exe = explode('",',$exec[1]);
$URL = $exe[0];

输出:

<p>Wikipedia Help here as <a href="<?php echo $URL;?>" target="_blank"><?php echo $URL;?></a></p>