如何从远程服务器收集HTML源响应? [英] How to collect HTML source response from a remote server?

查看:95
本文介绍了如何从远程服务器收集HTML源响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个服务器页面的HTML代码中,我需要解决在放置在另一个远程服务器上的数据库上的特定项目的搜索,这个服务器并非我自己。

From within the HTML code in one of my server pages I need to address a search of a specific item on a database placed in another remote server that I don’t own myself.

执行我的请求的搜索类型示例: http:// www.remoteserver.com/items/search.php?search_size=XXL

Example of the search type that performs my request: http://www.remoteserver.com/items/search.php?search_size=XXL

远程服务器向我(作为客户端)提供显示页面的响应几个符合我的搜索条件的项目。

The remote server provides to me - as client - the response displaying a page with several items that match my search criteria.

我不想显示此页面。我想要的是将字符串(或本地文件)收集到远程服务器HTML响应的全部内容(我们在IE浏览器客户端中单击查看源代码时可以访问的代码)。

I don’t want to have this page displayed. What I want is to collect into a string (or local file) the full contents of the remote server HTML response (the code we have access when we click on ‘View Source’ in my IE browser client).

如果我收集这些数据(它可以很容易地达到50000字节),然后我可以筛选我感兴趣的数据(子字符串)并将一个新的请求汇编到远程服务器,在提供的响应中的具体项目。

If I collect that data (it could easily reach reach 50000 bytes) I can then filter the one in which I am interested (substrings) and assemble a new request to the remote server for only one of the specific items in the response provided.

有没有什么方法可以通过Javascript或PHP远程服务器提供的响应来获取HTML,还可以避免在浏览器本身显示响应?

Is there any way through which I can get HTML from the response provided by the remote server with Javascript or PHP, and also avoid the display of the response in the browser itself?

我希望我没有混淆你的想法...
感谢您提供的任何帮助。

I hope I have not confused your minds … Thanks for any help you may provide.

推荐答案

正如@mario提到的,有几种不同的方法可以做到这一点。

As @mario mentioned, there are several different ways to do it.

使用 file_get_contents()

$txt = file_get_contents('http://www.example.com/');
echo $txt;

使用php的curl函数:

Using php's curl functions:

$url = 'http://www.mysite.com';
$ch = curl_init($url);

// Tell curl_exec to return the text instead of sending it to STDOUT
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

// Don't include return header in output
curl_setopt($ch, CURLOPT_HEADER, 0);

$txt = curl_exec($ch);
curl_close($ch);

echo $txt;

curl可能是最稳健的选项,因为您可以选择更多的控制确切的请求参数和可能性当事情没有按计划进行时进行错误处理

curl is probably the most robust option because you have options for more control over the exact request parameters and possibilities for error handling when things don't go as planned

这篇关于如何从远程服务器收集HTML源响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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