Code Monkey home page Code Monkey logo

php-youtube-api's Introduction

php-youtube-api

Build Status

A basic PHP wrapper for the Youtube Data API v3 ( Non-OAuth ). Designed to let devs easily fetch public data (Video, Channel, Playlists info) from Youtube. No 3rd party dependancy (except PHPUnit). The reason for returning the decoded JSON response directly, is that you only need to read the Google API doc to use this library, instead of learning my set of API again (Keep it simple).

Well.. actually some parameters are missing in this library, because I don't need them at this point. If you desire a particular feature, please file an issue here :)

Currently I will not consider adding OAuth endpoints. (those required "authorized request" will not be supported)

Requirements

  • PHP >=5.3
  • CURL extension in PHP

Install

Run the following command in your command line shell for your php project

$ composer require madcoda/php-youtube-api:^1.2

Done.

You may also edit composer.json manually then perform composer update

"require": {
    "madcoda/php-youtube-api": "^1.2"
}

Getting started

Please read the wiki on how to use this library with PHP with composer, Laravel 4 and Laravel 5.

For the functions implemented in this library, please visit API Reference

Example usage with pure PHP (with composer)

require 'vendor/autoload.php';
$youtube = new Madcoda\Youtube\Youtube(array('key' => '* Your API key here *'));
$video = $youtube->getVideoInfo('rie-hPVJ7Sw');

Example usage with Laravel 4/5

$video = Youtube::getVideoInfo(Input::get('vid', 'dQw4w9WgXcQ');

Format of returned data

The returned json is decoded as PHP objects (not Array). Please read the "Reference" section of the official API doc.

YouTube Data API v3

Contact

For bugs, complaints, or suggestions, please file an Issue here or send email to [email protected] :)

License

This repo madcoda/php-youtube-api is licensed under the MIT License.

php-youtube-api's People

Contributors

faenir avatar hackwar avatar jamesggordon avatar jbboehr avatar jk avatar konybakk avatar lorenzosapora avatar madcoda avatar marvin255 avatar mhor avatar peter279k avatar primus852 avatar saruman avatar sidhitesh7 avatar stichoza avatar tambait avatar wlkns avatar wyrihaximus avatar z38 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-youtube-api's Issues

I get a message from google

I get this message in the page when i add the API key to the config in the package

Error 403 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. : ipRefererBlocked

Fatal Error

Fatal error: Uncaught exception 'Exception' with message 'Curl Error : SSL certificate problem: unable to get local issuer certificate' in C:\xampp\htdocs\php-youtube-api-master\lib\Madcoda\Youtube.php:475 Stack trace: #0 C:\xampp\htdocs\php-youtube-api-master\lib\Madcoda\Youtube.php(75): Madcoda\Youtube->api_get('https://www.goo...', Array) #1 C:\xampp\htdocs\php-youtube-api-master\index.php(10): Madcoda\Youtube->getVideoInfo('rie-hPVJ7Sw') #2 {main} thrown in C:\xampp\htdocs\php-youtube-api-master\lib\Madcoda\Youtube.php on line 475

Class not found error.

I got a problem with the lib when I tried to use it with Symfony 2.3. The problem was that the filename of class was "youtube.php" and I had to rename it to "Youtube.php".

Request all videos with automatic nextToken

Hi,
I've created a new function to retrieve all videos with automatic token!

Check the code below:

private $next = array();
private $playItemsArray = array();

public function getAllPlaylistItemsByPlaylistId($playlistId,$maxResults=50,$pageToken=""){
    $API_URL = $this->getApi('playlistItems.list');
    $params = array(
        'playlistId' => $playlistId,
        'part' => 'id, snippet, contentDetails, status',
        'maxResults' => $maxResults,
        'pageToken' => $pageToken
    );
    $apiData = $this->api_get($API_URL, $params);
    return $this->decodePlayList($apiData,$playlistId);
}


        public function decodePlayList(&$apiData,$id){
    $resObj = json_decode($apiData);
    if(isset($resObj->error)){
        $msg = "Error ".$resObj->error->code." ".$resObj->error->message;
        if(isset($resObj->error->errors[0])){
            $msg .= " : " . $resObj->error->errors[0]->reason;
        }
        throw new \Exception($msg);
    }else{
        $itemsArray = $resObj->items;
        if(!is_array($itemsArray) || count($itemsArray) == 0){
            return FALSE;
        }else{
        if (isset($resObj->nextPageToken)) {
        array_unshift ($this->next, $resObj->nextPageToken);
        $this->playItemsArray = array_merge($this->playItemsArray,$itemsArray);
        self::getAllPlaylistItemsByPlaylistId($id,50,$this->next[0]);

        } else {
        $this->playItemsArray = array_merge($this->playItemsArray,$itemsArray);
            }
        return $this->playItemsArray;
        }
    }
}

