Showing posts with label Zend Framework. Show all posts
Showing posts with label Zend Framework. Show all posts

Friday, 22 November 2019

How to setup different configs in server (development, production)?

How to implement different configs for development, production?

Question: How to implement different configs for development, production etc?
  1. Added following line in .htacess (Location: /public/.htaccess)
    SetEnv APPLICATION_ENV development
    This is for development.
  2. Open application.config.php in /config/application.config.php
  3. Get the current environment.
    $applicationEnv = getenv('APPLICATION_ENV');

    Dynamic the file like below:
            'config_glob_paths' => array(         
                "config/autoload/{,*.}{$applicationEnv}.php"
            )
  4. Create the config files like below in /config/autoload
    development.php
    production.php
    
  5. Below are the sample of config file.
    return array(
        'db' => array(
            'driver' => 'Pdo',
            'dsn' => 'mysql:dbname=DBNAME;host=HOST',
            'username' => 'DB_USERNAME',
            'password' => 'DB_PASSWORD'
        )
    );
    Please update the database credentials.

Wednesday, 5 June 2019

Zend Framework 3 Authentication

Zend Framework 3 Authentication

use Zend\Authentication\Adapter\DbTable\CredentialTreatmentAdapter as AuthAdapter;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Storage\Session as SessionStorage;
            
$authAdapter = new AuthAdapter(
    $this->adapter,
    'admins',
    'email',
    'password',
     'md5(?)'
);

$authAdapter
    ->setIdentity($userName)
    ->setCredential($password);
$authResult = $authAdapter->authenticate();

if($authResult->isValid()){
    $result['success']=1;
    $identity=$authResult->getIdentity();

    //Save in session
    $adminDetails=$authAdapter->getResultRowObject(array('id','email'));
    $authService = new AuthenticationService();
    $authService->setStorage(new SessionStorage('AdminLogin'));
    $authService->getStorage()->write((array)$adminDetails);
}else{
    $messages = $authResult->getMessages();
    $result['msg'] =implode(' ',$messages);
}
    


HelpFul Links
https://github.com/zendframework/zend-authentication
https://github.com/zendframework/zend-authentication/blob/master/src/AuthenticationService.php

Monday, 6 May 2019

Zend 3 Interview Questions and Answers

Zend 3 Interview Questions and Answers

Question: How to print MySql query in zend framework?
$select = $this->sql->select();
echo $select->from('users')
        ->columns(array('id', 'first_name', 'last_name', 'email', 'status', 'total_events' => (`10`)))
        ->where($conds)
        ->order($orderBy);



Question: What are different type of route?
Literal routes: a literal route is one that exactly matches a specific string.
Segment routes: Segment routes allow you to define routes with variable parameters.


Question: How to disabled layout from controller in Zend Framework 3?
$view= new ViewModel();
$view->setTerminal(true);
return $view;



Question: How to print Sql Query in Zend3?
echo  $select->getSqlString($this->sql->getAdapter()->getPlatform(),\Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);



Question: Give example of Inner join, Left Join and Right join?
Inner join
$select = $this->sql->select();
$select->from(array('u'=>'users'))
        ->columns(array('id','email'))
        ->join(array('eq'=>'event_qualifiers'),"eq.user_id=u.id",array('total_events','event_type'));


Left join
$select = $this->sql->select();
$select->from(array('u'=>'users'))
        ->columns(array('id','email'))
        ->join(array('eq'=>'event_qualifiers'),"eq.user_id=u.id",array('total_events','event_type'),$select::JOIN_LEFT);


Right join
$select = $this->sql->select();
$select->from(array('u'=>'users'))
        ->columns(array('id','email'))
        ->join(array('eq'=>'event_qualifiers'),"eq.user_id=u.id",array('total_events','event_type'),$select::JOIN_RIGHT);


Question: How to autoload new module?
Once you have created the file structure for new module
1) Open the composer.json file.
2. Add the following line in psr-4 under autoload.
"Newmodule\\": "module/Newmodule/src/"


like below
"autoload": {
    "psr-4": {
        "Application\\": "module/Application/src/",
        "Album\\": "module/Album/src/"
    }
}



3. Execute following command
composer dump-autoload



Question: What are two components for databases?
A) One approach is to have "model classes" like Album represent each entity in your application
and then use mapper objects that load and save entities to the database.
B) Object-Relational Mapping (ORM) technology, such as Doctrine or Propel.


