PHP

musicfortrio.com

Goal: Develop an e-commerce website for the “Music for Trio” company that sells music online. Use WordPress as a CMS for the site with a e-commerce plugin. Test and deploy the site on the company’s server. Provide ongoing maintenance.
Web technologies used: HTML5, CSS, PHP, WordPress.
Tools and software: Photoshop, MS Visual Studio Code
Release date: October, 2017
Project type: Contract work for Music for Trio.
musicfortrio.com

clicksand.net

Goal: Develop a website (theme) for a book author based on provided PDF mocks. Use WordPress as a CMS for the site. Test and deploy the site on the company’s server.
Web technologies used: HTML5, CSS, PHP, WordPress.
Tools and software: Photoshop, MS Visual Studio Code
Release date: May, 2017
Project type: Contract work for Civilis Marketing.
clicksand.net

Add a New Subscriber to Your MailChimp List with API

In order to create a functionality that allow us to automatically add a subscriber to a list on MailChimp Email and Marketing platform, we can use MailChimp’s API. This example is written in PHP. Make sure that you have an API key and list id from your MailChimp account.

The following code adds a subscriber to the list:

<?php
$message = subscribe($email, $fname, $lname);

The function code is below:

<?php
function subscribe($email, $fname, $lname) {
  $apiKey = 'some-ip-key-dda80b';
  $list = '0123abc';
  $member = md5(strtolower($email));
  $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
  $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $list . '/members/' . $member;        
  $json = json_encode([
    'email_address' => $email,
    'status'        => 'subscribed',
    'merge_fields'  => [ 'FNAME' => $fname, 'LNAME' => $lname ]
  ]); 
       
  // send a HTTP POST request with curl
  $c = curl_init($url);
  curl_setopt($c, CURLOPT_USERPWD, 'user:' . $apiKey);
  curl_setopt($c, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($c, CURLOPT_TIMEOUT, 10);
  curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'PUT');
  curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($c, CURLOPT_POSTFIELDS, $json);
  $result = curl_exec($c);
  $httpCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
  curl_close($c);

  // get message based on response code
  if ($httpCode == 200) {
    $message = 'Subscribed.';
  } else {
    switch ($httpCode) {
        case 214:
	$message = 'Already subscribed.';
        break;
        default:
        $message = 'Some problem occurred.';
        break;
    }
    $message = 'Some problem occurred.';
  }
  return $message;
}

To unsubscribe an existing subscriber use PATCH request. The following code makes a subscriber to unsubscribe:

<?php
function unsubscribe($email) {
  $apiKey = 'some-ip-key-dda80b';
  $list = '0123abc';        
  $member = md5(strtolower($email));
  $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
  $url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $list . '/members/' . $member;        
  $json = json_encode(['status' => 'unsubscribed']);
        
  // send a HTTP POST request with curl
  $c = curl_init($url);
  curl_setopt($c, CURLOPT_USERPWD, 'user:' . $apiKey);
  curl_setopt($c, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($c, CURLOPT_TIMEOUT, 10);
  curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'PATCH');
  curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($c, CURLOPT_POSTFIELDS, $json);
  $result = curl_exec($c);
  $httpCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
  curl_close($c);

  $message = ($result) ? 'Unsubscribed.' : 'Some problem occurred.';
  return $message;
}

A basic MVC Framework Folder Structure

This is a typical structure of a basic PHP framework based on MVC design:
mvc structure

  • app folder contains the application related files and sub-folders, such as controllers, models and views.
  • core folder contains the core files of the framework itself.
  • log folder contains all log files.
  • public folder contains all publicly accessible files. This is the only folder accessible to the web. The root of the web server should poit to this folder. This folder will contain all static files, such as CSS, images, JavaScript files.
  • vendor folder contains all third-party code / frameworks

Use of Capture Groups in Regular Expressions for Routing

PHP offers very powerful tools that you may use to create routing for your MVC framework. Using regular expressions instead variable strings in URL routing may save you from writing many lines of code.

Let’s take a look at the preg_match() function. As we know, the function takes the sting and looks if it matches to the regular expression.

preg_match($reg_exp, $string)

If the string matches, the function returns 1. If not, it returns 0. But there is also an optional third parameter to this function, which will collect the results of the match in an array:

<?php 
preg_match("/[0-9]+/", "132", $match);
var_dump($match);

// Result: array(1) { [0]=> string(3) "132" }

Now we can use regular expression Capture Groups denoted as parentheses to catch more than one result into that array:

<?php 
preg_match("/a([0-9]+)b/", "a132b", $match);
var_dump($match);

// Result: array(2) { [0]=> string(5) "a132b" [1]=> string(3) "132" }

We also can use two groups like this:

<?php
preg_match("/([a-zA-Z]+) (\d+)/", "Apr 2017", $match);

// Result: array(3) { 
  [0]=> string(8) "Apr 2017" 
  [1]=> string(3) "Apr" 
  [2]=> string(4) "2017" }

We even can give names to the capture groups, that will be used in the $match array along with the numbers:

<?php
preg_match("/(?[a-zA-Z]+) (?\d+)/", "Apr 2017", $match);

// Result: array(5) { 
  [0]=> string(8) "Apr 2017" 
  ["month"]=> string(3) "Apr" 
  [1]=> string(3) "Apr" 
  ["year"]=> string(4) "2017" 
  [2]=> string(4) "2017" }

To get an array with the strings only, use a foreach loop:

<?php
preg_match("/(?[a-zA-Z]+) (?\d+)/", "Apr 2017", $matches);
$results=[];
foreach ($matches as $key => $match ) {
    if (is_string($key)) {
        $results[$key] = $match;
    }
}

// Result: array(2) { 
  ["month"]=> string(3) "Apr" 
  ["year"]=> string(4) "2017" } 

Now it is much easier to match URL to controllers in a MVC framework.