【问题标题】:how can i get data from mediwiki我如何从 mediawiki 获取数据
【发布时间】:2011-09-01 00:31:32
【问题描述】:

您好,我正在使用以下 api 从 mediawiki 获取数据。当我复制这个 url 并将其粘贴到浏览器中时,会出现一个 xml 响应。 http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=API|Main_Page&rvprop=timestamp|user|comment|content

但是当我尝试使用 curl 时,它给了我错误“脚本应该使用带有联系信息的信息性用户代理字符串,否则它们可能会在没有通知的情况下被 IP 阻止。”。

我为此使用以下代码。任何人都可以追踪我的错误吗?

$url='http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=API|Main_Page&rvprop=timestamp|user|comment|content';
$curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url); 
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($curl, CURLOPT_TIMEOUT, 1); 
        $objResponse = curl_exec($curl);
        curl_close($curl);

        echo $objResponse;die;

【问题讨论】:

    标签: php mediawiki-api


    【解决方案1】:

    这将绕过推荐用户代理检查:

        <?php
    
    
        function getwiki($url="", $referer="", $userAgent="") {
            if($url==""||$referer==""||$userAgent=="") { return false;};
            $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
            $headers[] = 'Connection: Keep-Alive';
            $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
            $user_agent = $userAgent;
            $process = curl_init($url);
            curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($process, CURLOPT_HEADER, 0);
            curl_setopt($process, CURLOPT_USERAGENT, $user_agent);
            curl_setopt($process, CURLOPT_REFERER, $referer);
            curl_setopt($process, CURLOPT_TIMEOUT, 30);
            curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
            $return = curl_exec($process);
            curl_close($process);
            return $return;
        }
    
        //edited to include Adam Backstrom's sound advice
        echo getwiki('http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=API|Main_Page&rvprop=timestamp|user|comment|content', 'http://en.wikipedia.org/', 'Mozilla/5.0 (compatible; YourCoolBot/1.0; +http://yoursite.com/botinfo)');
    
        ?>
    

    【讨论】:

    • 请注意,这里不需要“Mozilla/5.0 (compatible;" 垃圾。浏览器这样做只是为了避免破坏那些愚蠢地检查用户代理并拒绝在他们的字符串上运行的网站不认识。把它改成“YourCoolBot/1.0; +yoursite.com/botinfo”之类的。
    【解决方案2】:

    来自 MediaWiki API:Quick start guide

    传递正确识别您的客户端的 User-Agent 标头:不要使用客户端库中的默认 User-Agent,而是使用自定义的 User-Agent,包括您的客户端名称和版本号,例如 MyCuteBot/0.1 .

    在 Wikimedia wiki 上,未能提供 User-Agent 标头或提供空的或通用的标头将导致请求失败并出现 HTTP 403 错误。见meta:User-Agent policy。其他 MediaWiki wiki 可能有类似的政策。

    来自meta:User-Agent policy

    如果您运行机器人,请发送一个 User-Agent 标头来标识该机器人并提供一些与您联系的方式,例如:User-Agent: MyCoolTool (+http://example.com/MyCoolToolPage/)

    【讨论】:

      猜你喜欢
      • 2015-09-05
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多