php实现简单的“分享”功能

看到人人和facebook都有一个分享的功能。

就是输入一个url,然后返回对方的title名称和很多该页面上的图片,操作者选择之后就提交到后端:

如我在renren上分享链接 http://www.dyee.org/bbs/thread-182935-1-2.html

会跳转到这样的页面:

cc

其中,红色区域是获得的网页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;

}

}

}

稍加改动就可以使用了。

Comments 4

  1. xelin wrote:

    hello, 能交个朋友吗。
    我的QQ: 77645757

    Posted 26 十一 2010 at 11:50 下午
  2. 武城吧 wrote:

    我来过了,看看。博主你的博文不错啊

    Posted 19 一 2011 at 7:21 下午
  3. laperlee wrote:

    好久米更新了

    Posted 11 四 2011 at 10:15 上午
  4. bumao wrote:

    最近有点忙啊蒙队

    Posted 13 四 2011 at 5:05 下午

Post a Comment

Your email is never published nor shared. Required fields are marked *