Friday, August 29, 2014

Get Oauth2 Access & Refresh Token in Apigility (ZF2)

Oauth2 Access & Refresh Token:

For set up of oauth2 go to your config/autoload/local.php and add it in return array

'zf-oauth2' => array(
        'storage' => 'ZF\\OAuth2\\Adapter\\PdoAdapter',
        'db' => array(
            'dsn' => 'mysql:dbname=zend_db;host=localhost;charset=utf8',
            'username' => 'root',
            'password' => 'root',
        ),
        'allow_implicit' => true,
        'enforce_state' => true,
    ),


Set your configuration according to your requirements.

Create your tables accordingly this file

vendor/zfcampus/zf-oauth2/data/db_oauth2.sql

 Insert these fields accordingly your tables

client_id testclient and client_secret testpass, and a user with username testuser and a password testpass.

Password and Client Secret should be in bcrypt form

Now for testing run this in your address bar

http://localhost/oauth/authorize?response_type=code&client_id=testclient&redirect_uri=/oauth/receivecode&state=xyz
 
Now it will show a screen like 


Authorize
Now click on yes.

It will show a screen like

Authentication code
Now run curl command in your terminal with in 30 seconds. It will provide you access and refresh token.

Thanks

Friday, August 22, 2014

How to create RESTful DB Connected api through Apigility (ZF2) ?

Api through Apigility:

Install Apigility:

For installing apigility , open terminal & run this command where you want to install apigility.

curl -sS https://apigility.org/install | php

After that run this command for composer.phar

$ php composer.phar require "zfcampus/statuslib-example:~1.0-dev"
 


Configure files: 
Now go to this file config/autoload/local.php

 And replace

