Reputation: 93
I have a simple wordpress plugin which should fetch information from Vimeo. I was trying to fetch video duration metadata from vimeo using their "simple api" with the following code:
function vimeo_duration ($id) {
try {
$ch = curl_init("http://vimeo.com/api/v2/video/$id/json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
$obj = json_decode($res, true);
return $obj[0]['duration'];
} catch (Exception $e) {
# returning 0 if the Vimeo API fails for some reason.
return '0';
}
}
Result: Whitespace. It's not fetching anything and there's no "0" as well.
This link is the one we're using to fetch the information from: http://vimeo.com/api/v2/video/244509537/json , and it gives the following results when you load it in your browser:
(The end 'json' can be replaced with "/php" or"/xml" and it will give the result in the corresponding format.)
However I found out that this api is no longer supported and I'll have to use the new Api to fetch the duration data that I want. (perhaps that's why it's not fetching the data)
I've searched through StackOverflowa and other sources and I can't find a working example of this. I've also read through the vimeo documentation, however I can't implement it. I've created the API as they suggest, and now have the Access Token.
I've used this code which I've found in StackOverflow and modified it a little. However it doesn't return any values. The results are again Whitespace. It's not fetching anything and it's not returning "0" as well.
function vimeo_duration ($id) {
try {
$authorization = 'your_vimeo_api_authorization_token_goes_here';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://api.vimeo.com/videos/$id",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer {$authorization}",
"cache-control: no-cache",
),
));
$res = curl_exec($ch);
$obj = json_decode($res, true);
return $obj[0]['duration'];
} catch (Exception $e) {
# returning 0 if the Vimeo API fails for some reason.
return "0";
}
}
Can someone help with this code? I see other people are interested in using the new API to fetch different kinds of information but there are not many working examples here, and plenty of unanswered questions.
Upvotes: 1
Views: 2977
Reputation: 3212
Try this:
function vimeo_duration ($id) {
try {
$authorization = 'your_vimeo_api_authorization_token_goes_here';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://api.vimeo.com/videos/$id?fields=duration",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer {$authorization}",
"cache-control: no-cache",
),
));
$res = curl_exec($ch);
$obj = json_decode($res, true);
return $obj['duration'];
} catch (Exception $e) {
# returning 0 if the Vimeo API fails for some reason.
return "0";
}
}
Changes I made:
https://api.vimeo.com/videos/$id
to https://api.vimeo.com/videos/$id?fields=duration
. This tells our API to only pull back duration
for you. The result is faster responses since we aren't processing data you don't use, and also higher rate limits. See https://developer.vimeo.com/api/common-formats#json-filter for more information.return $obj[0]['duration'];
is now return $obj['duration'];
. Since you're querying a single video, the resultset is an associative array.Running these changes with the following, I receive back 9
:
print_r([
'duration' => vimeo_duration(245823639)
]);
Upvotes: 5