External API calling in Laravel by Guzzle Package

Recently I am working on a project that need to access some external API. And it’s a Laravel project, so I am looking for some package that can save my time and meet the deadline too.

 

I am using GuzzleHttp package for accessing external API and its really easy

 

Installation:

composer require guzzlehttp/guzzle:~6.0

Uses:
on your controller file just put before your class ClassName

use GuzzleHttp\Client;

 

Now call this Guzzle Client in your method

//create an instance of Client wiht base url of the API

$client = new Client(['base_uri' => 'http:127.0.0.1/path/to/api/']);

// Send a GET request to http:127.0.0.1/path/to/api/ 
// and method name is apiName 
// with api authentication (username and password)
$response = $client->request('GET', apiName', [
                 'auth' => ['username', 'password']
                 ]);

// check the response by

dd($response);

// Get the response status code

dd($response->getStatusCode());

// Get the response phase
dd($response->getReasonPhrase());

// Get all/full header
dd($response->getHeaders());

// Get specific specific entity of header, 
// here we retrive content-type
dd($response->getHeader('content-type'));

// Get api content. it return the main content the we need

dd($response->getBody()->getContents());

 

this post will continue as i go with this Guzzle package..

 

Repository – https://github.com/guzzle/guzzle

and here is the main document – http://docs.guzzlephp.org/en/latest/

 

How to define Global Variable or Constant in Laravel

We often need to define global variables/constants for our application, like Status, Conditions, Countries (we can put all countries in table as it will be along array/object), also weekdays, weekends and e.t.c. vary on projects.

Status (Active, Inactive) and Conditions (Ok, Good, Better, Best) or Grade (F, C, D, A, A-, A+), so like these type of variables/constants,  we write same code multiple times. But if we set all of these as Global Variables then we can use these anytime, anywhere in application.

 

 

And here I am presenting how to define GLOBAL Variable/Constant in Laravel 5.2. (This is not the only way, but I like it)

  1. First create a file named constants.php on config directory
  2. And code some variables like below

return [

‘STATUS’ => [

1 => ‘Active’,

0 => ‘Inactive’

],

‘Conditions’ => [

1 => ‘Ok’,

2 => ‘Good,

3 => ‘Better,

4 => ‘Best’

 

],

‘GRADE’ => [

‘A+’ => ‘A+’,

‘A’ => ‘A’,

‘A-’ => ‘A-’,

‘B’ => ‘B’,

‘C’ => ‘C’,

‘D’ => ‘D’,

‘F’ => ‘F’

]

];

  1. Now, how can we use/access this Global Variables? Very easy, and we can also access these variables on view too.

Config::get(‘constants. STATUS’); // it will output status array.

Or, if we want a specific one element then

Config::get(‘constants. GRADE.F’); // output: F

  1. And lastly we can also set Global variable only for the current request. This will not persist.

Config::set(‘constants. Conditions.5’, ‘Not Stable’)

 

And finally – if you like this article please inspire me by posting a comment (at below) for the next article 😉 . And I don’t mind if you want to share among with your friends, bocz. Sharing’s Caring.

 

Credit: google, stackoverflow, Laravel.io, Laravel docs and etc.

Update (custom) XAMPP/WAMP/LAMP default landing page layout

We are building website and applications on localhost and I use XAMPP package on Linux for my local development.
Screenshot from 2015-10-07 20:20:51

 

After installing Xampp, we always get a default home page from Xampp and most of the time we don’t need that. Basically before writing this article I always delete all items of htdocs/www folder, but sometimes we need to know things like versions and if functions are enable or not and also the limits various variables. So by using the below script you can easily get a good view of your localhost’s projects (as a list) and as well as databases from phpmyadmin. And you dont need to delete anything on htdocs folder and get all thing whenever you need.

 

In the header put this css code into the style tag for a better view

 

body {

background: #fcfcfc;

color: #111;

font-family: verdana, sans-serif;

padding: 0;

margin: 0;

}

#main {

width: 100%;

position: relative;

}

.container {

padding: 1em;

}

#menu { float: left; width: 24%;  }

#menu ul { padding: 0; border-right: 3px solid white; margin: 0; }

ul li { list-style-type: none; }

ul li a {

display: block;

background: #eee;

padding: 4px 7px;

border-top: 2px solid #fff;

}

ul li a:hover {

background: #B6C6D7;

color: black;

}

iframe {

border: 0 none;

overflow: hidden;

width: 74%;

height: 95%;

padding-left: 1%;

}

.clear-block {

width: 100%;

height: 1px;

clear: both;

}

Add this css file for a look like phpmyadmin

<link rel=”stylesheet” type=”text/css” href=”phpmyadmin/phpmyadmin.css.php”></script>

 

Put this code into the body

</div>

</div>

Now you are done 🙂 . And save this file as index.php at htdocs

Or download from here

 

 

Update: 07.10.2015

If there is issue of viewing your phpmyadmin, here is the solution. You just need to add an extra line to allow the third party framing on your phpmyadmin/config.inc.php file.

$cfg[‘AllowThirdPartyFraming’] = true;

 

How to find src attribute of image tag in php?

In my case, I had a string containing exactly one <img /> tag (and no other markup). And I was trying to get the src attribute.

// get the featured image
$image = '<img src="path/to/image_file" />';

// get the src for that image
$pattern = '/src="([^"]*)"/';
preg_match($pattern, $image, $matches);
$src = $matches[1];
unset($matches);

In the pattern to grab the title and the alt, you could simply use $pattern = ‘/title=”([^”]*)”/’; to grab the title and $pattern = ‘/alt=”([^”]*)”/’; to grab the alt.

I hope this solution work for you also. Share and Enjoy. And inspire me by your comments 😉

PHP code to execute MySQL script

There are advanced and comfortable tools to manage your MySQL databases (i.e.  server side: phpMyAdmin, client side:  SQLyog).

But sometimes you can not use those tools and need the ability to execute some SQL script on server by your own hands, easy and fast way. What to do in such situation? Use this PHP code to execute all SQL commands from SQL script text file, one by one:

$sqlFileToExecute = 'path/to/sql/script.sql';

$hostname = DB_HOST;
$db_name = DB_NAME;
$db_user = DB_USER;
$db_password = DB_PASSWORD;

$link = mysql_connect($hostname, $db_user, $db_password);
if (!$link) {
  die ("MySQL Connection error");
}

mysql_select_db($db_name, $link) or die ("Wrong MySQL Database");

$fp = file($sqlFileToExecute, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$query = '';
foreach ($fp as $line) {
  if ($line != '' && strpos($line, '--') === false) {
  $query .= $line;
    if (substr($query, -1) == ';') {
      mysql_query($query);
      $query = '';
    }
  }
}