0

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.

Tattat
  • 15,548
  • 33
  • 87
  • 138
  • Possible answer - http://stackoverflow.com/questions/323982/how-to-parse-xml-in-javascript-from-google/1741748#1741748 – Janis Aug 12 '11 at 11:41

2 Answers2

1

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.

h4cky
  • 899
  • 1
  • 13
  • 33
0

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

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123