如何使用ajax通过jquery获取其他网站的标题?

发布于 2024-11-29 01:05:02 字数 83 浏览 0 评论 0原文

例如,我想获取Google,如果我通过'www.google.com',但我发现跨域调用使我在jquery中失败......有什么办法吗解决它吗?谢谢。

For example, I would like to get Google, if I pass the 'www.google.com', but I find that the cross domain call make me do this fail in jquery..... ....Is there any way to solve it? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不是你的备胎 2024-12-06 01:05:02

可以使用ajax和php来做到这一点,ajax请求php文件将从您的url中获取所需的内容,之后您可以使用RegExp或其他方法从页面内容中获取标题。

用于获取网页内容的 PHP 脚本如下所示:

<?php
function get_url_contents($url){
        $crl = curl_init();
        $timeout = 5;
        curl_setopt ($crl, CURLOPT_URL,$url);
        curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
        $ret = curl_exec($crl);
        curl_close($crl);
        return $ret;
}

header('Content-Type: text/html; charset=utf-8'); // needed to display the right encoding

echo get_url_contents('http://example.com');
//echo file_get_contents('http://example.com');
?>

您唯一需要做的就是如何从该页面获取标题。

问候。

It is possible to do this with ajax and php, ajax request to php file which will get the desired from you url and after that u can use RegExp or other method to get the title from the page content.

PHP Script to fetch the web page content will look like:

<?php
function get_url_contents($url){
        $crl = curl_init();
        $timeout = 5;
        curl_setopt ($crl, CURLOPT_URL,$url);
        curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
        $ret = curl_exec($crl);
        curl_close($crl);
        return $ret;
}

header('Content-Type: text/html; charset=utf-8'); // needed to display the right encoding

echo get_url_contents('http://example.com');
//echo file_get_contents('http://example.com');
?>

The only thing who you will need to do is how to get the title from that page.

Regards.

半﹌身腐败 2024-12-06 01:05:02

您可以制作服务器站点代理。从本地服务器站点文件打开 url。

我曾经用 PHP 做了一个: proxy.php

<?php
    function content_type($filename) {

        $mime_types = array(

            'txt' => 'text/plain',
            'htm' => 'text/html',
            'html' => 'text/html',
            'php' => 'text/html',
            'css' => 'text/css',
            'js' => 'application/javascript',
            'json' => 'application/json',
            'xml' => 'application/xml',
            'swf' => 'application/x-shockwave-flash',
            'flv' => 'video/x-flv',

            // images
            'png' => 'image/png',
            'jpe' => 'image/jpeg',
            'jpeg' => 'image/jpeg',
            'jpg' => 'image/jpeg',
            'gif' => 'image/gif',
            'bmp' => 'image/bmp',
            'ico' => 'image/vnd.microsoft.icon',
            'tiff' => 'image/tiff',
            'tif' => 'image/tiff',
            'svg' => 'image/svg+xml',
            'svgz' => 'image/svg+xml',

            // archives
            'zip' => 'application/zip',
            'rar' => 'application/x-rar-compressed',
            'exe' => 'application/x-msdownload',
            'msi' => 'application/x-msdownload',
            'cab' => 'application/vnd.ms-cab-compressed',

            // audio/video
            'mp3' => 'audio/mpeg',
            'qt' => 'video/quicktime',
            'mov' => 'video/quicktime',

            // adobe
            'pdf' => 'application/pdf',
            'psd' => 'image/vnd.adobe.photoshop',
            'ai' => 'application/postscript',
            'eps' => 'application/postscript',
            'ps' => 'application/postscript',

            // ms office
            'doc' => 'application/msword',
            'rtf' => 'application/rtf',
            'xls' => 'application/vnd.ms-excel',
            'ppt' => 'application/vnd.ms-powerpoint',

            // open office
            'odt' => 'application/vnd.oasis.opendocument.text',
            'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
        );

        $ext = strtolower(array_pop(explode('.',$filename)));
        if (array_key_exists($ext, $mime_types)) {
            return $mime_types[$ext];
        }
        elseif (function_exists('finfo_open')) {
            $finfo = finfo_open(FILEINFO_MIME);
            $mimetype = finfo_file($finfo, $filename);
            finfo_close($finfo);
            return $mimetype;
        }
        else {
            return 'application/octet-stream';
        }
    }


    // Website url to open
    $daurl = $_SERVER["QUERY_STRING"];

    header("Content-type:" . content_type($daurl));

    // Get that website's content
    $handle = fopen($daurl, "r");

    // If there is something, read and return
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle, 4096);
            echo  $buffer;
        }
        fclose($handle);
    }
