How to add a subscriber using MailChimps API with PHP

How to add a subscriber using MailChimps API v3.0 with PHP and CURL

A quick step-by-step guide with PHP code

Here was the problem, my greeting card website (PamitCards.ca) has a list of subscribers sitting in a MySQL database table. Whenever I wanted to use MailChimp to send the list of subscribers an email. I would have to export the email addresses as a CSV file. Then log-in to MailChimp, import the file so that any new subscribers would be added. MailChimp’s import process would then email me with a list of additions, bounced and un-subscribed email addresses. I would then take those and update the MySQL database table removing the redundant addresses.

Whoa that’s a lot of steps, quite a time consuming process for something that could be automated / integrated!

Realizing that using MailChimp’s API, I could add subscribers to my MailChimp list. Thereby making the above process redundant along with the database table. What’s the point of keeping that data if it’s in MailChimp.

So here’s how to make it work…
  1. If you are reading this, I’ll assume you already have a MailChimp account and at least one list set-up. If not it’s really easy to sign-up and get started with MailChimp.
  2. Then you will need an API key if you don’t have one already, find it under Account > Extras > API keys. Note that running the code locally under WAMP I had to change the https to http to make it work.
  3. Once your key is generated, you will use the last 3 characters as part of the API call, in my case it was us2 to make this url https://us2.api.mailchimp.com/3.0
  4. Then you will need to generate your Auth token, I used Postman which is available in the Chrome Web store to add to your browser.
  5. Using Basic Auth you can put in your Username and Password and this will generate a string that looks like this Basic cGFtaXRjYXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXZGUwNS11czI=
  6. First do a GET call to http://us2.api.mailchimp.com/3.0/lists this will return a list of your lists, find your relevant list id to insert in the code below along with your API key
  7. Then specify an email address to Subscribe and run the code.
  8. You should get a response in the browser showing the the email address has been added
  9. You can extra fields if your list and signup process has them.
  10. Any problems, please let me know with a comment or follow me on Twitter for more helpful posts…
  11. Here’s the PHP code I used:

List of free pomodoro apps to improve your productivity as a web developer

Looking for a pomodoro app, to help keep you on task and productive?

Below is a list of different options for different OS’s

Windows

Mac

Android

Web based / online

iOS

Chrome apps:

  • Pomodo’more is a lightweight app you can add to your Chrome Browser.

You can read more about Pomodoro here: Why breaking time and tasks in to small blocks will increase your productivity as a web developer

Do you really need a custom e-commerce website developed for your small business?

The perfect custom e-commerce website for your business may be best left as a dream for now.

Most small businesses don’t need a custom e-commerce website. When an off the shelf product will work.

If your small business is tight on funds as most small businesses are. You don’t want to tie up your cash flow with a custom e-commerce dream site.

Think of it another way. Say you need to buy a car. Will you buy a car that’s already made and ready to go? Or would you order a custom made car, to your exact specifications? Are you prepared to pay for the extra cost and wait for it to be made for you?

And that’s after the time it will take to find a developer or company to make the site for you.

Almost everyone will choose a ready made car. So choose a ready made, easy to configure e-commerce solution.

There are many options:

  1. Don’t overlook the worlds largest e-commerce platform Amazon
  2. Ebay used to be about auctions, but now many buyers and sellers are just selling at a fixed price Ebay
  3. Etsy if you are selling craft / homemade style things Etsy
  4. Shopify has a great platform and even a POS solution for your small business Shopify
  5. Volusion is a competitor to Shopify Volusion and also has a 14 day free trial.

Don’t forget WordPress, Wix, Weebly and Squarespace also offer e-commerce integrations / plugins, but maybe at the end of the day all you need is a PayPal or Stripe button to accept payments?

So if you are looking to get going with e-commerce for your small business. Consider the options above, to get you transacting online

If you have any questions about the above and need any more help / advice, please contact me.

