file_get_contents和变量echo

时间:2013-10-08 07:09:20

标签: php file-get-contents

我想解决这个问题,我想回复'http://vine.co/v/' line 3<?php $vine = file_get_contents('http://vine.co/v/', false, $test); $test = "haiKqBFA9Yw"; preg_match('/property="og:image" content="(.*?)"/', $vine, $matches); $url = $matches[1]; $url = str_replace( 'https://', 'http://', $url ); $url = str_replace( 'versionId=', '', $url ); $img = $url; $url = substr($img, 0, strpos($img, "?")); echo $url; 旁边的变量或任何php代码的信息,任何人都可以帮助我吗?

我的代码:

$test

它只返回{{1}}而不是已定义的变量。

1 个答案:

答案 0 :(得分:1)

Don't parse HTML with regex.

简单函数,返回给定网址的所有开放图形元数:

function getUrlOpenGraphMetas($url) {
    $dom = new DOMDocument;
    @$dom->loadHTMLFile($url);
    $xpath = new DOMXPath($dom);
    $metas = array();
    if (!$entries = $xpath->query('//html/head/meta[starts-with(@property, "og:")]')) return $metas;
    foreach ($entries as $entry) $metas[$entry->getAttribute('property')] = $entry->getAttribute('content');
    return $metas;
}

使用:

$metas = getUrlOpenGraphMetas('https://vine.co/v/haiKqBFA9Yw');
$img = $metas['og:image'];
if (($pos = strpos($img, '?')) !== false) $img = substr($img, 0, $pos);
$img = str_replace('https://', '//', $img);
echo $img;