PHP Exercise 1: Fetch URL and Parse Content

July 15, 2015 at 1:47 pm


In an effort to help others learn some really nifty PHP tricks, I’m going to start this series of PHP quizzes. I’ll explain a few concepts and then pose a problem that can be solved using those concepts. Don’t expect rocket science here, but definitely use it as a resource to learn a lot about PHP in a short space of time. Especially if you are a newcomer, or if you just want to see how much PHP you know.

Concepts To Know

Function: file_get_contents

file_get_contents offers a very simple way to download a file or image or any URL. You can use it like this:

  $data = file_get_contents("");

Function: explode

Explode takes a string and breaks it up into pieces and places those pieces in an array. So if you have this string:


And you use explode like this:

  explode("-", "MY-LONG-STRING");

You will end with an array like this:

  array (
    0 => 'MY',
    1 => 'LONG',
    2 => 'STRING',

Quiz 1 offers a free IP to location service. Try it by opening these urls in your browser:


Your task is to:

  1. Create a PHP file that accepts an IP address as input.
  2. Use “file_get_contents” to fetch the data from for the ip that is specified as input
  3. Use “explode” to split the result you get from ip2c.
  4. Based on the result, either outpout
    a) the country the ip belongs to, or
    b) an appropriate error

How to deal with SSL in Laravel?

June 16, 2014 at 6:31 am

SSLAs you probably already know, Laravel has a lot of flexibility around “routes” and even more so if you combine it with “filters“. The easiest way to deal with SSL in Laravel is using filters. As apposed to struggling with .htaccess or doing it some other way in code. I created the following two filters and this solved the problem for me:

    Route::filter('force.ssl', function()
        if( ! Request::secure())
                    Request::path().(empty($_SERVER['QUERY_STRING']) ? '' : '?'.$_SERVER['QUERY_STRING'])

    Route::filter('no.ssl', function()
        if( Request::secure())
            return Redirect::to(
                Request::path().(empty($_SERVER['QUERY_STRING']) ? '' : '?'.$_SERVER['QUERY_STRING']), 302, array(), false

You can then use it like this:

Route::group(array('before' => 'no.ssl'), function()
    Route::resource('/', 'SiteController');

I’m sure the “QUERY_STRING” part could probably be replaced with a more Laravel-y way of doing this, but this worked for me.

How to open a port on Centos 6 that has WHM/cPanel installed

May 28, 2014 at 6:14 pm

Firstly, forget ipTables. There’s an easier way. Install “APF”:

  1. Login as Root
  2. Go to your home folder: cd ~
  3. Make a temporary download folder: mkdir downloads
  4. Go into that folder: cd downloads
  5. Download APF: wget
  6. Go into the extract folder: cd apf-9.7-2/
  7. Run the install file: ./

You should see something similar to this:

[root@root]# ./
Installing APF 9.7-2: Completed.

Installation Details:
Install path: /etc/apf/
Config path: /etc/apf/conf.apf
Executable path: /usr/local/sbin/apf

Other Details:
Listening TCP ports: 21,22,25,53,80,110,111,143,443,465,587,631,993,995,2077,2078,2082,2083,2086,2087,2095,2096,3306,43593
Listening UDP ports: 53,111,631,745,764,58037
Note: These ports are not auto-configured; they are simply presented for information purposes. You must manually configure all port options.

APF is now installed, and you can head on over to /etc/apf/conf.apf:

  1. vi /etc/apf/conf.apf
  2. Navigate to the line that reads: # Common inbound (ingress) TCP ports
  3. Below that line, you want to add something like this: IG_TCP_CPORTS=”20,21,22,25,53,80,110,143,443,465,993,995,2082,2083,2086,2087,2095,2096,3306,10000,35000_35999″
  4. Save the file and quit VIM
  5. Stop APF: service apf stop
  6. Start APF again: service apf start

You should be good to go! But, if you want to make sure APF starts on startup, do this:

  1. chkconfig –add apf
  2. chkconfig –level 345 apf on
  3. Restart Centos to make sure it works after the restart: shutdown -r now

Now you should be ready to rock!

Git Cheatsheet

May 21, 2014 at 12:20 pm

gitHere are some of the git commands I use daily (but still tend to forget from time to time):

push a new branch + create it on server: git push -u origin yourBranch
add a tag to a branch git tag <your tag>
push those tags git push –tags

Finally an easy way to test your .htaccess file

April 1, 2014 at 11:31 am

Ever write a long complicated .htaccess file only to not have it work like you thought it would? Then this tool is for you:

Posting an Array to a URL using Curl

April 1, 2014 at 8:51 am

Adding this snippet because I often find myself needing this. This will allow you to grab a url using CURL while posting variables to that URL.

    function makeCurlCall($url, $json = array())
	$ch = curl_init();
        curl_setopt($ch, CURLOPT_VERBOSE, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        if (!empty($json))
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        return curl_exec($ch);

Explode (split) a string on multiple delimiters

March 20, 2014 at 2:34 pm

This is the most elegant solution I’ve found so far:

function explodeX($delimiters,$string) {
return explode(chr(1),str_replace($delimiters,chr(1),$string));

Need to extract e-mails from a large chunk of text? Try this!

March 3, 2014 at 6:27 pm

You can easily extract e-mail addresses from a big chunk of text using this:

Today I broke the space time continuum

February 18, 2014 at 4:38 pm

I broke the space time continuum

Permanent Redirect vs Temporary Redirects in PHP

February 8, 2014 at 9:15 am

This is how you do a standard redirect:

[codesyntax lang=”php”]

And this is how you do a redirect that is permanent:

[codesyntax lang=”php”]<?php
header(“HTTP/1.1 301 Moved Permanently”);