?>

然后使用像这样的 ajax 调用:

proxy.php?http://www.google.dk/index.html

但是但是但是......

这取决于你的服务器,它是否理解 PHP 并允许你打开交叉- 使用 PHP 的域文件?

此脚本将确定扩展名 htmlphpcssjson 等的内容类型。

否则, 然后你可以使用jQuery ajax方法来接收内容:

var url = "proxy.php?http://www.google.dk/index.html";
$.ajax({
    url: url,
    type: 'get',
    dataType: 'html',
    success: function(data){
        var dom = $(data);
        var title = dom.find("title").text();
    }
});

希望它有帮助!

安德烈亚斯

You can make a serversite proxy. Open the url from a local server site file.

I once made one in PHP: proxy.php

<?php
    function content_type($filename) {

        $mime_types = array(

            'txt' => 'text/plain',
            'htm' => 'text/html',
            'html' => 'text/html',
            'php' => 'text/html',
            'css' => 'text/css',
            'js' => 'application/javascript',
            'json' => 'application/json',
            'xml' => 'application/xml',
            'swf' => 'application/x-shockwave-flash',
            'flv' => 'video/x-flv',

            // images
            'png' => 'image/png',
            'jpe' => 'image/jpeg',
            'jpeg' => 'image/jpeg',
            'jpg' => 'image/jpeg',
            'gif' => 'image/gif',
            'bmp' => 'image/bmp',
            'ico' => 'image/vnd.microsoft.icon',
            'tiff' => 'image/tiff',
            'tif' => 'image/tiff',
            'svg' => 'image/svg+xml',
            'svgz' => 'image/svg+xml',

            // archives
            'zip' => 'application/zip',
            'rar' => 'application/x-rar-compressed',
            'exe' => 'application/x-msdownload',
            'msi' => 'application/x-msdownload',
            'cab' => 'application/vnd.ms-cab-compressed',

            // audio/video
            'mp3' => 'audio/mpeg',
            'qt' => 'video/quicktime',
            'mov' => 'video/quicktime',

            // adobe
            'pdf' => 'application/pdf',
            'psd' => 'image/vnd.adobe.photoshop',
            'ai' => 'application/postscript',
            'eps' => 'application/postscript',
            'ps' => 'application/postscript',

            // ms office
            'doc' => 'application/msword',
            'rtf' => 'application/rtf',
            'xls' => 'application/vnd.ms-excel',
            'ppt' => 'application/vnd.ms-powerpoint',

            // open office
            'odt' => 'application/vnd.oasis.opendocument.text',
            'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
        );

        $ext = strtolower(array_pop(explode('.',$filename)));
        if (array_key_exists($ext, $mime_types)) {
            return $mime_types[$ext];
        }
        elseif (function_exists('finfo_open')) {
            $finfo = finfo_open(FILEINFO_MIME);
            $mimetype = finfo_file($finfo, $filename);
            finfo_close($finfo);
            return $mimetype;
        }
        else {
            return 'application/octet-stream';
        }
    }


    // Website url to open
    $daurl = $_SERVER["QUERY_STRING"];

    header("Content-type:" . content_type($daurl));

    // Get that website's content
    $handle = fopen($daurl, "r");

    // If there is something, read and return
    if ($handle) {
        while (!feof($handle)) {
            $buffer = fgets($handle, 4096);
            echo  $buffer;
        }
        fclose($handle);
    }
?>

And then using ajax call like this:

proxy.php?http://www.google.dk/index.html

But but but....

This depends on your server, does it understand PHP and does it allow you to open cross-domain files using PHP?

Else this script is determination the content-type from extension html, php, css, json, etc.

And then you can use jQuery ajax method to receive the content:

var url = "proxy.php?http://www.google.dk/index.html";
$.ajax({
    url: url,
    type: 'get',
    dataType: 'html',
    success: function(data){
        var dom = $(data);
        var title = dom.find("title").text();
    }
});

Hope it helps!

Andreas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文