使用php的维基百科API只能获取有关企业的信息


Wikipedia API using php only get information about businesses

我正在我的网站上制作一个搜索结果页面,我试图让它检测何时搜索公司并从中获取维基百科脚本。

但我只希望它显示代码,如果搜索是一家类似于谷歌和必应的公司

谷歌搜索显示公司信息的Microsoft。

我有 API 设置来获取第一段。但现在我需要检测它是否是一家企业,获取图像,并获取创始人/首席执行官。

这是我到目前为止的代码

<div style='width:400px;float:right;border-radius:5px;border:1px solid black;margin:10px 20px;'>
  <?php
     $search = urlencode($search);
     $url = 'http://en.wikipedia.org/w/api.php?action=parse&page='.$search.'&format=json&prop=text&section=0';
     $ch = curl_init($url);
     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt ($ch, CURLOPT_USERAGENT, "TestScript");
     $c = curl_exec($ch);
     $json = json_decode($c);
     $content = $json->{'parse'}->{'text'}->{'*'};
  ?>
  <div style='width:100px;height:100px;float:right;'>
     <?php
        //where i want the picture to display
     ?>
  </div>
  <div>
      <?php
          $pattern = '#<p>(.*)</p>#Us';
          if(preg_match($pattern, $content, $matches)){
              print clean(strip_tags($matches[1]));
          }
      ?>
  </div>
</div>

我查看了维基百科API,但我似乎找不到任何显示它是否在关于公司的部分中的内容,而且抓取页面的图像似乎非常滞后且不需要。另外,使用JavaScript会比使用php更好吗?

  • https://en.wikipedia.org/w/api.php?action=query&list=categorymembers&format=json&cmtitle=Category%3ACompanies&cmtype=subcat&cmlimit=max&rawcontinue= 为您提供所有公司的所有类别的列表。
  • 例如,https://en.wikipedia.org/w/api.php?action=query&prop=categories&format=json&cllimit=max&rawcontinue=&titles=Microsoft 是"Microsoft"的所有类别的列表,您可以解析该列表为/[Cc]ompan(y|ies)/
  • https://en.wikipedia.org/w/api.php?action=query&prop=images&format=json&imlimit=max&rawcontinue=&titles=Microsoft 是该页面上所有文件的列表。

您必须自己进行大部分解析,但 API 可以肯定地用于此目的。