0

我正在寻找创建一个 PHP 脚本,用户将在其中提供指向网页的链接,它将获取该网页的内容并根据其内容解析内容。

例如,如果用户提供 YouTube 链接:

http://www.youtube.com/watch?v=xxxxxxxxxxx

然后,它将获取有关该视频的基本信息(缩略图、嵌入代码?)

或者他们可能会提供一个 vimeo 链接:

 http://www.vimeo.com/xxxxxx

或者即使他们提供任何链接,但没有附加视频,例如:

 http://www.google.com/

它可以只抓取页面标题或一些元内容。

我想我必须使用 file_get_contents,但我不确定如何在这种情况下使用它。

我不是在找人来编写整个代码,但也许会为我提供一些工具,以便我可以完成此任务。

4

4 回答 4

3

您可以使用curlhttp库。您发送一个 http 请求,并且可以使用该库从 http 响应中获取信息。

于 2009-09-05T20:16:46.983 回答
2

我知道这个问题已经很老了,但我会回答以防万一有人点击它寻找相同的东西。

将 oEmbed (http://oembed.com/) 用于 YouTube、Vimeo、Wordpress、Slideshare、Hulu、Flickr 和许多其他服务。如果不在列表中,或者您想让它更精确,您可以使用:

http://simplehtmldom.sourceforge.net/

它是一种用于 PHP 的 jQuery,这意味着您可以使用 HTML 选择器来获取部分代码(即:所有图像、获取 div 的内容、仅返回节点的文本(无 HTML)内容等)。

你可以做这样的事情(可以做得更优雅,但这只是一个例子):

    require_once("simple_html_dom.php");
function getContent ($item, $contentLength) 
{
    $raw;
    $content = "";
    $html;
    $images = "";

    if (isset ($item->content) && $item->content != "")
    {
        $raw = $item->content;
        $html = str_get_html ($raw);            
        $content = str_replace("\n", "<BR /><BR />\n\n", trim($html->plaintext));

        try
        {
            foreach($html->find('img') as $image) {
                if ($image->width != "1") 
                {
                    // Don't include images smaller than 100px height
                    $include = false;
                    $height = $image->width;
                    if ($height != "" && $height >= 100)
                    {
                        $include = true;
                    }
                    /*else
                    {
                        list($width, $height, $type, $attr) = getimagesize($image->src);
                            if ($height != "" && $height >= 100)
                                $include = true;
                    }*/                 

                    if ($include == true)
                    {
                        $images = $images . '<div class="theImage"><a href="'.$image->src.'" title="'.$image->alt.'"><img src="'.$image->src.'" alt="'.$image->alt.'" class="postImage" border="0" /></a></div>';
                    }
                }
            }
        }
        catch (Exception $e) {
            // Do nothing
        }

        $images = '<div id="images">'.$images.'</div>';
    }
    else
    {
        $raw = $item->summary;
        $content = str_get_html ($raw)->plaintext;
    }

    return (substr($content, 0 , $contentLength) . (strlen ($content) > $contentLength ? "..." : "") . $images);
}
于 2011-09-01T06:53:03.003 回答
1

file_get_contents()假设您已在 php.ini中allow_fopen_url 设置为,则可以在这种情况下使用。true你会做的是这样的:

$pageContent = @file_get_contents($url);
if ($pageContent) {
    preg_match_all('#<embed.*</embed>#', $pageContent, $matches);
    $embedStrings = $matches[0];
}

也就是说,在错误处理其他成功或失败file_get_contents()时接收内容的方式上不会给您太多帮助。false如果您想对请求进行更丰富的控制并访问 HTTP 响应代码,请使用curl函数,特别是,curl_get_info查看响应代码、mime 类型、编码等。一旦您通过任一 curl 获取内容或者file_get_contents()您用于解析它以查找感兴趣的 HTML 的代码将是相同的。

于 2009-09-05T21:49:09.847 回答
0

也许ThumbshotsSnap已经拥有您想要的一些功能?

我知道这不是您正在寻找的东西,但至少对于可能很方便的嵌入式东西。txwikinger 也已经回答了你的其他问题。但也许这对你有帮助。

于 2009-09-05T20:35:11.773 回答