GuitarExtended
GuitarExtended

Reputation: 827

Vimeo.php : simple request not working

I'm trying to use vimeo.php on a WordPress website I'm developing. I have downloaded the library and put it in my theme's folder. I have also created an app on Vimeo API's website. And I use the code below :

// Include Vimeo's php library
require_once( "/assets/php/vimeo.php-1.3.0/autoload.php");
$client_id = 'xxxx'; //'Client identifier' in my app
$client_secret = 'xxxx'; // 'Client secrets' in my app
$lib = new \Vimeo\Vimeo($client_id, $client_secret);

function get_Vimeo(){
    $response = $lib->request('https://vimeo.com/6327777', array(), 'GET');
    return $response;
}

When I call the get_Vimeo() function I get a Fatal error: Call to a member function request() on null.

Vimeo's API is a bit obscure to me, any idea what I did wrong ?

Upvotes: 0

Views: 647

Answers (2)

GuitarExtended
GuitarExtended

Reputation: 827

Ofir Baruch's answer was very helpful, but I'm answering my own question for a more thorough fix. There is indeed a problem with the scope of $lib, but there's also an issue with the first request argument. The Vimeo.php library builds an URL like so : 'api.vimeo.com' . 'first_argument'. Here's the fixed code that works for me :

// Include Vimeo's php library
require_once( "/assets/php/vimeo.php-1.3.0/autoload.php");
$client_id = 'xxxx'; //'Client identifier' in my app
$client_secret = 'xxxx'; // 'Client secrets' in my app
$lib = new \Vimeo\Vimeo($client_id, $client_secret);

// Set the access token (from my Vimeo API app)
$lib->setToken('xxx...xxx');

function get_Vimeo(){
    global $lib;
    $response = $lib->request('/videos/6327777', array(), 'GET');
    return $response;
}

Upvotes: 0

Ofir Baruch
Ofir Baruch

Reputation: 10356

The term you're looking for is Variables Scope.

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well.

$lib is a variable which you've defined in the file. You can't access it in a function just because it's in the file, it's another scope.

Use global $lib in the function in order to access it.

Example:

$var = 'something';

function testA(){
  echo $var; //null
}

function testB(){
  global $var;
  echo $var; //something
}

Upvotes: 2

Related Questions