Laravel 5 + Google APIs Client Library

Prerequisite;

  1. Created a google app project
  2. Enabled Google+ API at your google project
  3. Created new Client ID
  4. Generated API Key
  5. Get service account name

You can find in this google link to do above steps.If you have already done those, you can start the following steps.

Step1

In your composer.json file, add

"require": {
 "google/apiclient": "1.0.*@beta"//This beta version is when I use, get updated version when you use
 }

Step2

(My apiclient version is still beta, so I need to set minimum-stability to dev, but if your version is stable, you don’t need to change and skip to Step3)Find minimum-stability in your laravel project, and change to

"minimum-stability": "dev"

Step3

run >>>>composer update. After that, you will see google folder under yourapp>vendor folder.

Step4

In your composer.json file, add the following;

{ "autoload": 
     { "classmap": [ "vendor/google/apiclient/src/Google" ],
      } 
}

Step5

In your http://console.developers.google.com Create a new Client ID in Credentials, Select Service account and click on Create Client ID

Step6

In config folder, add google.php and add;

return [
    'app_name'          => 'your_app_name', 
    'client_id'         => 'your_client_id',
    'client_secret'     => 'your_client_secret',
    'api_key'           => 'your_api_key',
    'service_account_name' => 'your_service_account_name'
];

Step7

Create app>Services>Google.php file and then add the following code;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;

class Google {

    protected $client;

    protected $service;

    function __construct() {
        /* Get config variables */
        $client_id = Config::get('google.client_id');
        $service_account_name = Config::get('google.service_account_name');
        $key = Config::get('google.api_key');//you can use later

        $this->client = new \Google_Client();
        $this->client->setApplicationName("your_app_name");
        $this->service = new \Google_Service_Books($this->client);//Test with Books Service        
    }

    public function getBooks(){
        $optParams = array('filter' => 'free-ebooks');
        $results = $this->service->volumes->listVolumes('Henry David Thoreau', $optParams);

        dd($results);
    }
}

Step8

In HomeController, modify index function

public function index(Google $google)
{
    $result = $google->getBooks();
    print_r($result);
}

Reference:

https://github.com/google/google-api-php-client

https://developers.google.com/api-client-library/php/start/installation

Clone Laravel Git Project

If you are new to laravel, you all would have read Laravel Doc. But it was hard for me to find how to run cloned laravel git project. When I run, I got this error;

Warning: require(/Applications/XAMPP/xamppfiles/htdocs/laravel-master/bootstrap/../vendor/autoload.php) [function.require]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/laravel-master/bootstrap/autoload.php on line 17

Fatal error: require() [function.require]: Failed opening required ‘/Applications/XAMPP/xamppfiles/htdocs/laravel-master/bootstrap/../vendor/autoload.php’ (include_path=’.:/Applications/XAMPP/xamppfiles/lib/php:/Applications/XAMPP/xamppfiles/lib/php/pear’) in /Applications/XAMPP/xamppfiles/htdocs/laravel-master/bootstrap/autoload.php on line 17

Actually it is so simple, here is steps;

Step 1Install Composer(same from laravel site)

Step 2: Clone git to your running environment(eg. if you use xampp, htdos>your_app)

git clone https://github.com/laravel/laravel

Step 3: In your_app folder, right click anywhere then you will see ‘Composer Install’

Done!!

Step 4:

For xampp user, go to http://localhost/your_app/public/

For laravel artisan user, in Step3 command prompt, type “php artisan serve”, then you will see url to run your project default is http://localhost:8000/

 

Hope it helps!

 

 

Only phpMyadmin and projects show blank

Steps and Error

Error occurs when I installed first wamp, then IIS. What I did are;

Note

  1. Wamp icon is green.
  2. No view source in web page.
  3. No error log in apache.
  4. All php files exist in app folder.
  5. Turn off all antivirus software.

Steps

  1. Installed wamp and changed port to 9999
  2. Installed IIS and others to run PHP on IIS
  3. IIS was ok and wamp shows only localhost:9999, phpMyadmin and project urls are showing blank page.
  4. Completely uninstalled both wamp and IIS.
  5. Though uninstalled and reinstalled wamp, the same result.(There is no error shown while reinstalled wamp). Then, I decided to install xampp.
  6. Installed xamp, that shows some services cannot be installed due to error.

2 Solutions

(were given while installing xampp)

Solution 1

Installed in different drive(means which can have permission to be installed)

Solution 2

Turn off User Account Control after installation

Cheers!

Using curl for codeigniter REST

http://philsturgeon.co.uk/code/codeigniter-curl

1. copy curl file to library file
2. There would be two controller in ci. One for sending data in JSON format and one for receiving data in JSON format by json_decode.
To insert data into MySQL db from data from android,
In one controller(eg. client_test controller),
function client_register()
{
$username = ‘admin’;
$password = ‘1234’;

$this->load->library(‘curl’);

$this->curl->create(‘http://hsbc.local/api/client/register/format/json’);

// Optional, delete this line if your API is open
$this->curl->http_login($username, $password);

$data = array (
“type_id” => 1,
“agent_id” => 2,
“first_name” => “Joanna”,
“last_name” => “Lee”,
“phone” => “234555”,
“address” => “Mandalay”,
“city” => “Mandalay”,
“mobile” => “095053453”,
“email” => “lee@gmail.com”,
“createddate” => “2013-10-04”,
“modifieddate” => “2013-10-0”
);

$json = json_encode($data);
$this->curl->post(array(
‘postdata’ => $json
));
$result = json_decode($this->curl->execute());
print_r($result);
}
In the other controller(client controller) that might extends REST_Controller(https://github.com/philsturgeon/codeigniter-restserver),
function create_post()
{
try
{
$json = $this->post(‘postdata’);
$data = json_decode($json);
$this->db->insert(‘client’, $data);
$message = json_encode(array(“last_insert_id” => $this->db->insert_id()));
$this->response($message, 200);
}
catch (Exception $e)
{
$this->response(array(‘error’ => $e->getMessage()), 500);
}
}
Then, in the url type client_register, then if it is successfully inserted it will display last insert id.