PHP Curl获取页面上的第一个图像和前25个文本单词


PHP Curl to get first image and first 25 words of text on a page

以下两个函数可以完美地从url获取元数据。首先,它获取网页的标题,然后搜索opengraph描述标签。如果它找不到日志描述,它将返回到元描述。

我想要发生的是有一个进一步的回落,所以如果没有元描述存在,它将抓取文本的前25个单词。

同样,我想创建一个返回og:image,所以如果不存在,它将抓取它在页面上找到的第一个图像。

function file_get_contents_curl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
function getit($site) 
{
    $content = file_get_contents_curl($site);
    $doc = new DOMDocument();
    @$doc->loadHTML($content);
    $nodes = $doc->getElementsByTagName('title');
    $node = $doc->getElementsByTagName('img');
    $title = $nodes->item(0)->nodeValue;
    $firstimage = $node->item(0)->getAttribute('src');
    $metas = $doc->getElementsByTagName('meta');
    for ($i = 0; $i < $metas->length; $i++) 
    {
        $meta = $metas->item($i);
        if($meta->getAttribute('property') == 'og:description') 
        {
            $description = $meta->getAttribute('content');
        } 
        elseif ($meta->getAttribute('name') == 'description') 
        { 
            $description = $meta->getAttribute('content'); 
        }
        if($meta->getAttribute('property') == 'og:image') 
        {
        $image = $meta->getAttribute('content'); 
        } 
    }
$str .= 'Title: '.$title.' <br/><br/>';
$str .= 'Description: '.$description.' <br/><br/>';
$str .= 'Image: <img src="'.$image.'">';
$str .= 'Image2: <img src="'.$firstimage.'">';
echo $str;
}

我对使用curl非常陌生,而且一点也不熟练,所以我真的不知道从哪里开始或结束修改我的代码来实现这一点。我已经将二进制传输选项添加到curl setopt,因为我相信它需要对图像进行二进制传输,但我真的不知道我在这里做什么,并将感谢任何关于我如何去做这件事的建议?

更新:

我更新了上面的代码以删除二进制传输。我还添加了以下内容:

$node = $doc->getElementsByTagName('img');
$firstimage = $node->item(0)->getAttribute('src');

然后为了测试的目的,我添加了:

$str .= 'Image2: <img src="'.$firstimage.'">';

所以我现在有返回的第一个图像,仍然需要弄清楚如何在页面上获得文本的前25个单词。

对于这25个单词,我调用了第一个段落标记并将其值设置为字符串,然后我分解了字符串并调用了前25个单词。我将这些设置为一个变量,这样它们就可以很容易地通过测试返回。

对于第一个图像,我添加了:

$node = $doc->getElementsByTagName('img');
$firstimage = $node->item(0)->getAttribute('src');

然后我做一个测试,看看og:image是否为空,如果是,我回显第一个图像。

<?
function file_get_contents_curl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
function getit($site) {
    $parsing = file_get_contents_curl($site);
    //parsing begins here:
    $doc = new DOMDocument();
    @$doc->loadHTML($parsing);
    $nodes = $doc->getElementsByTagName('title');
    $node = $doc->getElementsByTagName('img');
    $para = $doc->getElementsByTagName('p');
    //get and display what you need:
    $title = $nodes->item(0)->nodeValue;
    $firstimage = $node->item(0)->getAttribute('src');
    $firstparagraph = $para->item(0)->nodeValue;
    $metas = $doc->getElementsByTagName('meta');
    for ($i = 0; $i < $metas->length; $i++)
    {
        $meta = $metas->item($i);
        if($meta->getAttribute('property') == 'og:description') {
            $description = $meta->getAttribute('content');
        } 
        elseif ($meta->getAttribute('name') == 'description') { 
            $description = $meta->getAttribute('content');
        } else { 
            $descrition = "<p>".implode(' ', array_slice(explode(' ', $firstparagraph), 0, 25))."</p>"; 
        }
        if($meta->getAttribute('property') == 'og:image') {
            $image = $meta->getAttribute('content');
        }   
    }   
    if ($image != '') { $image = $image; } else { $image = $firstimage; }
    $str .= 'Title: '.$title.' <br/><br/>';
    $str .= 'Description: '.$description.' <br/><br/>';
    $str .= 'Image: <img src="'.$image.'"><br/><br/>';
    echo $str;
}
?>