I've checked and this function just consume 60 Google Developer points (with 300 videos) and take around 35 seconds to run !

If you guys have another better way to do it tell or post it here!

This is very powerful when you want to get all videos 🎯

Best Regards,

Peterson Tubini

I am getting Curl SSL Certificate Problem

I tried making an api request but currently I am having an issue with CURL

Fatal error: Uncaught exception 'Exception' with message 'Curl Error : SSL certificate problem: unable to get local issuer certificate'

Laravel 5.3?

I am doing the installation according to the documentation and it does not work

FatalThrowableError in YoutubeController.php line 11:
Class 'App\Http\Controllers\Youtube' not found;

help!

not getting page info into advance search method searchAdvanced()

use following example

use Madcoda\Youtube;

$youtube = new Youtube(array('key' => '/* Your API key here */'));

// Set Default Parameters
$params = array(
'q' => 'Android',
'type' => 'video',
'part' => 'id, snippet',
'maxResults' => 50
);

// Make Intial Call. With second argument to reveal page info such as page tokens.
$search = $youtube->searchAdvanced($params, true);

Request: Get Playlist Items by Playlist Id

Not sure if you want to add features to this. Will wait for your feedback. Just added the following and tested here:

var $APIs = array(
        // added to $APIs array
        'playlistItems.list' => 'https://www.googleapis.com/youtube/v3/playlistItems'
    );

public function getPlaylistItemsByPlaylistId($playlistId){
        $API_URL = $this->getApi('playlistItems.list');
        $params = array(
            'playlistId' => $playlistId,
            'part' => 'id, snippet, contentDetails, status',
            'maxResults' => 50
        );
        $apiData = $this->api_get($API_URL, $params);
        return $this->decodeList($apiData);
    }

Request: Optional params array can be passed to getPlaylistsByChannelId()

Hi,

This library seems great and I've just started using it, thanks! I was able to get my playlists by ID but the Youtube API default is 5, but there were much more than that in the account (however, less than the API maximum of 50)

Could this method take an optional params array as a second argument? I.e.

public function getPlaylistsByChannelId($channelId, $optionalParams = array()){
        $API_URL = $this->getApi('playlists.list');
        $params = array(
            'channelId' => $channelId,
            'part' => 'id, snippet, status'
        );
        if ($optionalParams) {
            $params = array_merge($params, $optionalParams);
        }
        $apiData = $this->api_get($API_URL, $params);
        return $this->decodeList($apiData);
    }

Using array_merge() we can also override your defaults if necessary. I've just hardcoded this into the class for now, let me know what you think.

Reference: https://developers.google.com/youtube/v3/docs/playlists/list

Curl Error

Curl Error : SSL certificate problem: unable to get local issuer certificate

get mostpopular video.list

how to get mostpopular video.list, seems like in your source code no method that accept params in video.list

Laravel 5 Auto discover

Hi

