dsbiw2911188 2017-10-27 05:30
浏览 469

如何在我的网站链接中显示og:图像作为缩略图

I have an input field where user can enter URL (website link, video link, google map link or any other link)

<input type="url" name="user_url">

I want to show thumbnail image and description of these links as preview instead of simple URL. As i wana use the og:image meta to get thumbnail but I don't know how i can archive this and what technology should i use or either is there any api to get this. thanks

  • 写回答

1条回答 默认 最新

  • dongnaoben4456 2017-10-27 11:03
    关注

    You can use the codes bellow. Just pass url variable to the get_url_data.php using POST request and in response you will get image, description and title.

    get_url_data.php

    <?php
    
    if(isset($_POST['url']) && !empty($_POST['url']) && filter_var($_POST['url'], FILTER_VALIDATE_URL)) {
        //gets the HTML data
        $source = getData($_POST['url']);
        // DOM document Creation
        $doc = new DOMDocument;
        libxml_use_internal_errors(true);
        $doc->loadHTML($source);
        libxml_clear_errors();
        // DOM XPath Creation
        $xpath = new DOMXPath($doc);
        // Gets title
        $title = $xpath->query('//title')->item(0)->textContent;
        if(empty($title))
            $title = "No title found";
        // Gets all Open Graph images
        $events = $xpath->query('//meta[@property="og:image"]/@content');
        $image = "";
        for($i = 0; $i < ($events->length); $i++) {
            $event = $events->item($i);
            $des = $xpath->evaluate('//meta[@property="og:image"]/@content', $event);
            if ($des->length > 0) {
                $image = $des->item(0)->value;
                break;
            }
        }
        if(empty($image))
            $image = "image-not-found.gif";
        // Gets all Open Graph descriptions
        $events = $xpath->query('//meta[@property="og:description"]/@content');
        $description = "";
        for($i = 0; $i < ($events->length); $i++) {
            $event = $events->item($i);
            $des = $xpath->evaluate('//meta[@property="og:description"]/@content', $event);
            if ($des->length > 0) {
                $description = $des->item(0)->value;
                break;
            }
        }
        if(empty($description))
            $description = "-- No description found --";
        //output
        $data = array('title' => $title, 'image' => $image, 'description' => $description);
        echo json_encode($data);
    }
    //fetches HTML data from a URL
    function getData($url)
    {   
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6 (.NET CLR 3.0.4506.2152)");
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_URL, $url);    
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }
    

    You can also see my git repository for more details.

    https://github.com/khmahbubul/get-thumbnail

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器