return array(
    'db' => array(
        'driver' => 'Pdo_Mysql',
        'dsn' => 'mysql:dbname=database_name;host=localhost',
        'username' => 'user_name',
        'password' => 'user_pass',
        'driver_options' => array(
            1002 => 'SET NAMES \'UTF8\'',
        ),
        'adapters' => array(
            'mainadapter' => array(
                'driver' => 'Pdo_Mysql',
                'database' => '
database_name',
                'username' => '
user_name',
                'password' => '
user_pass',
                'hostname' => '127.0.0.1',
                'charset' => 'utf8',
            ),
        ),
    ),

  
 
Now go to config/application.config.php 
and edit


array(
    'modules' => array(
        /* ... */
        'StatusLib',
    ),
    /* ... */
)
 
Create a PHP file data/statuslib.php that returns an array:
<?php
return array(); 
 
Now create a hash using  http://www.htaccesstools.com/htpasswd-generator/ and put it in data/htpasswd file. Here put a username and password which you want to authenticate.



Run for port 8888:
Run this command in your terminal where public is folder in side apigility folder

php -S localhost:8888 -t public public/index.php

and keep it as it is. Don't stop this command.

Create REST Api:

Go to http://localhost. Then click on 'Admin' and then 'APIs' 

Create new api with name 'Status'.

Now refresh page and click on 'Status'.

Goto Rest Service and click for new one.

Go to DB connected and select 'mainadapter'. put table name 'status' and create DB connected. 

Also create table 'status' in your db.

Make sure your config, data and module folder has write permission.


Now setting all the page according to these images.



REST Services Screen
Edit REST Parameter Settings
Edit Service Classes
Message Field
All Fields
 These field names should be accordingly your table fields.

Now go to authorization and do this

Authorization - Complete
Now create a new file
module/Status/src/Status/V1/Rest/Status/StatusResource.php

 
 and add

<?php
namespace Status\V1\Rest\Status;
use StatusLib\MapperInterface;
use ZF\ApiProblem\ApiProblem;
use ZF\Rest\AbstractResourceListener;

class StatusResource extends AbstractResourceListener
{
    protected $mapper;
    public function __construct(MapperInterface $mapper)
    {
        $this->mapper = $mapper;
    }

    /**
     * Create a resource
     *
     * @param  mixed $data
     * @return ApiProblem|mixed
     */
    public function create($data)
    {
        //return new ApiProblem(405, 'The POST method has not been defined');
        return $this->mapper->create($data);
    }

    /**
     * Delete a resource
     *
     * @param  mixed $id
     * @return ApiProblem|mixed
     */
    public function delete($id)
    {
        //return new ApiProblem(405, 'The DELETE method has not been defined for individual resources');
        return $this->mapper->delete($id);
    }

    /**
     * Delete a collection, or members of a collection
     *
     * @param  mixed $data
     * @return ApiProblem|mixed
     */
    public function deleteList($data)
    {
        //return new ApiProblem(405, 'The DELETE method has not been defined for collections');
        return $this->mapper->deleteList($data);
    }

    /**
     * Fetch a resource
     *
     * @param  mixed $id
     * @return ApiProblem|mixed
     */
    public function fetch($id)
    {
        //return new ApiProblem(405, 'The GET method has not been defined for individual resources');
        return $this->mapper->fetch($id);
    }

    /**
     * Fetch all or a subset of resources
     *
     * @param  array $params
     * @return ApiProblem|mixed
     */
    public function fetchAll($params = array())
    {
        //return new ApiProblem(405, 'The GET method has not been defined for collections');
        return $this->mapper->fetchAll();
    }

    /**
     * Patch (partial in-place update) a resource
     *
     * @param  mixed $id
     * @param  mixed $data
     * @return ApiProblem|mixed
     */
    public function patch($id, $data)
    {
       // return new ApiProblem(405, 'The PATCH method has not been defined for individual resources');
        return $this->mapper->update($id, $data);
    }

    /**
     * Replace a collection or members of a collection
     *
     * @param  mixed $data
     * @return ApiProblem|mixed
     */
    public function replaceList($data)
    {
        return new ApiProblem(405, 'The PUT method has not been defined for collections');
    }

    /**
     * Update a resource
     *
     * @param  mixed $id
     * @param  mixed $data
     * @return ApiProblem|mixed
     */
    public function update($id, $data)
    {
        //return new ApiProblem(405, 'The PUT method has not been defined for individual resources');
        return $this->mapper->update($id, $data);
    }
}


 Now create a new file
 module/Status/src/Status/V1/Rest/Status/StatusResourceFactory.php

and add

<?php
namespace Status\V1\Rest\Status;
class StatusResourceFactory
{
    public function __invoke($services)
    {
        return new StatusResource($services->get('StatusLib\Mapper'));
    }
}








Its Done Now.

For testing:

GET /status HTTP/1.1
Accept: application/json
 
You will get your table data in JSON format

Thanks

Friday, August 8, 2014

Install Zend on ubuntu

Install Zend Framework 2 on ubuntu:

For installing zend framework 2 on ubuntu , firstly we will be installed its skelton application.

we can directly get in on github

https://github.com/zendframework/ZendSkeletonApplication

or in command prompt we can directly install it.

php composer.phar create-project --repository-url=
 "http://packages.zendframework.com" zendframework/skeleton-application
 path/to/install

after this we have to install zend framework:

php composer.phar self-update
php composer.phar install
 
After this we have to set configuration of apache configuration file

Go to  /etc/apache2/sites-enabled/

then open default configuration file
and set its configuration

<VirtualHost *:80>
    ServerName zf2-tutorial.localhost
    DocumentRoot /path/to/zf2-tutorial/public
    SetEnv APPLICATION_ENV "development"
    <Directory /path/to/zf2-tutorial/public>
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
 
 
That's it.

Friday, August 1, 2014

Install magento on ubuntu

Install Magento on Ubuntu:

For installing magento on your ubuntu machine , you will be in need of install apache2, php and mysql (for installing them see previous articles).

After installing them go to the folder of your server

cd /var/www
After that download magento.

wget http://www.magentocommerce.com/downloads/assets/1.8.1.0/magento-1.8.1.0.tar.gz
Now extract this file

tar -zxvf magento-1.8.1.0.tar.gz
Remove that zip file from root

rm magento-1.8.1.0.tar.gz
Move all files / Folders from magento folder to out side

mv magento/* magento/.htaccess .
Now give the privilege to the following folders

chmod -R o+w media var
chmod o+w app/etc
Now create a database from phpmyadmin or command prompt. After that go to localhost/ and keep some setting regarding database.

Thats it its installed now