-1

Task: We have wikipedia English page and need to retrieve the same page address in Japanese.

suggested to parse http://en.wikipedia.org/wiki/Mini 4wd?action=raw results (there are other languages links in the bottom), but this way is too inefficient. Are there any other ways is the one real option?

We found some API in Wiki that seems fine for single word. but for two words like - Kamen rider, mini 4wd ... it doesn't work.

My code is not working

    $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;
suresh
  • 180
  • 11
  • Why do you specify `lllang=ru` if you're looking for japanese links? Also, the `titles` field is case-sensitive, and querying for `Mini 4wd` and `Mini 4WD` won't return the same results – svvac May 26 '14 at 13:24
  • Hi swordofpain,I replaced lllang=ja also not working. – suresh May 26 '14 at 13:39
  • You added capitalization? [this](https://en.wikipedia.org/w/api.php?action=query&prop=langlinks&format=json&lllimit=100&llprop=url&lllang=ja&titles=Mini%204WD) looks fine to me... – svvac May 26 '14 at 13:41
  • @suresh your code works fine in my current environment. maybe your curl isn't setup right – user1978142 May 26 '14 at 13:49
  • @suresh and check [**here**](http://codepad.viper-7.com/qhBF9z). if it still doesn't work. alternatively, you could use `file_get_contents()` – user1978142 May 26 '14 at 13:51
  • [It sure seems to work](http://codepad.viper-7.com/sEnbBL) – svvac May 26 '14 at 14:05

2 Answers2

1

Two words doesn't work because its not properly formatted. Kamen<space>rider and mini<space>4wd has spaces. You need it to be converted first. Consider this example:

$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

user1978142
  • 7,946
  • 3
  • 17
  • 20
  • More generally, your keyword needs to be [URL-encoded](https://en.wikipedia.org/wiki/Urlencoding), especially for spaces as you noticed, but also ampersands (`&`), question marks (`?`) and hash signs (`#`). PHP has a nice [`urlencode()` function](http://php.net/manual/en/function.urlencode.php) for that – svvac May 26 '14 at 13:15
  • Hi kevinabelita,It does not return url. I need https://ja.wikipedia.org/wiki/xxxxxxxxxxxxxxxxxxxxxxxx this kind of url from json response – suresh May 26 '14 at 13:16
  • hello @suresh kindly check this [**fiddle**](http://codepad.viper-7.com/ISvmg9) and try to modify it to fit to yours – user1978142 May 26 '14 at 13:20
  • Hi kevinabelita, actually i did those steps but i cant get the url link this is my problem that's why i'm asking kindly guide me – suresh May 26 '14 at 13:25
  • @suresh maybe you could also post your current code and **edit** your question and let us see your work as i will have no clue on how you are doing it :) – user1978142 May 26 '14 at 13:29
  • @suresh maybe you could also utilize a fiddle like this one – user1978142 May 26 '14 at 13:34
0

Kindly try this code it works. Your $keywords = 'Mini 4wd';

$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];

Output :

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