1

I need help printing a YouTube OpenGraph key, video:url, using the PHP OpenGraph library.

In the following code, the foreach loop works and so does $graph->site_name but $graph->video:url does not.

<?php
    require_once('OpenGraph.php');

    $graph = OpenGraph::fetch('https://www.youtube.com/watch?v=b6hoBp7Hk-A');
    print $graph->site_name;
    print $graph->title;
    print $graph->video:url;
    /*
    foreach ($graph as $key => $value) {
        print "$key => {$value}<br />";
    }
    */
?>

The above code reproduces the error:

Parse error: syntax error, unexpected ':' in line 7

How can I directly access the value for video:url and other YouTube properties with : using the OpenGraph object?

Grokify
  • 15,092
  • 6
  • 60
  • 81
Twunay
  • 11
  • 2
  • 1
    I've translated your post best I can, please try to use English, or use a translator yourself. (also, your error might be a missing colon `:`, or you meant to use `->`) – James Jun 22 '15 at 02:37

1 Answers1

0

Although YouTube returns a video:url property, you cannot retrieve it using OpenGraph as $graph->video:url but you can retrieve it using $graph->__get('video:url'). Explanation follows which examines the OpenGraph.php code.

Valid Function Names

$graph->video:url will not work because PHP does not allow : characters in function names. The following is from the PHP Manual: User-defined functions:

A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.

__get() Magic Method

In the OpenGraph object for a YouTube video, the ->site_name and ->title properties are provided by the __get() magic method which return the value matching the method name in the private ->_values associative array. Because the video:url associative array key does not conform to a valid function name, you cannot access it directly. Also, because the ->_values array is private, you cannot access it as an array. However, since the __get() magic method is public, you can access it directly like so:

$graph->__get('video:url')

Links:

Grokify
  • 15,092
  • 6
  • 60
  • 81