PHP 连接到 MediaWiki API 并检索数据 [英] PHP connecting to MediaWiki API and retrieve data

查看:21
本文介绍了PHP 连接到 MediaWiki API 并检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到有一个问题与我的有点相似,只有 c#:链接文本.让我解释一下:我对整个 Web 服务实现非常陌生,因此我在理解方面遇到了一些困难(尤其是由于 MediaWiki API 手册含糊不清).

I noticed there was a question somewhat similar to mine, only with c#:link text. Let me explain: I'm very new to the whole web-services implementation and so I'm experiencing some difficulty understanding (especially due to the vague MediaWiki API manual).

我想在 PHP(XML 文件)中检索整个页面作为字符串,然后在 PHP 中处理它(我很确定还有其他更复杂的方法来解析 XML 文件,但无论如何):主页维基百科.

I want to retrieve the entire page as a string in PHP (XML file) and then process it in PHP (I'm pretty sure there are other more sophisticated ways to parse XML files but whatever): Main Page wikipedia.

我尝试过 $fp = fopen($url,'r');.它输出: HTTP 请求失败!HTTP/1.0 400 错误请求.API 不需要密钥即可连接到它.

I tried doing $fp = fopen($url,'r');. It outputs: HTTP request failed! HTTP/1.0 400 Bad Request. The API does not require a key to connect to it.

能否详细描述一下如何连接到 API 并将页面作为字符串获取?

Can you describe in detail how to connect to the API and get the page as a string?

URL 是 $url='http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main Page';.我只是想把文件的全部内容读成一个字符串来使用它.

The URL is $url='http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main Page';. I simply want to read the entire content of the file into a string to use it.

推荐答案

连接到该 API 就像检索文件一样简单,

Connecting to that API is as simple as retrieving the file,

fopen

$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$fp = fopen($url, 'r');
while (!feof($fp)) {
    $c .= fread($fp, 8192);
}
echo $c;

file_get_contents

$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$c = file_get_contents($url);
echo $c;

仅当您的服务器启用了 fopen 包装器时,才能使用上述两个.

The above two can only be used if your server has the fopen wrappers enabled.

否则,如果您的服务器安装了 cURL,您可以使用它,

Otherwise if your server has cURL installed you can use that,

$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$c = curl_exec($ch);
echo $c;

这篇关于PHP 连接到 MediaWiki API 并检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