Check whether URL match with Wildcard pattern

Suppose, you have some rules to check whitelisted url. So everytime whenever you will get a new url so you can cross match that URL with your whitelisted URL. And whitelisted URLs sometimes can be wildcard url. So to check whether the given URL matched any of the wildcard urls.

<?php
$whiteListedUrls = [
    "https://google.com/test/*",
    "https://yahoo.com/test/index.html",
    "https://sohelrana.me/*",
    "https://sohelrana.me"
];

$url = "https://google.com/test/testWildCard/ok.html";


function checkUrlWildcard($url, $whiteListUrls = [])
{
    foreach ($whiteListUrls as $wUrl) {
        $pattern = preg_quote($wUrl, '/');
        $pattern = str_replace('\*', '.*', $pattern);
        $matched = preg_match('/^' . $pattern . '$/i', $url);
        if ($matched > 0) {
            return true;
        }
    }

    return false;
}

//it will return true
var_export(checkUrlWildcard($url, $whiteListedUrls));

https://google.com/test/* this url means actually all url path after https://google.com/test/. So if we want to check https://google.com/test/testWildCard/ok.html whether it is covered with our rules or not. So it will return true of course.

It’s a simple PHP script. But may help somebody.

Supercharge WP REST API with Google App Engine

It seems that I am pretty late here to play with WordPress REST API. It’s really super cool! Now I can think about how well and fast I can distribute or work with wordpress blog posts. Even now you can host your whole WordPress inside a static HTML page. Funny? Yea, like your HTML page will interact with your REST API json response and small JS can help you to display inside page. Then you will get 10000000x super fast blog post delivery. Trust me! It works. You will understand if you are a little techie guy.

I was thinking about if I can easily get all of my posts’s data as REST API Json response then easily I can use them wherever and whenever I want. But there must be a trick. Cause still this REST API is working from inside your main WP hosting.

But what if (IF) we can recruit some middleman who can hold data and deliver those data without even interacting with your original WordPress site. So you will save lots of money and your site’s performance will be super boosted.

I just run an experiment (I shared my experimented snippet here) with Google App Engine. Why Google App Engine? Ask that to google yourself. Google App Engine is the best solutions for ready-set-go types of app deployment with lots of features that Google maintains themselves. So I am going to write a small PHP code which will load your site’s REST API JSON data and after that rest of the request it will server from directly GAE memcache. So, more easily, if your site has 1M audience a day, then most probably you will hit your original WP site once, and 999999 user will get that data from directly App Engine (PHP app data persistent through memcache).

I just tried it and it works and I tried to do 50,000 hits and it just took few milliseconds to deliver me my wordpress site’s data from Google App Engine and I just designed a HTML page with simple JS that will load the content from my App Engine and will display inside HTML page, and you know HTML page you can serve billions user in a minute. Voila 🙂

So here is my little PHP script and also I uploaded the full GAE implementation in a GitHub repository so you can directly download, change your site’s URL and deploy to Google App Engine.

<?php
/**
 * Serve remote URL content through App Engine by using Memcache. Pretty interesting.
 * It's really cool!
 *
 * @Author Shaharia Azam <shaharia@previewtechs.com>
 */
define("KB_JSON_URL", "https://YOUR-WEBSITE-ADDRESS/wp-json/wp/v2");

$memcached = new Memcached();
$memcached->addServers([
    ['localhost', 11211, 33]
]);

/**
 * @param $url
 * @param string $method
 * @return mixed
 */
function fetchJSONFromRemoteWP($url, $method = 'GET')
{
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_MAXREDIRS => 2,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => $method,
        CURLOPT_HTTPHEADER => [],
    ));

    $data = curl_exec($curl);
    curl_error($curl);
    curl_close($curl);

    return $data;
}

$remoteUrl = KB_JSON_URL . $_SERVER['REQUEST_URI'];

$cacheKey = base64_encode($remoteUrl);

$content = $memcached->get($cacheKey);
if (!empty($content)) {
    $data = $content;
} else {
    $data = fetchJSONFromRemoteWP($remoteUrl);
    $memcached->set($cacheKey, $data, 360000);
}

header('Content-Type: application/json');
echo json_encode(json_decode($data, true));

exit;

And here is the app.yaml configuration for Google App Engine

runtime: php55
api_version: 1

service: kb-wp-reverse-proxy

threadsafe: true

handlers:
- url: /assets
  static_dir: public/assets

- url: /*
  script: public/index.php
  secure: always

- url: /.*
  script: public/index.php
  secure: always

skip_files:
- ^.git$

This snippets are available on GitHub. If you face any trouble and want to optimize your site and make it super-fast and stable with this types of solutions, reach me anytime at shaharia@previewtechs.com

Get ThemeForest theme information and stats with simple PHP script

Now a days, lots of professionals are selling their creative works on ThemeForest. I just created the following simple (very simple) PHP script that works with HTML DOM and XPATH will do a very interesting job to show the ThemeForest theme information and statistics.

Just run this script on your PHP environment and you will get the following information of any specific ThemeForest theme.

Array
(
    [sellCount] => 2
    [price] => $16
    [created] => 2016-10-13
    [lastUpdated] => 2016-10-13
    [highResolution] => No
    [compatibleBrowsers] => IE10,IE11,Firefox,Safari,Opera,Chrome,Edge
    [filesIncluded] => HTMLFiles,CSSFiles,JSFiles
    [columns] => 1
    [documentation] => Unrated
    [layout] => Responsive
    [totalComment] => 8
)

 

WHMCS PHP API – Integrate billing system with your application

WHMCS is a popular billing system specially for domain+hosting business. It provides mostly billing solutions. You can download WHMCS from download page. As a billing system, if you have several tools, utilities and services that require connection between your application and WHMCS then you badly need API to accomplish that interconnection.

WHMCS has a very basic API documentation (it’s enough though) without any specific programming library. You can use the following wrapper class to interact with your WHMCS system from your PHP application. This is very simple and doesn’t require further explanation in depth. See the following script. If you have any better idea or suggestions, don’t forget to write me in the comment area.