Question: What is use of config/modules.config.php?
We tell the ModuleManager that what module need to load with use of modules.config.php.


Question: What is Zend engine?
Zend Engine is a set of various components, which is used internally by PHP as a compiler.


Question: What are Decorators in the Zend framework?
Zend framework utilizes the decorator pattern to render elements and forms.


Question: What are Plugins in the Zend framework?
Zend Framework makes heavy use of plugin architectures.
Plugins allow for easy extensibility and customization of the framework while keeping your code separate from Zend Framework's code.


Question: How to check post method in zend framework?
if($this->getRequest()->isPost()){
}



Question: How to get post method in zend framework?
$postData = $this->getRequest()->getPost();



Question: How to get all GET data?
$this->getRequest()->getParams();



Question: How to redirect to another page from controller?
$this->_redirect("/users/login");



Question: What is Event Manager?
It gives the ability to create event based programming. This helps to create, inject and manage new events.


Question: What is Service Manager?
It gives the ability to consume any services (PHP classes) from anywhere with a little effort.


Question: What is Module Manager?
Ability to convert a collection of PHP classes with similar functionality into a single unit called as a module. The newly created modules can be used, maintained and configured as a single unit.


Thursday, 12 October 2017

Azure video indexer - Get text from the Video


Question: What is Video Indexer API?
This API is used to Get the Visual Text from the Video. It will give you all the text that appear in the video.



Question: How to Get Text from the video?
  1. Get the API key
  2. Get the VIdeo URL from Azure OR Amazon S3 Or any public video URL.
  3. Pass the Video URL with API key and get the video Id token.
  4. Pass the video Id token with API key and get the all Text



Question: How to Get the API Key?
Follow the below link, you will get the API key which can be used further.
https://docs.microsoft.com/en-us/azure/cognitive-services/video-indexer/video-indexer-use-apis#subscribe-to-the-api


Question: How to Get the videoId token from the video?
$subScriptionKey = 'AZURE_SUBSCRIPTION_KEY_HERE';   
$videoUrl='https://foldername.blob.core.windows.net/bucketname/GV-NNokn-989-5175_360p.mp4';

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns?name=new%20video%20test&privacy=Private&videoUrl=".urlencode($videoUrl),
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    "ocp-apim-subscription-key: {$subScriptionKey}",    
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

OR Using Zend Framework
$subScriptionKey = 'AZURE_SUBSCRIPTION_KEY_HERE';   
$videoUrl='https://foldername.blob.core.windows.net/bucketname/GV-NNokn-989-5175_360p.mp4';
$jsonData = '';        
$config = array(
       'adapter' => 'Zend_Http_Client_Adapter_Curl',
       'curloptions' => array(CURLOPT_FOLLOWLOCATION => true,CURLOPT_SSL_VERIFYPEER =>FALSE),            
   );
    $url='https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns';
   $client = new Zend_Http_Client($url, $config);
   //Set the Header value
    $client->setHeaders('Content-Type', "multipart/form-data");
    $client->setHeaders('Ocp-Apim-Subscription-Key', $subScriptionKey);

    $postData = array(
    'name'=>'GV-NNokn-989-5175',
    'privacy'=>'Private',
    'videoUrl'=>$videoUrl
    );
    $client->setParameterGet($postData);

   $response = $client->request('POST'); 
   $jsonData= ($response->getBody());



Output
9a10d9d0002



Question: How to get video text from videoId token?
$subScriptionKey = 'AZURE_SUBSCRIPTION_KEY_HERE';   
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns/VIDEOIDTOKEN",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "ocp-apim-subscription-key: {$subScriptionKey}",
    "postman-token: 43c1c192-1de6-ac3d-42e0-b1ddc7cc0ee0"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

OR Using Zend Framework
$subScriptionKey = 'AZURE_SUBSCRIPTION_KEY_HERE';   
        $language = 'English';
        $body = 'How to you?';
        $breadDownsId='2bc89c5ec1';

        //Main Data
        $client = new Zend_Http_Client('https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns/' . $breadDownsId, array(
            'maxredirects' => 0,
            'timeout' => 30
        ));       
        
        $client->setHeaders('Ocp-Apim-Subscription-Key', $subScriptionKey);

        $parameters = array(
            'language' => $language,
        );
        $client->setParameterGet($parameters);

        try {
            $response = $client->request('GET');
            $data = $response->getBody();
        } catch (Exception $ex) {
            $data = $ex;
        }
        echo $response->getBody();die;

