Friday 24 May 2019

How to download youtube video in PHP?

How to download youtube video in PHP?

Question: How to Get the YouTube Video Id?
Suppose we have following YouTube URL:
https://www.youtube.com/watch?v=iHp5veCNPVw
iHp5veCNPVw is YouTube Id.



Question: How to download YouTube using YouTube video Id?
$vid = 'iHp5veCNPVw'; //the youtube video ID
$vformat = 'video/mp4'; //video/mp4, video/webm, video/mp3 et.
$fileName = 'downloadvideos';//File name, downloadvideos.mp4
parse_str(file_get_contents("http://youtube.com/get_video_info?video_id=" . $vid), $info); //decode the data
$streams = $info['url_encoded_fmt_stream_map']; //the video's location info
$streams = explode(',', $streams);
foreach ($streams as $stream) {
    parse_str($stream, $data); //decode the stream
    if (stripos($data['type'], $vformat) !== false) { //We've found the right stream with the correct format
        $video = fopen($data['url'] . '&signature=' . @$data['sig'], 'r'); //the video
        $fullFileName = $fileName . '.' . str_replace('video/', '', $vformat);
        $file = fopen($fullFileName, 'w');
        stream_copy_to_stream($video, $file); //copy it to the file
        fclose($video);
        fclose($file);
        echo 'Downloaded Path:  ' . __DIR__ . '/' . $fullFileName;
        break;
    } else {
        echo "Unable to download this video.";
    }
}



Friday 10 May 2019

Zend 3 Interview Questions and Answers - Page 2

Zend 3 Interview Questions and Answers

Question: What is Hydrators?
Hydrators are used to create objects from different PHP array formats.
The main feature of each hydrator is a link between two separate layers, so that they are not cluttered up with unnecessary mappings, validations or filters in the classes, models etc.


Question: How to add filter in Zend3?
You can use Zend's HtmlEntities for filter the user data.
For Example:
$filter = new Zend\Filter\HtmlEntities();
print $filter->filter('<');



Question: How to set the UTC timezone for Zend?
Put the following code in public/index.php
date_default_timezone_set('UTC');



Question: What is Service Locator Pattern?
The service locator design pattern is used "Encapsulate the processes involved in obtaining a service with a strong abstraction layer".
It return the instances of services when they are requested for by the service consumers.

Following are the Design components of Service Locator Pattern.
Service Locator: It lookup services,dependencies,business object provides a simple interface to clients.
InitialContext: The InitialContext object is the start point in the lookup and creation process.
ServiceFactory: It provides the required BusinessService objects.
BusinessService: It the main object that fullfill your requirements.


Question: What is Service Manager?
Service Manager are heavely used in zend and it is based on Service Locator design Pattern.
It is used to register a service and get the server.


Question: How to install a Service Manager component?
composer require zendframework/zend-servicemanager



Question: How to register a service in Service manager?
use Zend\ServiceManager\ServiceManager; 
use Zend\ServiceManager\Factory\InvokableFactory; 
use stdClass;  
$serviceManager = new ServiceManager([ 
   'factories' => [stdClass::class => InvokableFactory::class,], 
]);



Question: How to get the object from Service Manager?
use Zend\ServiceManager\ServiceManager;  
$object = $serviceManager->get(stdClass::class);



Question: What is eventmanager?
The zend-eventmanager helps to design "High level architecture" and supports "subject/observer pattern".

It has two importants points
Event: Its event.
Listener: Listener are attached to the events and gets called when the event is triggered.
EventInterface Class: Used to specify the event itself.
EventManager class The instance of the EventManager tracks all the defined events in an application and its corresponding listeners.