Using Brevo service on WP Mail SMTP

Brevo (formerly Sendinblue) is one of our recommended mailers in WP Mail SMTP. It’s a transactional email provider with scalable price plans, so it’s suitable for any size of business.

I chose Brevo’s because of its free plan that allows to send up to 300 emails a day. Also, they don’t require to enter my credit card to try it out.

If they perform well and my needs require an upgrade, I can always just switch to a higher plan to increase my sending limits.

All you need to do is generate an API Key and connect it with any plugins that requires email such as forms.

Connection Time Out when connecting to AWS Server EC2 Instance

Did you ever had a hard time figuring out why you cannot connect to your EC2 Instance? And the response you are getting is “Connection Time Out”.

Well, it happened to me. It has been a long time since I connected to that server and I was thinking I must have the wrong username and password but that wasn’t the case.

It only needs a simple fix.

1. Go to your EC2 Instance and check your Security Group
2. Go to your Security Group
3. Check the Inbound tab
4. Check if your IP is in the list and if it’s not, that’s most likely the problem why you are not able to connect to your server.

Just add a rule. Select SSH and add your IP address/32 then SAVE.

You should be able to connect to your server.

Shorten your link using bitly API

Below is the code on how to make use of bitly API but first things first. In order to use the code below, you must create an account in bitly to gain your login and API Key which are both required in the API call. Once you have that setup, copy and paste the code below and you’re ready to get rollin’.

function makeBitlyUrl($url, $login, $apiKey, $format = 'xml', $version = '2.0.1')
{
	//First, you need to create the URL
	$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$apiKey.'&format='.$format;
	
	//Then, get the return data from the API call
	$response = file_get_contents($bitly);
	
	//Parse data depending on your preferred format
	if( strtolower( $format ) == 'json' )
	{
		$json = @json_decode($response,true);
		return $json['results'][$url]['shortUrl'];
	}
	else //Default is XML
	{
		$xml = simplexml_load_string($response);
		return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
	}
}

$shortenedUrl = makeBitlyUrl('https://example.com', 'winnie', 'R_123456789abcde', 'json');

Getting API response with authentication

Oftentimes, file_get_contents() function comes really handy when we are trying to pull data from an API. We can simply do it by doing this:

$json_data = file_get_contents('http://theurl.com/api/json');

But what if the API requires authentication? Then, you’ll need a way to communicate with the authenticated server or else the server will give you this response.

Warning: file_get_contents(http://www.yourdomain.com/file.php): failed to open stream: HTTP request failed! 
HTTP/1.1 401 Authorization Required in file.php on line 7

In order to avoid that, first we need a way to add the username and password to the HTTP header in the request. We can simply use the stream_context_create() function.

Next is to use that stream context in the file_get_contents() function as the third parameter. As for the second parameter, just simply use false or 0 to skip it.


$username = 'theusername';
$password = 'thepassword';

$context = stream_context_create(array(
  'http' => array(
    'header' => "Authorization: Basic " . base64_encode("$username:$password")
  )
));

$json_data = file_get_contents('https://theurl.com/api/json', false, $context);

http://www.hashbangcode.com/blog/using-authentication-and-filegetcontents

Dealing with currency, commas and decimals in PHP

If you are dealing with money on your code, you must know how to display comma when it reaches a thousand or million or billion and so on. You also need to handle the decimal places. Money usually have only 2 decimal places. If the money is a whole number, you still need to add 2 zeroes as decimals.

Good news! PHP has a built-in function that can do this for you. All you have to do is to call number_format

Example 1:

$number = 123456789.12345;
echo number_format($number, 2);

Output 1:

123,456,789.12

Example 2:

$number = 9876;
echo number_format($number, 2);

Output 2:

9,876.00

Protocol Relative URLs

As I was developing an audio player and creating its embed code, I learned that you can use 2 forward slashes as a shorthand whether a file could be loaded from either an http or https protocol. This shorthand is commonly known as protocol relative URLs.

The browser appends the right protocol in front of the URL.

For example,

//example.com/mycss.css

As I was telling earlier, it would prepend the right protocol into this https://example.com/mycss.css

So next time, if you want to load up a URL but not quite sure as to what protocol to use, you can just simply use the shorthand — 2 forward slashes.