【问题标题】:error with link preview script链接预览脚本错误
【发布时间】:2014-07-12 03:08:14
【问题描述】:

我有一个链接预览脚本的以下代码,它像 facebook 一样工作,但问题是当我输入 url https://www.facebook.com 而不是向我显示 fb 徽标时,它向我显示 google chrome 徽标并显示更新您的浏览器

代码

    <?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='".@$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>

我的问题:facebook 的链接预览脚本搜索上述脚本不是什么?

【问题讨论】:

  • 是否有可能因为您在抓取过程中没有指定用户代理,facebook 会将您重定向到 google chrome 网站?
  • Facebook 可能正在查看您的用户代理字符串并确定您的“浏览器”太旧
  • @Ignas 我是新手,所以请详细说明
  • @WizKid 这太高级了,你能告诉我认为我对这一切都是菜鸟吗????

标签: javascript php facebook hyperlink


【解决方案1】:

$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);

如果应该发送用户代理标头并让 Facebook 认为您正在使用 chrome 并希望绕过浏览器检查。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-07-20
  • 2015-12-01
  • 1970-01-01
  • 2018-06-13
  • 1970-01-01
  • 2012-02-26
  • 1970-01-01
相关资源
最近更新 更多