Google Mobile-friendly test in PHP

Lots of folks out there about to make your website Mobile-friendly. It’s obvious that there will be significant impact on your website if your site is not mobile-friendly or what we regularly call ‘responsive’. Mobile friendly sites will get higher rank in search results in Google. And Google already started to rolling out this algorithms.

After searching on internet, I didn’t get any quick solution to check mobile-friendliness website in PHP. Although Google published it’s official Mobile-friendliness Test Tools.

When I tried to find some solutions then I figured out that Google has it’s public Pagespeed Test API v2. But in this version Google didn’t make it clear to see Pass or fail result for mobile-friendliness test. It only shows UX score. But in their v3beta1 API version they have a clear indicator about mobile-friendliness. And I figured it out and made a simple PHP functions to check the mobile-friendliness test of any website. Here is my scripts:

In the above script you just need to put your website URL to check and Google PageSpeed API Key what you will get from your Google API Console or see the Google PageSpeed Developer Getting Starter page.

Now, run your script and you will get the following array.

array (size=8)
  'kind' => string 'pagespeedonline#result' (length=22)
  'id' => string 'http://facebook.com/' (length=29)
  'responseCode' => int 200
  'ruleGroups' =>
    array (size=1)
      'USABILITY' =>
        array (size=2)
          'score' => int 100
          'pass' => boolean true
  'pageStats' =>
    array (size=2)
      'numberRobotedResources' => int 0
      'numberTransientFetchFailureResources' => int 0
  'formattedResults' => etc.........

 

From the above array $result['ruleGroups']['USABILITY']['pass'] is the actual result of your website’s mobile-friendliness status. true means your website is mobile friendly and false means your failed to make your website mobile friendly. Simple.

Have fun! Write comments if you need any more assistance.

Enable Debug mode in Twig Template

Sometimes you need to see your variables or object in Twig template before printing it out. So to enable debug mode in twig template system you just need to tweak few settings upon initializing twig environment. Just follow the below code where I initiated Twig environment with debug mode.

<?php
$twig = new Twig_Environment($loader, array(
    'debug' => true,
    // ...
));
$twig->addExtension(new Twig_Extension_Debug());

 

And inside your template where you want to dump the variable just write “. You can read more about dump function twig template from here

Create INI file in PHP

Write configuration data in a .ini file in PHP. It’s important for large application to store global configurations about the whole/partial system for applications.

Enable OpCache in PHP5 to boost your application

After lots of folk PHP5 has released [Zend OpCache] engine with core PHP5.5 and PHP internals will continue to develop PHP with this Zend OpCache in future releases of PHP.
It’s great news for all the PHP lovers who used APC cache to boost their application performance. But keep in mind, APC is no more supported with PHP5.5 and may not be for future release also.
So here you should love OpCache (original name: Zend OpCache) for tweaking your application performance.
Although this opCache is built-in in PHP5.5 so you just have to enable this from your php.ini file and you are ready to go.
So let’s see how you can enable this OpCache in your environment.

Just open your PHP php.ini file (location may depends on your environment) and search for opcache and here you will see the configuration like below.

[opcache]
; Determines if Zend OPCache is enabled
;opcache.enable=0

; Determines if Zend OPCache is enabled for the CLI version of PHP
;opcache.enable_cli=0

; The OPcache shared memory storage size.
;opcache.memory_consumption=128

; The amount of memory for interned strings in Mbytes.
;opcache.interned_strings_buffer=4

Now you just have to remove ; from ;opcache.enable=0 and replace 0 with 1. Also do the same for ;opcache.enable_cli=0.

And if you want to modify share memory storage size then just uncomment it and change the integar value. So after change it should look like

[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1

; Determines if Zend OPCache is enabled for the CLI version of PHP
opcache.enable_cli=1

; The OPcache shared memory storage size.
opcache.memory_consumption=256

; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=4

Now you have to restart your apache or if you are using nginx then you have to restart php5-fpm. Enjoy!

Tools to monitor your server’s OpCache status
Founder of PHP Rasmus Lerdorf developed few snippet to test the OpCache status for your environment.
You can download that from here. Download this repo and put it anywhere in your server and browse opcache.php and it will display the current status of your OpCache engine like the following image.
Zend OpCache Status Screenshot

Enable mCrypt extension in your PHP5 in nGinx server in Ubuntu

mCrypt is one of the best and most popular Cryptography Extensions in PHP5. To install it with your PHP5 in your NGinx server for Ubuntu 14.04 please install it with following command

sudo apt-get install php5-mcrypt

and then restart your NGinx server with sudo service nginx restart. But sometimes it may fails to load your mCrypt extension with your PHP5. If you face those types of problem just execute the following command in your Ubuntu 14.04 command line and restart Nginx.

sudo phpenmod mcrypt

Now you have mcrypt enabled in your PHP5. Keep coding!