【问题标题】:PHP file_get_contentsPHP 文件获取内容
【发布时间】:2010-11-25 22:19:43
【问题描述】:

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

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

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

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

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

 http://www.vimeo.com/xxxxxx

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

 http://www.google.com/

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

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

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

【问题讨论】:

  • 试着问一个更直截了当的问题,比如“我如何使用 PHP 在 youtube 中获取电影的缩略图”这可能会让人们反应更快。

标签: php file-get-contents


【解决方案1】:

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

【讨论】:

  • 此外,您可以使用正则表达式从这些网站解析您想要的信息。
【解决方案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);
}

【讨论】:

    【解决方案3】:

    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() 获取的内容将是相同的。

    【讨论】:

    • 在使用 HTTP 包装器调用 file_get_contents(打开 URL)后,变量 $http_response_header 将填充响应头
    【解决方案4】:

    也许Thumbshots 或Snap 已经拥有一些你想要的功能?

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 2015-10-01
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      相关资源
      最近更新 更多