Do you have enough time for a custom e-commerce website to be developed?

Time, money and patience is required for custom software development.

Custom software development can really hurt, so do your research before committing to it.

Developing a customer e-commerce website can be very time consuming. Do you really need to try and “re-invent the wheel”?

The development process can also be annoying and irritating. More so if you don’t understand the technical aspect. Even seemingly small visual changes can seem to take way more time than you think they should.

You might start thinking…

“Am I being ripped off by my website developer?”

If you haven’t yet embarked down this process, pause and consider your options:

  1. Pay to develop custom software to exactly meet your needs. Which could be a long frustrating expensive journey or
  2. Use an existing e-commerce service like Shopify. Where in a few hours you could have a professional looking site live and ready to take orders.

I’m a software developer. Through years of experience of being paid to develop custom software. I recommend you avoid all the potential pains associated with option 1. Take option 2, start accepting orders today!

Save yourself the pain, take the easy way of using software. That you can then customize (within certain boundaries) for yourself!

If you liked this post about developing a custom e-commerce website, be sure to subscribe below for my next post

How to get started with PHP as a complete newbie

How do you get started with PHP as a complete newbie?

A common question that I get asked is

How do I get started with PHP?

The following is a simple recipe to get you started with PHP.

You’ll need the following basic ingredients for success:

  • A reasonably modern computer running Windows
  • A text editor, notepad for example
  • A web browser, like Chrome or Firefox

Then follow these instructions to cook up your first “hello world” PHP page:

  1. Setup a local webserver on your computer, by installing WAMPSERVER – download from here
  2. Once installed, right click on the WAMPSERVER icon in your Sys Tray and click on “www directory”.
  3. Create a new folder called “Project1”
  4. Enter the folder
  5. Create a new file, called index.php
  6. Edit the file with your text editor, notepad will do
  7. Enter the following code into the file, or copy and paste from here:
  8. <?php echo "Hello World!";
  9. Save the file
  10. Open your web browser and navigate to this URL: http://localhost/Project1
  11. You should see a page with the text “Hello World!” displayed in it
  12. Congratulations you’ve successfully made your first PHP page!

How to use CodeIgniter to prevent malicious SQL injections

Should you escape input when using CodeIgniters active record class?

Do you need to worry about MySQL injection vulnerabilities when using CodeIgniter?

Yes, yes you do.

It is of course good security practice. To escape your data before submitting it into your database.

CodeIgniter provides three helpful methods. Which are part of it’s database library to stop SQL code injection.

$this->db->escape()
$this->db->escape_str()
$this->db->escape_like_str()

CodeIgniter also provides a method to to use Bindings. Whereby the question marks are replaced by the data parameters. That you pass to the query function.

For example:

$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";
$this->db->query($sql, array(3, 'live', 'Rick'));

This way they are escaped so you don’t have to escape them. Resulting in safer queries for you. Stopping attackers from injecting malicious SQL.

Refer to the last two sections of this page of the CodeIgniter manual.

If you liked this tip, be sure to subscribe below and be the first to get my next tip…

How to integrate a WordPress.com blog RSS feed into a PHP page

What comes first your wordpress blog or your website?

You might have started with a free wordpress blog. Then later created a separate website or vice versa. Well whichever way round it is. You might have ended up with two disconnected websites. Now you want to reuse your wordpress.com blog content in your website.

One easy way to do this and integrate your blog content in your website is using its RSS feed. Which is available on your wordpress blog. Just add this query string “?feed=rss” to your blog’s url and you will get an RSS feed from your blog.

Try it now in your web browser, by clicking on this link:

http://element121.com/?feed=rss

Now that you can access the RSS feed, you can parse the RSS data into your page

Last RSS is a PHP RSS parser. Available to download as a single file here: http://lastrss.oslab.net/

So you can now parse the RSS feed. With a few lines of PHP code as demonstrated below. This is based on one of Last RSS’s examples.