since laravel 5 support auto discover package we don't need to add the service provider and the facade manually
(https://laravel.com/docs/5.8/packages#package-discovery)

juste add to the composer.json :
"extra": { "laravel": { "providers": [ "Madcoda\\Youtube\\YoutubeServiceProviderLaravel5" ], "aliases": { "Youtube": "Madcoda\\Youtube\\Facades\\Youtube" } } }

another suggestion is to add the group for publish (to use it with php artisan vendor:publish tag=)

$this->publishes([ __DIR__.'/config/youtube.php' => config_path('youtube.php'), ], 'youtube-api');

'part' optional params

This library has everything I am looking for, however, would it be possible to support passing parts as an optional parameter?

I ask because I only would need the snippet part (2 quota) but your default call for Youtube#getVideoInfo() calls 6 different parts (8 quota per call). Quota is precious with YT, so I'd like to be able to use as little as possible when it is feasible.

Thanks!

Serving Limit Exceeded

exception 'Exception' with message 'Error 403 Serving Limit Exceeded : servingLimitExceeded' in /var/www/vendor/madcoda/php-youtube-api/src/Madcoda/Youtube.php:537

need 'return false' or other error for dont die process and repeat via 5-30 seccond, if limit exceeded for controller

ps: sorry for my bad english

relatedToVideoId

2015-12-27 17 36 54
if you set relatedToVideoId not need set q,
but searchAdvanced want's

Returntype invalid

Namespace: \Madcoda\Youtube\Youtube
Method: getVideosInfo()

The Docblock of Method getVideosInfo say, the return value is always \StdClass.
First the type call \stdClass (lowercase s) and secondary in the method will be the method \Madcoda\Youtube\Youtube::decodeList called. There it can be return \stdClass OR array.

Docblock getVideoInfo()

    /**
     * @param $vIds
     * @return \StdClass
     * @throws \Exception
     */

Docblock decodeList()

    /**
     * Decode the response from youtube, extract the list of resource objects
     *
     * @param  string $apiData response string from youtube
     * @throws \Exception
     * @return array Array of StdClass objects
     */

The correct Docblock sould be looks like this:
Docblock getVideoInfo()

    /**
     * @param array|string $vIds
     * @return \stdClass|array
     * @throws \Exception
     */

Docblock decodeList()

    /**
     * Decode the response from youtube, extract the list of resource objects
     *
     * @param  string $apiData response string from youtube
     * @throws \Exception
     * @return \stdClass|array Array of StdClass objects
     */

Fatal error: Class 'PHPUnit_Framework_TestCase'

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /opt/lampp/htdocs/utube/tests/YoutubeTest.php on line 14

can i have clear documentation on how to get started with this github project and list youtubr videos and i am getting the above mention error

error

[10-Apr-2015 05:38:32] WARNING: [pool jugme] child 1760 said into stderr: "NOTICE: PHP message: PHP Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/jugme/www/lib/Youtube.php on line 473"

In php-fpm
php_value[max_execution_time] = 0

How can I get this to run in just PHP?

I can't figure out exactly how this is meant to be loaded in pure PHP. Which file in your ZIP needs to be included?

I'm assuming I can't use the use Madcoda... line as I get a fatal error.

Please give some easier tutorials to get started,.

New method: getAllPlaylistItemsByPlaylistId()?

Hey here I have written a method to get all playlist items by playlist id and was wondering whether you would like me to create a pull request; Unsure whether extending new features is part of the scope of this project.

How to refresh the api key ?

I use an api key generated from google console but it expires every 24 hours, How can i make it valid for ever or for long time ?

Thanks

Will be better using a real object that you hydrate

Hello,

This is just a suggestion. But what if you can't retrieve all the data you hope from the api ? You can have critical error trying to get information.

I think it could be better using a simple class that you hydrate (as doctrine do for entities, for example).

What do you think about ?

Server error

Hi,
I faced this error today. please have a look.

Error 400 'invideo_promotion' : unexpectedPart

Library seems limited

This library seems limited. For example, I want to fetch my public uploads. Looking through the public methods in the class I can’t do this. I can’t even craft my own request, because the api_get() method’s visibility is set to private.

It would be good if more public methods were created for all scenarios, or the visibility of the api_get() method being changed to public so developers could craft their own requests where the library is unable to offer its own, built-in shortcut method.

Pagination for getPlaylistItemsByPlaylistId

You could add the pagination for this property. I managed to do it by adding the token argument like in the paginateResults function and returning the array with results and info.

public function getPlaylistItemsByPlaylistId($playlistId, $maxResults = 50, $token = null)
{
...
if (!is_null($token)) $params['pageToken'] = $token;
...
return array(
            'results' => $this->decodeList($apiData),
            'info'    => $this->page_info
        );
}

Thank you

I just wanted to say thank you for making an awesome api. 👍 :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.