2011-06-17 223 views
0

我有這樣Facebook共享鏈接腳本

<html> 
<head> 
<title>mytitle</title> 
</head> 
<body> 
my data 
some pics... 
</body> 
data and some pics 
</html> 

我需要一個Facebook共享像腳本的頁面時,,所以當任何一個輸入一個網站的網址,該腳本獲取網站標題,,圖片以縮略圖的形式和一些來自body tag的數據..任何想法?

+0

我試圖編輯烏爾問題...但什麼是你的問題 – Ibu 2011-06-17 06:32:20

回答

0
function getMetaTitle($content){ 
    $pattern = "|<[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*>|Ui"; 
    if(preg_match($pattern, $content, $match)) 
    return $match[1]; 
    else 
    return false; 
} 
function getMetaDescription($content) { 
    $metaDescription = false; 
    $metaDescriptionPatterns = array("/]*>/Ui", "/]*>/Ui"); 
    foreach ($metaDescriptionPatterns as $pattern) { 
    if (preg_match($pattern, $content, $match)) 
    $metaDescription = $match[1]; 
    break; 
    } 
    return $metaDescription; 
} 


    function getExcerpt($content) { 
    $text = html_entity_decode($content); 
    $excerpt = array(); 
    //match all tags 
    preg_match_all("|<[^>]+>(.*)]+>|", $text, $p, PREG_PATTERN_ORDER); 
    for ($x = 0; $x < sizeof($p[0]); $x++) { 
    if (preg_match('<p>i', $p[0][$x])) { 
     $strip = strip_tags($p[0][$x]); 
     if (preg_match("/\./", $strip)) 
     $excerpt[] = $strip; 
    } 
    if (isset($excerpt[0])){ 
     preg_match("/([^.]+.)/", $strip,$matches); 
     return $matches[1]; 
     } 
    } 
    return false; 
} 

url = 'http://www.example.com/'; 
$content = file_get_contents($url); 
$title = getMetaTitle($content); 
$description = getMetaDescription($content); 
$excerpt = getExcerpt($content); 
print "title: $title "; 
print "< br />"; 
print "description: $description "; 
print "< br />"; 
print "excerpt: $excerpt"; 
+0

我需要一個瀏覽器解決方案,如Facebook做..像JavaScript的解決方案 – 2011-06-17 06:36:22

+0

你可以發送一個Ajax請求,並得到此信息在結果和顯示在JavaScript窗口 – beta 2011-06-17 06:37:33

+0

好吧,謝謝測試版 – 2011-06-17 06:38:16

0
<?php 
$url = $_REQUEST['url']; 
$url = checkValues($url); 

function checkValues($value) 
{ 
$value = trim($value); 
if (get_magic_quotes_gpc()) 
{ 
    $value = stripslashes($value); 
} 
$value = strtr($value, array_flip(get_html_translation_table(HTML_ENTITIES))); 
$value = strip_tags($value); 
$value = htmlspecialchars($value); 
return $value; 
} 

function fetch_record($path) 
{ 
$file = fopen($path, "r"); 
if (!$file) 
{ 
    exit("Problem occured"); 
} 
$data = ''; 
while (!feof($file)) 
{ 
    $data .= fgets($file, 1024); 
} 
return $data; 
} 

$string = fetch_record($url); 
/// fecth title 
$title_regex = "/<title>(.+)<\/title>/i"; 
preg_match_all($title_regex, $string, $title, PREG_PATTERN_ORDER); 
$url_title = $title[1]; 

/// fecth decription 
$tags = get_meta_tags($url); 

// fetch images 
$image_regex = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui'; 
preg_match_all($image_regex, $string, $img, PREG_PATTERN_ORDER); 
$images_array = $img[1]; 
?> 
<div class="images"> 
<?php 
$k=1; 
for ($i=0;$i<=sizeof($images_array);$i++) 
{ 
    if(@$images_array[$i]) 
    { 
     if(@getimagesize(@$images_array[$i])) 
     { 
      list($width, $height, $type, $attr) = getimagesize(@$images_array[$i]); 
      if($width >= 50 && $height >= 50){ 

      echo "<img src='"[email protected]$images_array[$i]."' width='100' id='".$k."' >"; 

      $k++; 

      } 
     } 
    } 
} 
?> 
<!--<img src="ajax.jpg" alt="" />--> 
<input type="hidden" name="total_images" id="total_images" value="<?php echo --$k?>" /> 
</div> 
<div class="info"> 

    <label class="title"> 
     <?php echo @$url_title[0]; ?> 
    </label> 
    <br clear="all" /> 
    <label class="url"> 
     <?php echo substr($url ,0,35); ?> 
    </label> 
    <br clear="all" /><br clear="all" /> 
    <label class="desc"> 
     <?php echo @$tags['description']; ?> 
    </label> 
    <br clear="all" /><br clear="all" /> 

    <label style="float:left"><img src="prev.png" id="prev" alt="" /><img src="next.png" id="next" alt="" /></label> 

    <label class="totalimg"> 
     Total <?php echo $k?> images 
    </label> 
    <br clear="all" /> 

</div> 

我希望這個腳本運行良好。

來源:http://www.99points.info/2010/07/facebook-like-extracting-url-data-with-jquery-ajax-php/