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

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

问题描述

我注意到有一个问题有点类似我的,只有用C#:链接文本
让我解释一下:我是很新的,整个网络的服务实现,因此我遇到了一些困难,理解(特别是由于模糊链接到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处理它(我pretty肯定还有其他更复杂的方式来解析XML文件但不管):
<一href=\"http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main%20Page\"相对=nofollow>主页维基。

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.

我试着做 $计划生育=的fopen($网址,'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?

编辑:
网址是<$c$c>$url='http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=xml&redirects&titles=Main页'; 。我只是想将文件的全部内容读入一个字符串来使用它。

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.

否则,如果你的服务器有卷曲安装,您可以使用,

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天全站免登陆