0

How can I add a PHP conditional to the following code so that if an English wikipedia article has an Arabic version of the page (via a link on the page), then the code will retrieve the Arabic version of the article?

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <?php
    $array = array();
$myFile = "C:/Users/Public/english4.txt";

set_time_limit(0);
$array = file($myFile, FILE_IGNORE_NEW_LINES);
for ($x=0; $x<=100; $x++) {

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://en.wikipedia.org/w/api.php?format=xml&prop=extracts&action=query&titles=".$array[$x]."&redirects=true");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Testing for http://stackoverflow.com/questions/8956331/how-to-get-results-from-the-wikipedia-api-with-php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

//echo $result;

        $e=htmlspecialchars_decode($result);
        $eb=strip_tags($e);

     $eb=  preg_replace('~[^a-zA-Z]+~', ' ', $eb);

     $output = preg_replace('!\s+!', ' ', $eb);
       $n=str_replace("�","",$output);
       $r=substr_count($n, " ");
      if ($r>100){
      file_put_contents($x.".txt",$n);}

}
?>
Shawn
  • 3,583
  • 8
  • 46
  • 63
  • Please modify the question to show us code that you've used to check if the article exists in Arabic. If the code that you show us doesn't do exactly what you'd like to do, also modify the question to tell us the difference between desired and actual behavior. The code currently in your question doesn't appear to be an attempt to do what your question is about -- if this is true, please omit it from the question. – Seth Difley Nov 20 '16 at 19:30
  • Are you looking for the [langlinks API](https://www.mediawiki.org/wiki/API:Langlinks)? – Tgr Nov 20 '16 at 23:51
  • I doesn't ask about wikipedia API due I know it , I asked how to put the condition to check if article exists in Arabic language, also I know how to hit language links ( for example to hit language links for jerusalem article : https://en.wikipedia.org/w/api.php?format=xml&prop=langlinks&action=query&titles=jerusalem&redirects=true ), my specific question how to put condition in php code to check if English article in Wikipedia exists in Arabic then do the extraction – Rinad Lahlooh Nov 21 '16 at 07:43
  • As author's first language is not English, clearly articulate what the author is trying to find. Also adjust heading to be more searchable – Shawn Nov 23 '16 at 07:43

0 Answers0