YouTube API v3 Grab Duration - php

I have a script that grabs the duration of a Youtube video.
It was working fine but for some reason it now gets the following error:
Warning:
file_get_contents(https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=Prt-G4cPIn4&key=[API_KEY]):
failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
Here's the code:
$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$video_id&key=$apikey");
$duration = json_decode($dur, true);
foreach ($duration['items'] as $vidTime) {
$vTime = new DateInterval($vidTime['contentDetails']['duration']);
}
$vid_time = $vTime->format('%H:%I:%S');
I have no idea why this would stop working all of a sudden.

The var $apikey is wrong "[API_KEY]", solve this first and go appear other error, comming by the headers and CORS

Related

file_get_contents Returns NULL

I am trying to fetch some data like first name, last name from LinkedIn. I am using the below code for the same. But I am getting NULL in response. I tried some solutions from SO and other websites but none of them worked.
Below is the code I am using:-
$url = 'http://api.linkedin.com/v1/people/~:(id,first-name,last-name,picture-urls::(original),headline,public-profile-url,location,industry,positions,email-address)?format=json&outh2_access_token='.$accessToken;
$user = file_get_contents($url,false);
var_dump(json_decode($user));
die();
Below is the response that I am getting.
[16-Jul-2018 12:27:20 Europe/London] PHP Warning: file_get_contents(http://api.linkedin.com/v1/people/~:(id,first-name,last-name,picture-urls::(original),headline,public-profile-url,location,industry,positions,email-address)?format=json&outh2_access_token=###########): failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized
Basically what I want is to fetch the data from LinkedIn using php, if anybody has done this before please share the solution with me.

PHP file_get_contents problems with $_GET

I have a problem with my website. My website gets a variable and returns a String. What I mean by that is that if you visit my website the code will me only a 3 digit String eg FEG, PSJ, FGT, HJK. I want to take this String using the file_get_contents function. But when I do $con = file_get_contents("http://website.com/test.php?name=George"); it gives me this error
Warning: file_get_contents("http://website.com/test.php?name=George"): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in C:\xampp\htdocs\test.php on line 2
Example using cURL instead:
<?php
$ch = curl_init("http://website.com/test.php?name=George");
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>

Error handling file get contents

I am trying to create a simple script with file_get_contents and Facebook graph. However, there seems to be some issue with the error handling.
Here is the code I have:
$json = file_get_contents('https://graph.facebook.com/', true);
if($json !== FALSE) {
echo "test";
}
It returns the following error message:
Warning: file_get_contents(https://graph.facebook.com/): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home/test/tst/mellemmand/test.php on line 4

Google Changed Keyword Tool URL?

I've been using the following code to scrape keywords from Google:
$data=file_get_contents('http://clients1.google.com/complete/search?hl=en&gl=us&q='.$keyword);
However, my script has suggest started showing these errors:
Warning: file_get_contents(http://clients1.google.com/complete/search?hl=en&gl=us&q=money) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home/username/public_html/keywords.php on line 10
I'm guessing this is being caused by Google changing the link? Does anyone know what the new link would be or what I need to change in my code?
Try this:
$url = 'http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en-US&q=';
$data=file_get_contents($url . urlencode( $keyword ) );
Hope it helps.

YouTube XML Feed Error

I've got a page on a website that's pulling my favorites feed from YouTube and embedding them into site.
The problem is it's working 80% of the time, but the other 20% of the time i'm getting errors on the page - nothing in the code is changing to cause this, so i'm wondering what might be causing this, or if there's a better way to do what i'm doing...
The Error I'm gettings is a 403 Forbidden when retrieving the XML feed... here's what it looks like (note: the line numbers won't match exactly, because i've simplified the code sample below.
The XML feed in question is here:
https://gdata.youtube.com/feeds/api/users/umarchives/favorites
Warning: simplexml_load_file(https://gdata.youtube.com/feeds/api/users/umarchives/favorites) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 42
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "https://gdata.youtube.com/feeds/api/users/umarchives/favorites" in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 42
Warning: Invalid argument supplied for foreach() in /mnt/stor3-wc2-dfw1/web/content/videos.php on line 47
Here's the code i'm using:
<?php
// set feed URL
$YouTubeUsername = "umarchives";
$feedURL = "https://gdata.youtube.com/feeds/api/users/".$YouTubeUsername."/favorites";
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
$attrs = $media->group->content->attributes();
$videoURL = $attrs['url'];
$videoURL = preg_replace('/\?.*/', '', $videoURL);
$videoURL = str_replace("/v/","/embed/",$videoURL);
$videoTitle = $media->group->title;
echo "<iframe class='youtube-player' width='300' height='225' src='$videoURL'></iframe>\n";
echo "<br>\n";
}
?>
You should be validating the result of $sxml = simplexml_load_file($feedURL); per the Google error validation docs. Then you can print out the actual message that comes along with the 403 code, or possibly decide to retry the request. If it's a random occurrence my guess is a quota limit issue, but the actual error information will likely tell you exactly what you want to know.
MYUSERNAME is not a valid username. Add your own youtube username!
When I call your feed URL in browser (https://gdata.youtube.com/feeds/api/users/wfptv/favorites) I receive this error:
Favorites of requested user are not public.
Make your feed public, and the failure should be gone.

Categories