看到人人和facebook都有一个分享的功能。
就是输入一个url,然后返回对方的title名称和很多该页面上的图片,操作者选择之后就提交到后端:
如我在renren上分享链接 http://www.dyee.org/bbs/thread-182935-1-2.html
会跳转到这样的页面:

其中,红色区域是获得的网页title,而点击绿色部分就可以切换所要使用的图片。
我写的后端代码如下:
<?php
set_time_limit(0);
$url = trim($_REQUEST['url']);
if($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_exec($ch);
$output = curl_exec($ch);
//$c_info = curl_getinfo($ch);
//print_r($c_info);
curl_close($ch);
preg_match(”/charset=([^\"]+)\”/i”,$output, $matches);
//print_r($matches);
$code = $matches[1];
echo ‘<p>编码:’.$code.’</p>’;
preg_match(”/<title>([^<]+)</i”,$output, $matches);
//print_r($matches);
$title = mb_convert_encoding($matches[1],’utf-8′,$code);
echo ‘<p>标题:’.$title.’</p>’;
//$output=str_replace(”\”",”\n”,$output);
//$output=str_replace(”\’”,”\n”,$output);
//preg_match_all(”/src=[\"|']{0,1}(.*\.(gif|jpg|jpeg|png))/i”,$output, $imgs);
//preg_match_all(”/src=[\"|']{0,1}(([^\.]+)\.(gif|jpg|jpeg|png))/i”,$output, $imgs);
//preg_match_all(’/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)/sim’, $output, $imgs, PREG_PATTERN_ORDER);
//preg_match_all(”/<img.*src\s*=\s*[\"|\']?\s*([^>\"\'\s]*)/i”,$output,$imgs);
//preg_match_all(’/<img.+src=\”?(.+\.(jpg|gif|bmp|bnp|png))\”?.+>/i’,$output,$imgs);
preg_match_all(’/src=[\"\']?([%+\*\w\/:\._-]+(?:jpg|gif|bmp|jpeg|png))/ism’, $output,$imgs);
if(is_array($imgs)){
$tmp = explode(’/',$url);
$http = $tmp[0].’//’.$tmp[2];
$tpath = $tmp[0].’//’;
for($i=2;$i<count($tmp)-1;$i++){
$tpath.=$tmp[$i].’/';
}
foreach ($imgs[1] as $i){
echo ‘<img src=”‘.parseImg($i,$url,$http,$tpath).’”/>’;
//echo ”.parseImg($i,$url,$http,$tpath).’<br/>’.”\n”;
}
}
}
function parseImg($img,$page,$http,$tpath){
#直接有http
if(preg_match (’/^http/i’, $img) ){
return $img;
}
#根路径
if(preg_match (’/^\//i’, $img)){
return $http.$img;
}
#本路径
if(preg_match (’/^[^\/]/i’, $img)){
if(preg_match(’/\/$/i’,$page)){
return $page.$img;
}
if($http.’/'==$page || $http==$page){
return $http.’/’.$img;
}else{
return $tpath.$img;
}
}
}
<?php
set_time_limit(0);
$url = trim($_REQUEST['url']);
if($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
preg_match(”/charset=([^\"]+)\”/i”,$output, $matches);
$code = $matches[1];
echo ‘<p>编码:’.$code.’</p>’;
preg_match(”/<title>([^<]+)</i”,$output, $matches);
$title = mb_convert_encoding($matches[1],’utf-8′,$code);
echo ‘<p>标题:’.$title.’</p>’;
preg_match_all(’/src=[\"\']?([%+\*\w\/:\._-]+(?:jpg|gif|bmp|jpeg|png))/ism’, $output,$imgs);
if(is_array($imgs)){
$tmp = explode(’/',$url);
$http = $tmp[0].’//’.$tmp[2];
$tpath = $tmp[0].’//’;
for($i=2;$i<count($tmp)-1;$i++){
$tpath.=$tmp[$i].’/';
}
foreach ($imgs[1] as $i){
echo ‘<img src=”‘.parseImg($i,$url,$http,$tpath).’”/>’;
}
}
}
function parseImg($img,$page,$http,$tpath){
#直接有http
if(preg_match (’/^http/i’, $img) ){
return $img;
}
#根路径
if(preg_match (’/^\//i’, $img)){
return $http.$img;
}
#本路径
if(preg_match (’/^[^\/]/i’, $img)){
if(preg_match(’/\/$/i’,$page)){
return $page.$img;
}
if($http.’/'==$page || $http==$page){
return $http.’/’.$img;
}else{
return $tpath.$img;
}
}
}
稍加改动就可以使用了。