Youtube API - wyciąganie informacji o video - JS działa a PHP nie

0

Witam
Chcę "wyłuskać" informację z youtube informacje za pomocą Youtube API. Stworzyłem projekt, dostałem klucz i kopiując skrypt z tej strony ...

http://salman-w.blogspot.com/2010/01/retrieve-youtube-video-title.html

... po zmianie klucza na swój wszystko zadziałało.

<html>
<head>
	<title>YouTube: Retrieve Title, Description and Thumbnail</title>
	<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
	<style>
		#search-bar { margin: 1em 0; overflow: hidden; }
		#search-txt { float: left; width: 60%; }
		#search-btn { float: right; width: 39%; }
		#video-data-1 img { float: right; }
		#video-data-1 p { white-space: pre-line; }
	</style>
</head>
<body>
	<p>Enter YouTube Video ID or URL in the text box below</p>
	<div id="search-bar">
		<input id="search-txt" type="text" value="http://www.youtube.com/watch?v=gzDS-Kfd5XQ" maxlength="100">
		<input id="search-btn" type="button" value="Fetch Video Information">
	</div>
	<div id="video-data-1"></div>
	<ul id="video-data-2"></ul>
	<script>
		/*
		 * YouTube: Retrieve Title, Description and Thumbnail
		 * http://salman-w.blogspot.com/2010/01/retrieve-youtube-video-title.html
		 */
		$(function() {
			$("#search-txt").on("keypress", function(e) {
				if (e.which === 13) {
					e.preventDefault();
					$("#search-btn").trigger("click");
				}
			});
			$("#search-btn").on("click", function() {
				$("#video-data-1, #video-data-2").empty();
				var videoid = $("#search-txt").val();
				var matches = videoid.match(/^http:\/\/www\.youtube\.com\/.*[?&]v=([^&]+)/i) || videoid.match(/^http:\/\/youtu\.be\/([^?]+)/i);
				if (matches) {
					videoid = matches[1];
				}
				if (videoid.match(/^[a-z0-9_-]{11}$/i) === null) {
					$("<p style='color: #F00;'>Unable to parse Video ID/URL.</p>").appendTo("#video-data-1");
					return;
				}
				$.getJSON("https://www.googleapis.com/youtube/v3/videos", {
					key: "AIzaSyBdFokFzk-gnW5q46_sN9CuU0PsMTI8uJo",
					part: "snippet,statistics",
					id: videoid
				}, function(data) {
					if (data.items.length === 0) {
						$("<p style='color: #F00;'>Video not found.</p>").appendTo("#video-data-1");
						return;
					}
					$("<img>", {
						src: data.items[0].snippet.thumbnails.medium.url,
						width: data.items[0].snippet.thumbnails.medium.width,
						height: data.items[0].snippet.thumbnails.medium.height
					}).appendTo("#video-data-1");
					$("<h1></h1>").text(data.items[0].snippet.title).appendTo("#video-data-1");
					$("<p></p>").text(data.items[0].snippet.description).appendTo("#video-data-1");
					$("<li></li>").text("Published at: " + data.items[0].snippet.publishedAt).appendTo("#video-data-2");
					$("<li></li>").text("View count: " + data.items[0].statistics.viewCount).appendTo("#video-data-2");
					$("<li></li>").text("Favorite count: " + data.items[0].statistics.favoriteCount).appendTo("#video-data-2");
					$("<li></li>").text("Like count: " + data.items[0].statistics.likeCount).appendTo("#video-data-2");
					$("<li></li>").text("Dislike count: " + data.items[0].statistics.dislikeCount).appendTo("#video-data-2");
				}).fail(function(jqXHR, textStatus, errorThrown) {
					$("<p style='color: #F00;'></p>").text(jqXHR.responseText || errorThrown).appendTo("#video-data-1");
				});
			});
		});
	</script>

</body>
</html>

