2014-05-22 4 views
-1

오류가 있습니다. 아래 코드는 페이스 북과 유사한 링크 미리보기 스크립트입니다. 그러나 문제는 내가 입력 한 URL이 https://www.facebook.com인데 fb 로고가 표시되면 Google 크롬 로고가 표시됩니다.링크 미리보기 스크립트로 오류가 발생했습니다

코드 브라우저

<?php 
    $url = $_POST['url']; 
    $url = url_clean($url); 

    //clean url 
    function url_clean($ini_url) 
    { 
     $uri = trim($ini_url); 
     if (get_magic_quotes_gpc()) 
     { 
      $uri = stripslashes($uri); 
     } 
     $uri = strtr($uri, array_flip(get_html_translation_table(HTML_ENTITIES))); 
     $uri = strip_tags($uri); 
     $uri = htmlspecialchars($uri); 
     return $uri; 
    } 
    //strip the url 
    function domain_strip($url) 
    { 
     if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === FALSE) 
     { 
      return false; 
     } 
     /*** get the url parts ***/ 
     $parts = parse_url($url); 
     /*** return the host domain ***/ 
     return $parts['host']; 
    } 
    //get the information 
    function get_dat($clean_url) 
    { 
     $web_site = fopen($clean_url, "r"); 
     if (!$web_site) 
     { 
      exit("Error"); 
     } 
     $info = ''; 
     while (!feof($web_site)) 
     { 
      $info .= fgets($web_site, 1024); 
     } 
     return $info; 
    } 

    $string = get_dat($url); 


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

    /// get 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="link_prev_container"> 
    <!----image_cont-----> 
    <?php 
    if(!$images_array) 
    { 
     echo ''; 
    } 
    else 
    { 
     echo '<div class="image_holder">'; 
     $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++; 

        } 
       } 
      } 
     } 
     echo '</div>'; 
    } 
    ?> 
    <!----text inf-----> 
    <div class="text_inf_holder"> 
     <div class="title_container"><?php echo substr(@$url_title[0],0,40) ?></div> 
     <div class="brief_container"><?php echo @$tags['description']; ?></div> 
     <div class="link_container"><a class="link" href="<?php echo $url; ?>"><?php echo domain_strip($url) ?></a></div> 
    </div> 
    </div> 

내 질문을 업데이트 : 위의 스크립트가 아님을 검색 페이스 북의 linkpreview 스크립트는 무엇인가?

+0

스크래핑 중에 사용자 에이전트를 지정하지 않았기 때문에 Facebook에서 Google 크롬 웹 사이트로 리디렉션 할 수 있습니까? – Ignas

+0

Facebook이 사용자 에이전트 문자열을보고 "브라우저"가 너무 오래되었음을 확인했습니다 – WizKid

+0

@Ignas im new이에요. –

답변

2

다음과 $web_site = fopen($clean_url, 'r'); 교체 :

$opts = array (
    'http' => array (
     'method' => "GET", 
     'user_agent' => 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36', 
    ) 
); 

$context = stream_context_create($opts); 
$web_site = fopen($clean_url, 'r', false, $context); 

를 따라 사용자 에이전트 헤더를 보내고 페이스 북은 당신이 크롬을 사용하고 희망 브라우저 검사를 무시할 수 있다고 생각해야합니다.

+0

thnx man 그것이 효과가 있지만'$ tags = get_meta_tags ($ url); –

+0

아마도 동일한 문제 일 수 있습니다. 예를 들어 http://www.php.net/manual/en/function.stream-context-set-default.php를 참조하고 위에 게시 한 $ opts를 참조하십시오. –

+0

확인했지만 사용 방법은 무엇입니까? 나는 이것에 모든 멍청한 놈이다. –

관련 문제