PHP Curl:获取目录列表并下载连接到HTTP的目录 [英] PHP Curl: Getting a directory listing and downloading directories connecting to HTTP

查看:65
本文介绍了PHP Curl:获取目录列表并下载连接到HTTP的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对CURL还是很陌生,我能够这样获取单个文件:

I'm fairly new to CURL and I was able to fetch individual files like this:

$c_session = curl_init();

curl_setopt ($c_session, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($c_session, CURLOPT_URL, $uri);
curl_setopt ($c_session, CURLOPT_TIMEOUT, '12');

$content = curl_exec($c_session);
curl_close ($c_session);

现在,我需要能够列出目录并使用CURL下载它们。问题是我需要连接到HTTP服务器而不是FTP服务器。

Now I need to be able to list directories and download them using CURL. The catch is I need to connect to an HTTP server and not an FTP one.

推荐答案

您将不得不解析服务器生成的列表,无论是上面的DirectoryListing生成的,还是服务器另一个生成链接列表的脚本。

You're going to have to parse a list generated by the server, whether that is by DirectoryListing as above, or another server-side script that generates a list of links.

然后您将解析HTML并提取所有a href标记。

You'll then parse the HTML and pull out of all the a href tags.

如果您依赖另一个脚本的输出(Directorylisting),则可能需要通过整洁地运行HTML来生成XHTML,然后将其传递给simplexml。然后,您可以编写类似‘// a’的xpath查询并检索所有属性。

If you're relying on the output of another script (Directorylisting), you may need to run the HTML through tidy to produce XHTML, then pass that into simplexml. You can then write an xpath query like '//a' and retrieve all the attributes.

$list = array();
$x = new SimpleXMLElement($stringfromcurl);
foreach ($x->xpath('//a') as $node) {
    curl_fetch_href($x['href']);
}

或者...自己生成列表,因为它比较容易解析,然后执行相同的交易。

Or... generate the list yourself as something a little easier to parse, then do the same sort of deal.

这等效于执行 wget -r -l1

这篇关于PHP Curl:获取目录列表并下载连接到HTTP的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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