Jednak chcę to zrobić przy użyciu PHP. Spróbowałem coś takiego.

 
$json=file_get_contents("https://www.googleapis.com/youtube/v3/videos?key=AIzaSyBdFokFzk-gnW5q46_sN9CuU0PsMTI8uJo&part=snippet,statistics&id=j3dfCPS9Ypc");
	//$json = file_get_contents('http://gdata.youtube.com/feeds/api/videos/gzDS-Kfd5XQ?v=2&alt=json');
	$obiekt=json_decode($json);
	
	var_dump($json);

Niestety nie działa i nie wiem dlaczego. Googlałem trochę i ludzie używają takiego samego kodu a ja nawet pliku nie mogę odczytać. Oczywiście jak widać klucze są te same.

UWAGA!
Klucz jest ustawiony na moją stronę, jak będziecie próbować używajcie swojego lub dajcie znać na priv jaką domenę mam dodać.

0

Sprawdź pod konsolą, co tak naprawdę wysyła przeglądarka.

0

Już sobie poradziłem, chodziło o https i funkcję file_get_contents(). Ludzie w internecie radzą że trzeba takie rzeczy robić "po Bożemu" czyli przy pomocy CURL

$url = 'https://www.googleapis.com/youtube/v3/videos?key=AIzaSyBdFokFzk-gnW5q46_sN9CuU0PsMTI8uJo&part=snippet,statistics&id=yZj_EC8C5Ng';
	//$url = 'https://www.googleapis.com/youtube/v3/liveBroadcasts?key=AIzaSyBdFokFzk-gnW5q46_sN9CuU0PsMTI8uJo&part=snippet,statistics&id=yZj_EC8C5Ng';
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_HEADER, false);
	$json = curl_exec($curl);
	curl_close($curl);

	$obiekt=json_decode($json);
	echo '<h1>'.$obiekt->items[0]->snippet->title.'</h1>';
	echo '<img src="'.$obiekt->items[0]->snippet->thumbnails->standard->url.'"/>';
	echo '<p>'.$obiekt->items[0]->snippet->description.'</p><br />';
	echo '<h3>TAGI:</h3>';
	echo '<p>';
	foreach ($obiekt->items[0]->snippet->tags as $items)	{
		echo $items.', ';
	}
	
	echo'</p>';
	echo $obiekt->items[0]->snippet->liveBroadcastContent;
	
	
	echo '<br /><br /><br />';var_dump($obiekt);
 

Co prawda działa ale musiałem wyłączyć restrykcje co do stron. Jeżeli włączę restrykcję umieszczając domenę .przyklad.pl/ do listy dopuszczonych domen a później odpale skrypt z adresu http://przyklad.pl/error.php rezultat vardumpa mam taki

object(stdClass)#1 (1) { ["error"]=> object(stdClass)#2 (3) { ["errors"]=> array(1) { [0]=> object(stdClass)#3 (4) { ["domain"]=> string(11) "usageLimits" ["reason"]=> string(16) "ipRefererBlocked" ["message"]=> string(254) "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed." ["extendedHelp"]=> string(37) "https://console.developers.google.com" } } ["code"]=> int(403) ["message"]=> string(254) "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed." } }

A z poprzedniego skryptu JQuery oczywiście działało bez względu na to czy włączałem blokadę czy nie

0

@doman18: no to albo dodaj IP, albo zapodaj nagłówek Referer z domeną twojej strony tym curlem

0

Już wcześniej dodawałem IP. Dodanie referera nie pomogło

 curl_setopt($curl, CURLOPT_REFERER, 'http://przyklad.pl/');

Poza tym mimo wszystko dziwne że JS działa bez dodawania refererów i ip

0

Bo z JS nie weryfikuje IP, a referer dodaje przeglądarka z automatu

0

Czyli nie ma możliwości żeby to działało na PHPie?

0

Skoro jest API do PHP to na pewno działa, tylko coś robisz źle ;) Ja nie mam czasu aż tak wchodzić w szczegóły, więc zostaje czekać na kogoś innego.

1 użytkowników online, w tym zalogowanych: 0, gości: 1