2011-11-02 49 views
0

有沒有什麼辦法像Facebook那樣從url中提取數據?如何從像asp.net mvc3中的網址提取數據?

我引用了很多鏈接,但這是使用php代碼。這 - http://www.99points.info/2010/07/facebook-like-extracting-url-data-with-jquery-ajax-php/

http://www.9lessons.info/2010/06/facebook-like-extracting-url-data-with.html

www.webinfopedia.com/extract-meta-data-from-url-using-php.html

我想要實現這樣的。我怎麼能在我的asp.net mvc3應用程序中實現這個?

我只是refered這個-www.achari.in/facebook-like-url-data-extract-using-jquery 在此我不明白他們在PHP代碼都做了, 誰能解釋一下這段代碼,什麼他們在這做過?

在此, index.php文件有

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>achari.in - Demo - Facebook Like URL data Extract Using jQuery PHP and Ajax</title> 

<link rel="stylesheet" href="css/style.css" type="text/css"> 
<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script> 
<script type="text/javascript" src="js/jquery.livequery.js"></script> 
<script type="text/javascript" src="js/jquery.watermarkinput.js"></script> 
</head> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
     // delete event 
     $('#attach').livequery("click", function(){ 

      if(!isValidURL($('#url').val())) 
      { 
       alert('Please enter a valid url.'); 
       return false; 
      } 
      else 
      { 
       $('#load').show(); 
       $.post("includes/fetch.php?url="+$('#url').val(), { 
       }, function(response){ 
        $('#loader').html($(response).fadeIn('slow')); 
        $('.images img').hide(); 
        $('#load').hide(); 
        $('img#1').fadeIn(); 
        $('#cur_image').val(1); 
       }); 
      } 
     }); 
     // next image 
     $('#next').livequery("click", function(){ 

      var firstimage = $('#cur_image').val(); 
      $('#cur_image').val(1); 
      $('img#'+firstimage).hide(); 
      if(firstimage <= $('#total_images').val()) 
      { 
       firstimage = parseInt(firstimage)+parseInt(1); 
       $('#cur_image').val(firstimage); 
       $('img#'+firstimage).show(); 
      } 
     }); 
     // prev image 
     $('#prev').livequery("click", function(){ 

      var firstimage = $('#cur_image').val(); 

      $('img#'+firstimage).hide(); 
      if(firstimage>0) 
      { 
       firstimage = parseInt(firstimage)-parseInt(1); 
       $('#cur_image').val(firstimage); 
       $('img#'+firstimage).show(); 
      } 

     }); 
     // watermark input fields 
     jQuery(function($){ 

      $("#url").Watermark("http://"); 
     }); 
     jQuery(function($){ 

      $("#url").Watermark("watermark","#369"); 

     }); 
     function UseData(){ 
      $.Watermark.HideAll(); 
      $.Watermark.ShowAll(); 
     } 

    }); 

    function isValidURL(url){ 
     var RegExp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; 

     if(RegExp.test(url)){ 
      return true; 
     }else{ 
      return false; 
     } 
    } 

    function confirmQuit() 
    { 
     var response = confirm('do you wish to close window !');   
     if(response === true) 
      { 
      return true; 
      } 
     else { 
      return false; 
     } 
    } 
</script> 
<body> 
<div align="center"> 
<br clear="all" /><br clear="all" /> 
<div style="font-size:30px;">Facebook Like URL data Extract Using jQuery PHP and Ajax</div> 
<br clear="all" /> 
<a style="color:#000000; font-size:14px" href="#">Back To Tutorial</a> 
<br clear="all" /> 

<input type="hidden" name="cur_image" id="cur_image" /> 
<div class="wrap" align="center"> 

    <div class="box" align="left"> 

     <div class="head">Link</div> 
     <div class="close" align="right"> 
      <div class="closes" onclick="if(confirmQuit() == true) { window.close(); }"></div> 
     </div> 

     <br clear="all" /><br clear="all" /> 

     <input type="text" name="url" size="64" id="url" /> 
     <input type="button" name="attach" value="Attach" id="attach" /> 
     <br clear="all" /> 

     <div id="loader"> 

      <div align="center" id="load" style="display:none"><img src="images/load.gif" /></div> 

     </div> 
     <br clear="all" /> 
    </div> 

</div> 
</div> 
    <br clear="all" /> 
</body> 
</html> 

上面的代碼是UI部分和

fetch.php 
    <?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="images/prev.png" id="prev" alt="" /><img src="images/next.png" id="next" alt="" /></label> 

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

    </div> 

我不知道PHP所以任何人可以幫我找到這個解決方案?

在上面的代碼中使用此功能

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

有誰能夠告訴我究竟是什麼在此代碼 什麼是使用的文件在這個開放的,這是用於讀取URL索引文件?

回答