Php 如何阅读网页的标题部分?

Php 如何阅读网页的标题部分?,php,javascript,Php,Javascript,在我的网页上,当用户在文本字段中键入URL时,我希望获得有关该网页的一些信息,如标题或链接信息 有办法吗?在客户端(JavaScript)还是服务器(PHP)上?如何操作?在服务器上: 需要 include(“simpledom.php”); $html=file\u get\u html('http://www.google.com/'); echo$html->find('head')->outertext;//返回。。。 除非页面位于您的域中,否则无法通过Javascript完成。这是因为

在我的网页上,当用户在文本字段中键入URL时,我希望获得有关该网页的一些信息,如标题或链接信息

有办法吗?在客户端(JavaScript)还是服务器(PHP)上?如何操作?

在服务器上:

需要

include(“simpledom.php”);
$html=file\u get\u html('http://www.google.com/');
echo$html->find('head')->outertext;//返回。。。

除非页面位于您的域中,否则无法通过Javascript完成。这是因为跨服务器脚本编写受到限制


但是,您可以使用PHP(检查
file\u get\u contents()
函数),使用simpledom解析
标记的内容,然后将其传递给ajax请求。

在PHP中,您可以打开类似文件的URL,即

$f = fopen ("http://www.site/page.htm", r);
如果您想要实际使用真正的DOM,那么就使用simpledom或其他模块


编辑:您可能可以忽略上面的fopen()建议,出于某种原因,我认为您只询问您完全控制的阅读网站。

因此,您的页面有一个输入文本框,当您的用户键入链接时,您希望检索有关它的信息吗

这可能有用:

根据该页面,将返回如下内容:

Array
(
    [headers] => Array
        (
            [Date] => Mon, 18 Jun 2007 13:56:22 GMT
            [Server] => Apache/2.0.54 (Unix) PHP/4.4.7 mod_ssl/2.0.54 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 SVN/1.4.2
            [X-Powered-By] => PHP/5.2.2
            [Expires] => Thu, 19 Nov 1981 08:52:00 GMT
            [Cache-Control] => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
            [Pragma] => no-cache
            [Set-Cookie] => PHPSESSID=85g9n1i320ao08kp5tmmneohm1; path=/
            [Last-Modified] => Tue, 30 Nov 1999 00:00:00 GMT
            [Vary] => Accept-Encoding
            [Transfer-Encoding] => chunked
            [Content-Type] => text/xml
        )
    [body] => ... Contents of the Page ...
    [info] => Array
        (
            [url] => http://www.bin-co.com/rss.xml.php?section=2
            [content_type] => text/xml
            [http_code] => 200
            [header_size] => 501
            [request_size] => 146
            [filetime] => -1
            [ssl_verify_result] => 0
            [redirect_count] => 0
            [total_time] => 1.113792
            [namelookup_time] => 0.180019
            [connect_time] => 0.467973
            [pretransfer_time] => 0.468035
            [size_upload] => 0
            [size_download] => 2274
            [speed_download] => 2041
            [speed_upload] => 0
            [download_content_length] => 0
            [upload_content_length] => 0
            [starttransfer_time] => 0.826031
            [redirect_time] => 0
        )

)

如今,大多数网络主机都阻止允许url打开。它将在PHP6中完全禁用。如果您需要应用程序支持PHP6,或者您的Web主机已禁用allow_url_fopen,则更好的选择是cURL。我最初的假设是,他正在阅读一个由他控制的网站,经过思考,该网站很差。cURL当然是一个选项,尽管我认为我可能仍然倾向于使用simpledom stype模块。他想要的是部分,而不是标题。如果您想要在没有第三方库的情况下实现相同的功能,PHP中包含的SimpleXML和DomXML库都支持xpath查询。语法有点冗长,但它一定会执行得更好,并使您安装的库集更小。
Array
(
    [headers] => Array
        (
            [Date] => Mon, 18 Jun 2007 13:56:22 GMT
            [Server] => Apache/2.0.54 (Unix) PHP/4.4.7 mod_ssl/2.0.54 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 SVN/1.4.2
            [X-Powered-By] => PHP/5.2.2
            [Expires] => Thu, 19 Nov 1981 08:52:00 GMT
            [Cache-Control] => no-store, no-cache, must-revalidate, post-check=0, pre-check=0
            [Pragma] => no-cache
            [Set-Cookie] => PHPSESSID=85g9n1i320ao08kp5tmmneohm1; path=/
            [Last-Modified] => Tue, 30 Nov 1999 00:00:00 GMT
            [Vary] => Accept-Encoding
            [Transfer-Encoding] => chunked
            [Content-Type] => text/xml
        )
    [body] => ... Contents of the Page ...
    [info] => Array
        (
            [url] => http://www.bin-co.com/rss.xml.php?section=2
            [content_type] => text/xml
            [http_code] => 200
            [header_size] => 501
            [request_size] => 146
            [filetime] => -1
            [ssl_verify_result] => 0
            [redirect_count] => 0
            [total_time] => 1.113792
            [namelookup_time] => 0.180019
            [connect_time] => 0.467973
            [pretransfer_time] => 0.468035
            [size_upload] => 0
            [size_download] => 2274
            [speed_download] => 2041
            [speed_upload] => 0
            [download_content_length] => 0
            [upload_content_length] => 0
            [starttransfer_time] => 0.826031
            [redirect_time] => 0
        )