我希望创建一个PHP脚本,其中,用户将提供一个网页的链接,它将获得该网页的内容,并根据它的内容,解析的内容。
例如,如果用户提供了YouTube链接:
http://www.youtube.com/watch?v=xxxxxxxxxxx然后,它将获取该视频的基本信息(缩略图、嵌入代码?)
或者他们可能会提供一个vimeo链接:
http://www.vimeo.com/xxxxxx或者,即使他们提供了任何链接,但没有附加视频,例如:
http://www.google.com/它可以只抓取页面标题或一些元内容。
我想我必须使用file_get_contents,但我不太确定如何在这种情况下使用它。
我并不是想找人来写完整的代码,但可能会给我提供一些工具,这样我就可以完成这项工作。
发布于 2009-09-05 20:16:47
您可以使用curl或http库。您发送了一个http请求,并且可以使用该库从http响应中获取信息。
发布于 2011-09-01 14:53:03
我知道这个问题很老了,但我会回答的,以防有人找到同样的东西。
将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);
}发布于 2009-09-05 21:49:10
假设您在php.ini中将allow_fopen_url设置为true,则file_get_contents()在这种情况下可以工作。你会这样做:
$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的代码将相同。
https://stackoverflow.com/questions/1384152
复制相似问题