If you found this post useful, please subscribe below. If you have any questions, don’t hesitate to leave a comment…

Help for avoiding undefined index notices

If you are just starting out with PHP, you may come across this message:

Notice: Undefined index: username in C:\wamp\www\mysite\index.php on line 99

Even if you’ve been coding for years you can still get this message!

You may be confused as to what an “Undefined index” is? Often seen when trying to access global server variables such as $_POST and $_GET when passing data from another page or posting it back to the same page.

So what is it? It really is an index of an array that hasn’t been defined yet.  You are trying to access data in a variable that doesn’t exist!

Depending on the PHP error reporting level you configure, you can even hide these notices, but that’s not recommended, better to deal with them and write good code.

You can normally fix your “Undefined index” errors with the isset function to check whether a variable is set, like this:

// Check if the username is set or not...
If (isset($_POST['username']))
{
$username = $_POST['username'];
}
else
{
$username = "";
}

However a very useful function which can save you many lines of code like the above and undefined index notices, can be found in the comments (all credit to the author) on the same page of the PHP manual.

function isset_or(&$check, $alternate = NULL)
{
return (isset($check)) ? $check : $alternate;
}

//Example usage which will give the same result as the if / else statement above
$username = isset_or($_POST['username'], "");

It’s a very useful function if you are using plain PHP and need to determine the value of variables. It will also make your code cleaner and easier to read than repeated if / else statements like the above.

How do you deal with stress as a web developer?

Life can be stressful enough, work as a web developer has it’s own unique stresses from dealing with users who don’t know what they are doing, managers who don’t know what they are managing and technology that never stands still.

At times and some days at multiple times in the day the stress can feel too much.  Sometimes you feel like you need to release the built up angst, anger and frustrations.  Venting and releasing the stress in a controlled manner is important for you and everyone around you.

There are times when you can spend hours trying to fix a bug, that can turn out to be a single character (often invisible) that’s out of place causing a bug and stress.

There are lots of options for dealing with stress, but one that is free and should be available to almost anyone, is just getting up and going for a walk.  Preferably in natural surroundings, if you are in a city, try and walk around a nearby park.

Take a break, when you are stuck go for a walk.  Disconnect from your problem, when you come back after your 10-20 minute walk, you will have a fresh perspective.  I do this all the time and it is crucial to my productivity.

It’s amazing how bugs can be solved, by just leaving the code alone, getting outside for a walk for 10-20 minutes and then when you come back, you have the fix and the stress is gone!  Often I find the fix will come to me halfway on the walk, or when I’m on my way back to the office.  When you are desperately trying to fix an urgent problem, it’s amazing how time can fly by, so stop, get up and take a break with a walk and let the solution come to you, while the stress dissipates…

How to protect your PHP source code as a freelancing web developer

How to protect your PHP source code?  You might be worried about giving away your secret sauce  or having it reverse engineered.

Another good reason for wanting to protect your code is delivering the code to a clients server / hosting before final payment has been made for the development of the code.

I have experienced this first hand! I was very surprised to find a delivery of code for sign off was encrypted, I hadn’t seen that before.  Sure enough when we asked the development company they said they would remove the encryption once the final payment had been made (there already had been a number of part payments through the project).

So definitely a useful tool for small shops and freelancers, make it a part of your contract that the source code will be decrypted when fully paid for.

We also had another client who wanted to distribute PHP code on USB thumb sticks.  Not recommended, but it is possible to run a web server like that, using a SQLite database.  Given the accessibility of a USB thumb stick rather than a Linux server, we encrypted the PHP code to protect it.

Here’s a couple of PHP encoders that we’ve used:

So encode, obfuscate and protect your code, if you feel the need, especially if you aren’t 100% confident of a client paying you.

Have you experienced a similar situation before, please share below in the comments.  If you would like more useful tips like this, please subscribe to be updated on new ones.