-3

I am trying to figure this out, I want to echo information from a variable or any php code beside 'http://vine.co/v/' under line 3 can anyone help me?

My code:

<?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;

it just returns with $test not a defined variable.

Nelson
  • 38
  • 1
  • 9
  • According to docs, third parameter in `file_get_contents`, needs to be a resource. Check 4th example: http://php.net/manual/en/function.file-get-contents.php – Sal00m Oct 08 '13 at 07:21
  • Alright, Thank-You for the help. For those who down voted this i'm still new to php and I did research this quite a bit just need help with understanding. – Nelson Oct 08 '13 at 07:23

1 Answers1

1

Don't parse HTML with regex.

Simple function that returns all open graph metas from given url:

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;
}

Use:

$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;
Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107
  • Thank-You for the response Glavic, However when I inserted your code it returned with this error `Parse error: syntax error, unexpected '[' in C:\UwAmp\www\tos.php on line 4` – Nelson Oct 08 '13 at 07:37
  • Sorry, PHP>=5.4 syntax ;) I changed array from `[]` to `array()`. Try now. – Glavić Oct 08 '13 at 07:38
  • Ah Sorry, I didn't see your updated code. You're my hero haha. Thankyou for the help :3 – Nelson Oct 08 '13 at 07:39