Output
{
    "accountId": "2b525a6-3a5408006294",
    "id": "9a10d9d912",
    "partition": null,
    "name": "new video test",
    "description": null,
    "userName": "Ram Kumar",
    "createTime": "2017-10-12T04:41:20.8986398+00:00",
    "organization": "",
    "privacyMode": "Private",
    "state": "Processed",
    "isOwned": true,
    "isEditable": false,
    "isBase": true,
    "durationInSeconds": 1373,
    "summarizedInsights":{/* It will have data*/},
    "breakdowns":{/* It will have data*/},
    "social":{/* It will have data*/}
}




Tuesday, 10 October 2017

CURL Example with Zend Framework1

CURL Example with Zend Framework1

Question: Give CURL Example with parameter in POST Method in Zend framework?
$config = array(
    'adapter' => 'Zend_Http_Client_Adapter_Curl',
    'ssltransport' => 'tls',
    'strict' => false,
    'persistent' => true,
);
$url = 'http://www.example.com';
$client = new Zend_Http_Client($url, $config);
$postArray = array('name'=>'Georage','age'=>33);
$client->setParameterPost($postArray);
$response = $client->request('POST');
echo $response->getBody();



Question: How to set Header data in CURL with Zend Framework1?
$client->setHeaders(array(
    'Host: www.example.com',
    'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0',
    'Accept: text/javascript, application/javascript, */*',
    'Accept-Language: en-gb,en;q=0.5',
    'Accept-Encoding: gzip, deflate',
    'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With: XMLHttpRequest',
    'Referer: http://example.com'
));


Question: How to set Cookie data in CURL with Zend Framework1?
$client->setCookie('__utma', '174057141.1507422797.1392357517.1392698281.1392702703.3');
$client->setCookie('PHPSESSID', '0a12ket5d8d7otlo6uh66p658a5');



Question: How to set Content-Type in CURL with Zend Framework1?
$client->setHeaders('Content-Type', 'audio/flac');



Question: Give CURL Example with different option in Zend framework?
$config = array(
    'adapter' => 'Zend_Http_Client_Adapter_Curl',
    'ssltransport' => 'tls',
    'strict' => false,
    'persistent' => true,
);
$url = 'http://www.example.com';
$client = new Zend_Http_Client($url, $config);
$postArray = array('name'=>'Georage','age'=>33);
$client->setParameterPost($postArray);
/** set Headers **/
$client->setHeaders(array(
    'Host: www.example.com',
    'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0',
    'Accept: text/javascript, application/javascript, */*',
    'Accept-Language: en-gb,en;q=0.5',
    'Accept-Encoding: gzip, deflate',
    'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
    'X-Requested-With: XMLHttpRequest',
    'Referer: http://example.com'
));

/** set cookie **/
$client->setCookie('__utma', '174057141.1507422797.1392357517.1392698281.1392702703.3');
$client->setCookie('PHPSESSID', '0a12ket5d8d7otlo6uh66p658a5');
$response = $client->request('POST');

$data= $response->getBody();
echo $data; 



Question: How to Skip SSL Check in CURL using Zend Framework?
$url='www.example.com';
$config = array(
    'adapter' => 'Zend_Http_Client_Adapter_Curl',
    'curloptions' => array(
        CURLOPT_FOLLOWLOCATION => TRUE,
        CURLOPT_SSL_VERIFYPEER => FALSE
    ),
);
 $client = new Zend_Http_Client($url, $config);
 $response = $client->request('GET');



Question: How to pass binary data of in CURL usig Zend Framework?
$file='http://example.com/test/GM-qE2zq-1078-5525_360p.flac';
$jsonData = '';        
$config = array(
       'adapter' => 'Zend_Http_Client_Adapter_Curl',
       'curloptions' => array(CURLOPT_FOLLOWLOCATION => true,CURLOPT_SSL_VERIFYPEER =>FALSE),            
   );
    $url='https://stream.watsonplatform.net/speech-to-text/api/v1/recognize';
   $client = new Zend_Http_Client($url, $config);
   //Set the Header value
    $client->setHeaders('Content-Type', 'audio/flac');
    $client->setHeaders('Transfer-Encoding', 'chunked');            

   $client->setRawData(file_get_contents($file));
    //Set post method
   $response = $client->request('POST'); 
   $jsonData= ($response->getBody());