开发者

PHP/JavaScript: Get images or content of a linked website

I'd like to create a little webservice where you can input a link and it will get somehow the content of the linked site.

So, how could I do that? I actually do not have a single idea. If it is an iFrame, the user called the website in the end, but I开发者_运维知识库 want the server to do that, so that the user can't see anything of this linked website.

It's actually a function like facebook has, if you post a link, it gets the title, the content (text and images) and some tags. How does it do this?

Thanks for your answers!

Flo


In php you can use Curl :

$link = 'http://example.com/page.html';

$ch = curl_init ($link);    
curl_setopt($ch, CURLOPT_AUTOREFERER , true);          
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);    
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);    
$data = curl_exec ($ch);    
curl_close($ch);

echo $data;

Hope it helps.

Edit

Some sites require user agents :

$useragent= "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0_1 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Mobile/7A400";        
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); //add this line

A list of different ones can be found here

Update

$data is a string of the html page, so for the title :

$string = substr($data, strripos($data,'<title>')+strlen('<title>'));
$string = substr($string, 0, stripos($string, "</title>") );

echo $string;

Would give you the page title (like in your example)

Facebook also relies on special meta tags :

<meta property="og:title" content="Welcome to the homepage" />
<meta property="og:image" content="this-is-the-image-for-this-page.jpg" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