Reputation: 85
I tried running the sample code that Google gives you on their developer Code Sample page (https://developers.google.com/youtube/v3/code_samples/php#search_by_keyword) isn't working for me. I made sure to download the latest version of the library from GitHub, installed it to my PHP5/Apache2 server, changed the Include Path, and for some reason, one of the require_once items is not working properly. Here is the code that I have (modified only with some echo statements). I put in my developer key, but have removed it in this post for obvious reasons.
<?php
$htmlBody = <<<END
<form method="GET">
<div>
Search Term: <input type="search" id="q" name="q" placeholder="Enter Search Term">
</div>
<div>
Max Results: <input type="number" id="maxResults" name="maxResults" min="1" max="50" step="1" value="25">
</div>
<input type="submit" value="Search">
</form>
END;
// This code will execute if the user entered a search query in the form
// and submitted the form. Otherwise, the page displays the form above.
if ($_GET['q'] && $_GET['maxResults']) {
echo "echo 1";
echo "<br/>";
// Call set_include_path() as needed to point to your client library.
set_include_path(get_include_path() . PATH_SEPARATOR . '/home/pi/google-api-php-client/src');
require_once('Google/Client.php');
echo "echo 2<br/>";
require_once ('Google/Service/YouTube.php');
echo "echo 3";
echo "<br/>";
/*
* Set $DEVELOPER_KEY to the "API key" value from the "Access" tab of the
* Google Developers Console <https://console.developers.google.com/>
* Please ensure that you have enabled the YouTube Data API for your project.
*/
$DEVELOPER_KEY = 'my-api-key-here';
$client = new Google_Client();
$client->setDeveloperKey($DEVELOPER_KEY);
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
try {
// Call the search.list method to retrieve results matching the specified
// query term.
$searchResponse = $youtube->search->listSearch('id,snippet', array(
'q' => $_GET['q'],
'maxResults' => $_GET['maxResults'],
));
$videos = '';
$channels = '';
$playlists = '';
// Add each result to the appropriate list, and then display the lists of
// matching videos, channels, and playlists.
foreach ($searchResponse['items'] as $searchResult) {
switch ($searchResult['id']['kind']) {
case 'youtube#video':
$videos .= sprintf('<li>%s (%s)</li>',
$searchResult['snippet']['title'], $searchResult['id']['videoId']);
break;
case 'youtube#channel':
$channels .= sprintf('<li>%s (%s)</li>',
$searchResult['snippet']['title'], $searchResult['id']['channelId']);
break;
case 'youtube#playlist':
$playlists .= sprintf('<li>%s (%s)</li>',
$searchResult['snippet']['title'], $searchResult['id']['playlistId']);
break;
}
}
$htmlBody .= <<<END
<h3>Videos</h3>
<ul>$videos</ul>
<h3>Channels</h3>
<ul>$channels</ul>
<h3>Playlists</h3>
<ul>$playlists</ul>
END;
} catch (Google_ServiceException $e) {
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
} catch (Google_Exception $e) {
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage()));
}
}
?>
<!doctype html>
<html>
<head>
<title>YouTube Search</title>
</head>
<body>
<?=$htmlBody?>
</body>
</html>
with the interesting part being here
echo "echo 1";
echo "<br/>";
// Call set_include_path() as needed to point to your client library.
set_include_path(get_include_path() . PATH_SEPARATOR . '/home/pi/google-api-php-client/src');
require_once('Google/Client.php');
echo "echo 2<br/>";
require_once ('Google/Service/YouTube.php');
echo "echo 3";
echo "<br/>";
When I run the page, the output is this
echo 1
echo 2
What's happening with the second require_once statement that causes the page to stop running after that? At one point I had an echo statement immediately following the set_include_path(), but that seemed to be working properly, so I removed it.
I solved the issue by adding all dependencies manually. After adding all required dependencies, this is what I ended up with.
require_once ('Google/Logger/Null.php');
require_once('Google/Client.php');
require_once ('Google/Config.php');
require_once ('Google/Model.php');
require_once ('Google/Collection.php');
require_once ('Google/Service.php');
require_once ('Google/Service/Resource.php');
require_once ('Google/Service/YouTube.php');
Upvotes: 0
Views: 3205
Reputation: 36
You can add
require_once 'include/Google/autoload.php';
in front of code, and delete all other require like:
//require_once 'include/Google/Client.php';
//require_once 'include/Google/Service/YouTube.php';
But the php version should greater than 5.3
Upvotes: 2
Reputation: 12877
Looks like you didn't set your PHP's include path correctly. If you add this client library's src/ file into include path, it should just work fine.
Upvotes: 0
Reputation: 2538
Some things you can try:
Turn on PHP's error output. If an error occurs, you get an information in the resulting html. You can find more information on how to enable it in this thread.
Make sure Google/Service/YouTube.php
exists. Navigate to the folder and check manually.
Print the include path before including YouTube.php
(I don't actually think this could be the problem, but who knows...)
Upvotes: 0