24. februar 2011

HP gets down to business with redesigned and refreshed EliteBook and ProBook laptops (hands-on)

HP gets down to business with redesigned and refreshed EliteBook and ProBook laptops (hands-on):

Two weeks ago 'consumers' got totally redesigned HP Pavilion dv and g-series laptops, leaving those poor guys in the conference room with nothing but their 'old' Calpella-based machines and sad Excel spreadsheets. Well, it's their turn now alright -- HP's been stirring up brand new EliteBook and ProBooks for the guys and gals in suits (though, we're really of the mind that these laptops are for anyone looking for some tough and powerful hardware). All ten of the new machines (yep, 10!) have been given new metal designs, Intel's latest Sandy Bridge processors, facial recognition software, and an easy-access latch to get to the hard drive and RAM. And there's more -- hit the jump for a break down and impressions of each of the models, and don't forget to check those galleries below for some shots of these rigs in the flesh.



Zotac's Fusion-based Zbox AD03 Blu-ray HTPC gets reviewed, smiled upon

Zotac's Fusion-based Zbox AD03 Blu-ray HTPC gets reviewed, smiled upon:

While AMD's Zacate E-350 APU has managed to find its way into a number of laptops, it's not exactly simple to find a desktop based on Fusion right now. Thankfully, Zotac was champing at the bit to be one of the first, and its Zbox AD03 / AD03 Plus has just hit the test bench over at Hot Hardware. All told, the results were fairly predictable, with the Fusion APU running laps around the prior Atom-based version. Of course, 'laps' is a relative term, and while it hasn't single-handedly redefined the SFF PC sector, it has provided a real alternative to Intel's stable of underwhelming nettop chips. Across the board, the Zacate E-350 managed to hold its own, with the only real issue being 'lackluster Flash video acceleration.' We're told that updated drivers are expected to remedy that, however, so there's really little to gripe about from a numbers standpoint. As for value proposition? The AD03 Plus (ringing up at $529.99) seems like a worse deal with its skimpy 2GB of RAM and (comparatively sluggish) 250GB HDD, while the barebones AD03 (which lists for $439.99) could be turned into quite the powerhouse with 4GB of memory and a speedy SSD. Head on down to the source link to get your nerd on, but only if you're interested in scratching that DIY itch that's been so bothersome of late.

20. februar 2011

VMware Android handset virtualization hands-on

VMware Android handset virtualization hands-on:



VMware and LG's virtualized Android OS demo at MWC 2011 is pretty slick. The speed of the transition from the owner's OS to the virtualized OS is nothing short of amazing considering it is all running from the device's SD card. As we posted earlier, the potential in the work world are pretty impressive: an employer can simply provision and add the new OS replete with all the security policies, remote wipe capability, security, VPN config, mail settings, and all that magic. Sure, VMware has started with Android and LG, but there's apparently nothing holding them back from offering this on any smartphone platform -- and honestly, we can't wait to see more. But until then, the following video will have to do.

12. februar 2011

Palm TouchPad leaked: 1.5 pounds, 13mm thick, and just moments from official

Palm TouchPad leaked: 1.5 pounds, 13mm thick, and just moments from official:



According to PreCentral, Palm's forthcoming tablet -- which we suspect will be officially revealed in just a few hours -- will be dubbed TouchPad. They were able to sneak in and hear a few details during the event preparation session, enabling them to confirm that it'll weigh 1.5 pounds and measure 13mm thick (just as we'd heard). If those figures sound familiar, it's because a fledgling startup in Cupertino just so happens to have a slate that aligns exactly to those very specifications. Keep it locked right here for our impending liveblog -- we'll be bringing you the blow by blow as it goes down today in San Francisco.

Update: Robert Scoble seems to have let loose a bit of information as well, confirming that a 10-inch slate is indeed on the docket, as well as 'the smallest little phone' he has ever seen. And before you think that's a positive attribute, he also mentions it being too small to be taken seriously, and definitely not as 'a competitor for the iPhone or Android.' Ouch. Guess we'll have to see for ourselves as the day develops.

Update 2: Oh, boy! The HP Veer, Pre 3 and TouchPad just leaked on HP's official site. We're cautiously optimistic that all these will be on sale later today.

Motorola lifts our spirits with new Atrix 4G video

Motorola lifts our spirits with new Atrix 4G video:

Been feeling down this cold and dreary January? Well, let Motorola 'introduce' you to its new smartphone! The CES conqueror we already know as the Atrix 4G has received its first promo video from the mothership, highlighting its laptop and HD multimedia docks (giving us a decent peek at the docked UI for both), dual-core processor and related gaming credentials, and, of course, that gorgeous qHD screen resolution on the handset itself. It's all set to a very uplifting soundtrack, connoting all the beautiful opportunity and potential that our collective smartphone future holds.

Visualized: Google's periodic table of APIs

Visualized: Google's periodic table of APIs:

The world of Google APIs and developer tools can be a confusing one, but the company has now at least brought a bit of order to the chaos with its own take on the periodic table of the elements. As you can see above, Android occupies the top spot normally reserved for hydrogen in the actual periodic table, and the remaining APIs and developer products are all grouped into their appropriate categories -- and, of course, linked to their respective websites. Hit up the link below to check out the table in its interactive form.

9. februar 2011

Palm TouchPad leaked: 1.5 pounds, 13mm thick, and just moments from official

Palm TouchPad leaked: 1.5 pounds, 13mm thick, and just moments from official:



According to PreCentral, Palm's forthcoming tablet -- which we suspect will be officially revealed in just a few hours -- will be dubbed TouchPad. They were able to sneak in and hear a few details during the event preparation session, enabling them to confirm that it'll weigh 1.5 pounds and measure 13mm thick (just as we'd heard). If those figures sound familiar, it's because a fledgling startup in Cupertino just so happens to have a slate that aligns exactly to those very specifications. Keep it locked right here for our impending liveblog -- we'll be bringing you the blow by blow as it goes down today in San Francisco.



Update: Robert Scoble seems to have let loose a bit of information as well, confirming that a 10-inch slate is indeed on the docket, as well as 'the smallest little phone' he has ever seen. And before you think that's a positive attribute, he also mentions it being too small to be taken seriously, and definitely not as 'a competitor for the iPhone or Android.' Ouch. Guess we'll have to see for ourselves as the day develops.



Update 2: Oh, boy! The HP Veer, Pre 3 and TouchPad just leaked on HP's official site. We're cautiously optimistic that all these will be on sale later today.

8. februar 2011

20 Ways to Save Kittens and Learn PHP

20 Ways to Save Kittens and Learn PHP:

There’s an old adage – dating back to the 1700s – which, in English, says: “A kitten dies each time a PHP programmer doesn’t follow best practices.” Okay, not really; but just go with it!


Getting started in PHP can be a daunting experience. With that in mind, these 20 tips will teach you how to follow best practices, and save lives…kitty lives.





0. Program as Often as You Possibly Can



Programming often and with purpose will make the lessons you learn stick.



Did you study a foreign language in school? Studied all the parts of speech, learned the verbs and how to conjugate them, followed along as your teacher said common phrases?


How much of that language do you still speak?


If you’re answer is, “none,” I’m willing to bet it’s due to the fact that you never actually used the language — you only studied it. But if you can still hold up a conversation, it’s likely because you actually spent some time speaking that language outside of the learning environment. Perhaps you spent a year abroad, or worked a job where a second language was necessary?


Whatever the reason, you retained it because you used it in real-life situations and put it into a personal context that is much easier to recall later.



PHP is a foreign language, just like Spanish or French. In order to become comfortable with it, you need to use it outside of the classroom setting. Tutorials and sample projects are great for teaching the fundamentals, but unless you’re applying those concepts to your own projects, it will be much more difficult to apply those fundamentals in context and burn them into your memory.



So, don’t worry that you “don’t know enough” to build a project. When you choose your project, you have a valid reason to research and implement a concept. Programming often and with purpose will make the lessons you learn stick.




1. Get Familiar with the PHP Manual


Every list of tips for beginners has this tip, and for good reason.


Learning to navigate the PHP documentation is the single most useful thing you can do for yourself as a programmer.



If you look in my browser history at the sites I most often visit, the PHP manual will be right at the top. I don’t suspect that will change for as long as PHP remains my programming language of choice.


At first, the manual does look rather daunting — it doesn’t seem to be particularly easy to browse, and the navigation can be a bit awkward at times. However, you’ll get the hang of it quickly.


Perhaps the best thing to know about the PHP manual is that most functions can be looked up using the pattern http://php.net/function-name in your address bar. For example, to look up the strpos() function, use http://php.net/strpos, and for array_key_exists(), use http://php.net/array-key-exists. (NOTE: pay attention to the omission of parentheses and the substitution of hyphens (-) for the underscore (_) in the address.)


1a. Read the Comments!


It's easy to overlook the comments, but do yourself a favor and have a look through them. If you're getting an unexpected result from a function, chances are someone has spotted it and explained it in the comments.


You can also pick up a plethora of great tips and ideas from the developer community by reading through comments.




2. Take Advantage of the Huge Online PHP Community


In addition to the PHP manual, there are wonderful developer communities all over the internet. Some of my personal favorites include StackOverflow.com and the W3Schools.com forum.


Additionally, Twitter is a surprisingly excellent place to post PHP questions. If you tag a tweet with #PHP, it's likely someone in the community will spot it and lend a hand.


A Note About Twitter: Of course, anything that's useful will inevitably be overrun with spammers and those sorry individuals who deeply misunderstood the purpose of social media. If you're going to use Twitter as a support network, you'll probably want to routinely block or hide the accounts which spew job postings or retweet everything that mentions PHP.



Just remember: as you get better, please try to pay it forward. The development community needs everyone to pitch in, and it won't be long before you'll have the ability to answer questions for other beginners. Don't turn a deaf ear.





3. Don't Put Off Best Practices for Later


As you're learning, you're going to hear a lot about 'best practices' in programming; stuff like prepared statements and PEAR coding standards.


Do not put off learning this stuff because it seems hard.



If something is a best practice, it's not because we (meaning other PHP developers) got together and said, 'How can we make life harder for the noobs?'



Best practices exist to keep your scripts secure, fast, and manageable. Learn them as early as you can. In fact, don't even bother learning the wrong way.


It takes just about the exact same amount of learning to figure out mysql_query() as it does to learn PDO or MySQLi. So if you start with your choice of the latter two, you're starting with a strong foundation in database interaction and, really, you've put in less overall effort.


Be sure to browse Nettuts+ for a variety of tutorials on PHP best practices, including prepared statement usage.




4. Don't Put Off Best Practices for Later!


I just wanted to make sure you saw this.



Seriously, folks. Don't take shortcuts. Every time you violate best practices because the right way seems 'too hard,' BP dips a kitten in crude oil.



So if you won't do it for yourself, your projects, your peers, or the advancement of the community at large, at least consider the kittens.




5. Make Code Self-Documenting



If you need to squeeze characters off your variable names to shave .2ms off your script’s execution time, there’s likely a whole different problem going on.



It's tempting, early on, to be 'clever' with your variable and function names. Maybe you read an article about performance, or saw a code snippet that accomplished a ton of work in two lines of code. Maybe you want to create your own 'signature style' of coding. Maybe you just heard that I hate it and you wanted to piss me off.


Whatever your temptation, resist it at all costs.


Consider the following snippet of code:


<?php

$a = b('jason.lengstorf@copterlabs.com');

$c = explode('@', $a);

$d = $c[1];

echo 'The email address ', $a, ' belongs to the domain ', $d, '.';

function b($e) { return htmlentities($e, ENT_QUOTES); }

?>

Does that make any sense to you?


Of course, you can figure out what it does, but why force anyone trying to work in your code to spend the extra 1-5 minutes scratching his head, trying to remember what $c is storing.


So let's take that code and make it self-documenting:


<?php

$email = sanitize_string('jason.lengstorf@copterlabs.com');

$email_pieces = explode('@', $email);

$domain = $email_pieces[1];

echo 'The email address ', $email, ' belongs to the domain ', $domain, '.';

function sanitize_string($string) { return htmlentities($string, ENT_QUOTES); }

?>

There. Much better. Now, just by glancing at the code, you can get the general idea of what's going on. No head-scratching, no muttered curses, and most importantly, no real difference.


Sure, you save a few bytes with short variable names. But, honestly, if you need to squeeze characters off your variable names to shave .2ms off your script's execution time, there's likely a whole different problem going on.




6. Add a Comment to Anything You Had to Think About



Comments are the sign of a competent programmer.



Comments are not the sign of a novice. In fact, as I see more and more code that's not mine, I'm starting to think that comments are the sign of a competent programmer, as they seem to be the only ones doing it.


If your code is self-documenting, you won't require too many comments. However, no matter how clear your variable and function names are, you'll always have spots where the action taken simply isn't that obvious.


When that happens, slap a comment in there. 'Future You' will give 'Present You' a high five when the time comes to update the script.



As a rule of thumb, if you had to stop and think for a few seconds about what needed to happen to make the script work properly, it's probably a good spot for a quick note.



Consider the following:



$pieces = explode('.', $image_name);
$extension = array_pop($pieces);

What does that do? Did you have to stop and think about it? Do you still not know for sure what's stored in $extension?


Look at that snippet again, but with one quick comment:



// Get the extension off the image filename
$pieces = explode('.', $image_name); $extension = array_pop($pieces);

Now, even if you don't know how or why that code works, you at least know that $extension refers specifically to an image extension. If that saves 'Future You' or another developer five seconds of processing the script's intent, it was well worth your ten seconds of effort to add the comment in the first place.


As with most things, moderation is key. Too few comments and you risk leaving the next developer (or Future You) puzzled by a code snippet, which can even lead to accidental breaking of code because the solution, without explanation, might look silly or superfluous. Too many and it becomes too difficult to scan through your code, which is equally frustrating.


Moderation is key.




7. Learn Docblock and Use It


If I could be sure every developer in the world would do one thing with absolute consistency, I think it would be the use of the Docblock commenting standard.


I have a few reasons for my strong support of Docblock:



  1. It requires one to think about the what and why for each file, function, method, and so on.

  2. It gives a clear description of the expected types for parameters and return values in functions/methods

  3. It provides a quick description of what the code does

  4. When coupled with one of the many IDEs that support Docblock, it creates code hinting (which allows you to see a description and expected parameters/return values for the method or function you're using)


This tip does border on the upper level of beginner, but I group this under 'best practices to be learned as quickly as possible.' Feel free to skip it, but before you do, think about the kittens.


Docblock shows its versatility best when used to document a class:



/**
* A simple class to get the sum or difference of $_foo and a value
*
* @author Jason Lengstorf <jason.lengstorf@copterlabs.com>
* @copyright 2011 Copter Labs
* @license http://www.opensource.org/licenses/mit-license.html
*/

class CopterLabs_Test
{

/**
* The value to use in addition and subtraction
* @var int
*/
private $_foo = 0;

/**
* Adds a value to $_foo and returns the sum *
* @param int $add_me The value to add to $_foo
* @return int The sum of $_foo and $add_me
*/
public function add_to_foo( $add_me=0 )
{
return $this->_foo += $add_me;
}

/**
* Subtracts a value from $_foo and returns the difference
* @param int $subtract_me The value to subtract from $_foo
* @return int The difference of $_foo and $subtract_me
*/
public function subtract_from_foo( $subtract_me=0 )
{
return $this->_foo -= $subtract_me;
}

}

At first it might look overwhelming, but the benefits are very much worth taking the time to familiarize yourself with the syntax.


The above Docblock, when used in Netbeans, will generate the following code hint:


Code hinting in Netbeans



8. Don't Be Too Hardcore to Use an IDE


If you don't already know the type, you will soon: the developers who think real programmers don't use IDEs.


Now, look: if you want to impress people, learn to juggle. Refusing to use anything but Emacs in the command line to write scripts will not get you chicks or grant you instant hacker status; it will, however, hang a sign on your forehead warning your coworkers that you are, in fact, That Guy.


Don't be That Guy.



There is nothing wrong with using software to give you on-the-fly syntax highlighting, error-checking, and code hints.



How in-depth your IDE goes is entirely up to you. Personally, I really like Netbeans. I've heard tons of praise for Coda for Mac (though it's not really an IDE), and I previously used Eclipse before moving to Netbeans.


Whatever IDE you choose, you'll see your coding speed increase and your facepalm-worthy bugs decrease. Further, as you expand your code library, you'll have code hinting for all of your custom software. (Because you're using Docblock, right? Right?!)


Don't think IDEs are uncool -- no matter what 'That Guy' tries to tell you.




9. Group Common Code Into Functions



If you see an action repeated, it's time to strongly consider moving that code into a function.



When you first start programming, it's easy to start at the top of the page and work down, adding each piece of code right where it's needed.


However, when you code this way, you'll begin to notice that certain pieces of code are appearing over and over again. This is a minefield when it comes to maintenance and upgrades, because you have to hunt through each file for every occurrence of that action to change its functionality.


If you see an action repeated, even if it's only twice, it's time to strongly consider moving that code into a function.


Consider the following for example:


$unclean1 = '<a href="javascript:alert(\'Holy Crap!\');">Click Me!</a>';

$detagged1 = strip_tags($unclean1);
$deslashed1 = stripslashes($detagged1);
$clean1 = htmlentities($deslashed1, ENT_QUOTES, 'UTF-8');

$unclean2 = "Let's call Björn!";

$detagged2 = strip_tags($unclean2);
$deslashed2 = stripslashes($detagged2);
$clean2 = htmlentities($deslashed2, ENT_QUOTES, 'UTF-8');

echo $clean1, "<br />", $clean2;

As you can see, both of those strings required a few steps before they could be considered safe to use. However, you'll also notice that those same steps could be considered necessary for every bit of information that is passed to the script.


This is an instance where using a function instead is far more desirable:



$unclean1 = '<a href="javascript:alert(\'Holy Crap!\');">Click Me!</a>'; $unclean2 = "Let's call Björn!";

$clean1 = sanitize_input($unclean1);
$clean2 = sanitize_input($unclean2);

echo $clean1, "<br />", $clean2;

function sanitize_input( $input )
{
$detagged = strip_tags($input);
$deslashed = stripslashes($detagged);
return htmlentities($deslashed, ENT_QUOTES, 'UTF-8');
}

By wrapping the common code in a function, it's a bit easier to see what's going on, and it's much easier to edit the steps you want to take when sanitizing your input.




10. Group Related Functions Into Classes


Getting a handle on OOP is another one of those things that I file under 'best practices to learn as quickly as possible.'



If you have a handful of functions that all deal with database actions, you can save yourself a lot of time and effort by grouping them into classes.



Learning object-oriented programming is definitely outside the scope of this list, but I felt it was definitely worth mentioning in this beginners' list.




11. Use Constants, Not Globals



PHP allows you to define your own constants with the function define().



When I first started programming on larger projects, I found myself using global variables more often than seemed necessary or reasonable. Admitting you have a problem is the first step.


I was storing things like application-wide data (such as the site's name or the maximum image width) and database credentials in variables, and I found myself required to use the $GLOBALS superglobal to access this information.


Then I realized PHP allows you to define your own constants with the function define().


A constant is a great way to store information like the aforementioned app-wide data and database info. An additional bonus is that it can't be modified, so you can't accidentally overwrite your database password somewhere later in the script.


As a matter of best practices, the use of globals is generally discouraged to begin with, so the use of constants is preferred anyways. Review the following code for an example and see for yourself:


<?php

define('FOO', 'constant value');

$bar = 'global value';

echo baz();

function baz()
{
$constant = ' Constant: ' . FOO;
$global = 'Global: ' . $GLOBALS['bar'];

return $constant . "<br />\n" . $global;
}

?>



12. Don't Be Afraid to Use Includes


Often, as you're building larger products, it will make a lot of sense to break it apart into smaller chunks, or include files.


A generally accepted way to look at includes is to put any bit of code that will be used in multiple scripts into an include file (such as your database connection details, the header and footer data that is common across the whole site, utility functions like your input sanitization actions, etc.) so it can be pulled in by the file that needs it, rather than being copy-pasted.


For example, on a site with multiple pages, a standard template may emerge that looks something like this:


<?php

// Application-wide data and database connection
require_once 'constants.inc.php';
require_once 'database.inc.php';

// Utility functions
require_once 'utilities.inc.php';

// Header markup
require_once 'header.inc.php';

/* * Page-specific processing goes here */

// Footer markup
require_once 'footer.inc.php';

?>



13. Don't Obsess Over Performance


This is a point of near-paralysis for some developers, and it's really too bad; there is a blurry line between writing efficient code and wasting time trying to squeeze an extra 5ms out of a script's execution.


Definitely read a few performance articles and learn some of the major pitfalls that can drag your scripts to a slow crawl, but don't waste extra time refactoring your code to change double quotes to single quotes because you found out it was a tiny fraction faster.



Use your head, avoid the big problems, and keep your ears open, in case a tip you've never heard comes along to speed up your code, but don't make it a race.



No one can tell the difference between a 25ms page load and a 40ms page load. Make sure it's not 700ms and move on to more important things.




14. Avoid Marrying HTML to Your Scripts


This can be tricky, but do your best to avoid tangling up your HTML markup in your PHP. It's nearly impossible to get away from it completely, but try to make sure that you don't include any non-essential HTML markup in your code.


Consider the following:



echo '<div class="example-div"><p>This is some test content.</p></div>';

Was it necessary for that code to wrap the paragraph tag in a div? Could it have been modified to only include the paragraph tag that holds the text? Have a look at an alternative solution:



<div class="example-div">
<?php echo '<p>This is some test content.</p>'; ?>
</div>

Note: This example is grossly oversimplified. Generating HTML with PHP is usually more of an issue when dealing with a complex function or method that organizes a dataset. The point I'm trying to make is that it can sometimes be tempting to include more markup than is necessary in the output.


In most cases you can keep the HTML outside the PHP, which makes things easier to read and, usually, easier to work with as well.




15. Try to Use at Least One Unfamiliar Concept in Every Project



Push yourself outside your comfort zone.



You're never going to learn if you keep doing the same old thing. Try out a concept that you're not quite comfortable with on every project you possibly can.


Don't be over-ambitious, but definitely push yourself outside your comfort zone. It gives you a challenge, saves you from getting bored doing the same old thing over and over again, and forces you to progress as a developer.


Look at the project, find all the bits that you know well (or at least well enough), and then pick an area you'd like to understand. Sign up for it. Then do it.




16. Don't Be Too Proud to Change


You will be wrong. Frequently. But that's not a bad thing.


As you improve, you'll find newer, better solutions to problems that you've faced in the past. Don't feel stupid; you're learning.


But it's extremely important that you don't become attached to the code you write. Don't think your way is better just because it's your way. If you happen across a great solution that makes yours look like a Band-Aid on a bullet wound, use it! Pay attention to what's different and what you did that could have been better. File that away under, 'Things I've Learned.'



Never allow yourself to believe that an inelegant solution is acceptable because it's yours. That's hubris (which, if you're not aware, doesn't generally result in happy fun times).





17. Validate


If you're a web programmer, start becoming familiar with input validation as soon as possible.


Keep in mind: validation is quite different from sanitization.



Input validation is the practice of making sure data matches the format you've requested, like checking an email field for a valid email address or ensuring that a submitted username is 8-20 alphanumeric characters.



It can be tedious and a pain in the ass, but making sure that only valid data makes it through your processing scripts will enhance the user experience and avoid a lot of bugs in the scripts that have to use the data later on.




18. Whitelists Instead of Blacklists



If you're not on top of your blacklist, vulnerabilities appear.



In plenty of situations, you'll want to block or get rid of certain tags, words, email addresses, or other various bits of data.


A common solution is to use a blacklist: a collection of the tags, terms, etc. that aren't allowed.


This poses a problem, however; you have to be more clever than the person trying to do something naughty. For instance, in the case of disabling JavaScript in posts, you might blacklist the onclick attribute, as well as most of the event attributes, but what if you forget one? What if a new one is added to the spec sometime in the future?


If you're not on top of your blacklist, vulnerabilities appear.


However, to save headache later, use a whitelist whenever possible. A whitelist is the opposite of a blacklist: a collection of allowed tags, terms, etc.


For instance, in the strip_tags() function, you can provide a whitelist to specify which tags are allowed in strings:



strip_tags($string, '<em><strong><tt>');

Now your problem is most likely going to be that you can actually do less than you wanted, but that's far safer and usually less of an emergency to handle.


You can't get away with it in every situation, but saying what is allowed vs. what isn't will provide you with more confidence and control over your scripts.




19. Learn to Count Like a Computer


Are you looking for tip #20? Remember that in nearly all cases, counts in PHP start at 0, so this is actually the 20th tip. You'll find this to be the case in most languages; don't let this one trip you up!




Summary


If you're a beginner, the tips covered above will help you take great strides toward good habits and best practices. Don't get overwhelmed if all of this is news to you; take things one step at a time (see tip #15).


Want to talk specifics? Discuss this post on the forums.



"

How to Use the Latest Updates to the Marketplace API

How to Use the Latest Updates to the Marketplace API:

The Envato Marketplaces are exploding, with more than 600k members and over 65k files!

A great addition to the marketplaces is the API, which is becoming more popular everyday, thanks to the latest updates! Our awesome marketplace devs are constantly listening to community feedback, and have added some sweet features to the edge release!

Be sure to check out some of the applications available around the web — all powered by the Envato API.

Today, we’ll take a look at the latest API features! Join me after the jump!



What is an API?

An Application Programming Interface (API) is a particular set of rules and specifications that a software program can follow to access and make use of the services and resources provided by another particular software program. It serves as an interface between different software programs and facilitates their interaction, similar to the way the user interface facilitates interaction between humans and computers.

Web API

Web APIs allow for the combination of multiple services into new applications, known as mashups.

When used in the context of web development, an API is typically a defined set of Hypertext Transfer Protocol (HTTP) request messages, along with a definition of the structure of response messages, which is usually in an Extensible Markup Language (XML) or JavaScript Object Notation (JSON) format.


While “Web API” is virtually a synonym for web service, the recent trend has been moving away from Simple Object Access Protocol (SOAP) based services, toward more direct Representational State Transfer (REST) style communications. Web APIs allow for the combination of multiple services into new applications known as mashups.



Are APIs Used Much?

Most developers will know the answer to this question, but for the others, the answer is a resounding YES.

API’s are found everywhere; they’re partcicularly popular in the social networking sector! Most large networks, including Twitter and Facebook, provide APIs to interact with their service.



New API Features

In this tutorial, we are only going to take a look at the new API features. If you want to learn more about the existing API features, be sure to check out Drew’s tutorial on the topic.

Public

  • item – Details for a single item, specified by the given ID.

  • item-prices – Return available licences and prices for the given item ID.

  • user-items-by-site – Show the number of items an author has for sale on each site. Requires a username, e.g. collis.

Private

  • verify-purchase – Details of a purchase. Requires a purchase code, e.g. verify-purchase:550e8400-e29b-41d4-a716-446655440000.

  • download-purchase – URL to download purchase. Requires a purchase code, e.g. download-purchase:550e8400-e29b-41d4-a716-446655440000



Requirements



Tutorial image

In order to get started with the API, you will need an Envato Marketplace account and an API key.


You can retrieve your API key by visiting your account settings page.



Tutorial image

We’ll be using a PHP function, called json_decode; this allows us to retrieve the API data and convert it to an array. This function is included in PHP 5 >= 5.2.0.


Got everything? Let’s get started!




Step 1 - Building our Class


Let’s begin by building a simple PHP class, which will allow us to retrieve data from the API.


Create a new project folder and create another directory called classes, to keep everything organized. Inside this folder, add a new PHP file called envato.api.class.php


First, we’ll construct the basic skeleton of our class:



<?php

/**
* Envato API
*
* An PHP class to interact with the Envato Marketplace API
*
* @author Philo Hermans
* @copyright Copyright (c) 2011 NETTUTS+
* @link http://net.tutsplus.com
*/

/**
* envatoAPI
*
* Create our new class called "envatoAPI"
*/
class envatoAPI{

}

?>



Step 2 - Global Variables


We need a couple of variables inside our class to make it flexible. To figure out which variables we need, we have to take a look at the API url formatting.



  • Public – http://marketplace.envato.com/api/edge/set.json

  • Private – http://marketplace.envato.com/api/edge/username/api-key/set.json


As you can see, we need a total of four variables:



  • API Url

  • Set

  • Username

  • API Key


Let’s create these private variables like so:



class envatoAPI{

private $api_url = 'http://marketplace.envato.com/api/edge/'; // Default URL
private $api_set; // This will hold the chosen API set like "user-items-by-site"
private $username; // The username of the author only needed to access the private sets
private $api_key; // The api key of the author only needed to access the private sets

}



Step 3 - Setter & Getter Methods


I like to use setter and getter methods when building classes in PHP. So what exactly are these methods? I’ve made a small example below:



$API = new envatoAPI();

$API->api_url = 'http://nettuts.com';
echo $API->api_url ;

The code above will not work and should return the following error:


Fatal error: Cannot access private property envatoAPI::$api_url

Because the variable is set to private, we cannot access or change it. If we need to change the API url without editing the class file, we could do something along the lines of:



$API = new envatoAPI();

$API->set_api_url('http://nettuts.com');
echo $API->get_api_url();

Now switch back to the PHP class and add the function, set_api_url.



/**
* set_api_url()
*
* Set the API URL
*
* @access public
* @param string
* @return void
*/
public function set_api_url($url)
{
$this->api_url = $url;
}

Since this function is within our class, we can access and change the private api_url variable. Now in this tutorial, we don’t really need getters, but let’s still create one to provide you with a better understanding of the concept.



/**
* get_api_url()
*
* Return the API URL
*
* @access public
* @return string
*/
public function get_api_url()
{
return $this->api_url;
}

As you may have inferred at this point, we don’t need a get function for the API key for example. This makes our class more secure.




Step 4 - Completing the Rest of the Setters


We already finished a setter, so there are three remaining and all of them have the same structure.


Set Username



/**
* set_username()
*
* Set the Username
*
* @access public
* @param string
* @return void
*/
public function set_username($username)
{
$this->username = $username;
}

Set API Key



/**
* set_api_key()
*
* Set the API key
*
* @access public
* @param string
* @return void
*/
public function set_api_key($api_key)
{
$this->api_key = $api_key;
}

Set API Set



/**
* set_api_set()
*
* Set the API set
*
* @access public
* @param string
* @return void
*/
public function set_api_set($api_set)
{
$this->api_set = $api_set;
}

We are now able to set all variables like so:



<?php

include('classes/envato.api.class.php');

$API = new envatoAPI();

// $API->set_api_url(); - We don't need to change the API URL
// $API->set_api_key('ahdio270410ayap20hkdooxaadht5s'); - We only need to set the API key to fetch data from the private set
//$API->set_username('JohnDoe'); // - We only need to set the Envato Marketplace Username to fetch data from the private set
$API->set_api_set('popular:codecanyon'); // Set the API set to request

?>



Step 4 - Request Data


Let’s build a function that will request the data from the Envato Marketplace API. We begin by creating a new public function called request.



/**
* request()
*
* Request data from the API
*
* @access public
* @param void
* @return array
*/
public function request()
{
}

We can use the same technique used in the previous tutorial. To request the data from the API, we can use cURL. Let’s start of by building the API URL — if the username and API key are set, we require a different url.



if(!empty($this->username) && !empty($this->api_key))
{
// Build the private url
$this->api_url .= $this->username . '/'.$this->api_key.'/'.$this->api_set . '.json'; // Sample: http://marketplace.envato.com/api/edge/JohnDoe/ahdio270410ayap20hkdooxaadht5s/popular:codecanyon.json
}
else
{
// Build the public url
$this->api_url .= $this->api_set . '.json'; // Sample: http://marketplace.envato.com/api/edge/popular:codecanyon.json
}

We send a request to the API using cURL like so:



$ch = curl_init($this->api_url); // Initialize a cURL session & set the API URL
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); //The number of seconds to wait while trying to connect
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the transfer as a string instead of outputting it out directly.
$ch_data = curl_exec($ch); // Perform a cURL session
curl_close($ch); //Close a cURL session

We now have a variable, called $ch_data which contains a JSON formatted string. Before we can do something with this, we need to decode it into a array.



// Check if the variable contains data
if(!empty($ch_data))
{
return json_decode($ch_data, true); // Decode the requested data into an array
}
else
{
return('We are unable to retrieve any information from the API.'); // Return error message
}

The function in its entirety looks like so:



public function request()
{

if(!empty($this->username) && !empty($this->api_key))
{
// Build the private url
$this->api_url .= $this->username . '/'.$this->api_key.'/'.$this->api_set . '.json';
}
else
{
// Build the public url
$this->api_url .= $this->api_set . '.json';
}

$ch = curl_init($this->api_url); // Initialize a cURL session & set the API URL
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); //The number of seconds to wait while trying to connect
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the transfer as a string instead of outputting it out directly.
$ch_data = curl_exec($ch); // Perform a cURL session
curl_close($ch); //Close a cURL session

// Check if the variable contains data
if(!empty($ch_data))
{
return json_decode($ch_data, true); // Decode the requested data into an array
}
else
{
return('We are unable to retrieve any information from the API.'); // Return error message
}

}



Step 5 - Testing the Function


Let’s try our class and request some data from the API:



<?php
include('classes/envato.api.class.php');

$API = new envatoAPI();

//$API->set_api_url();
//$API->set_api_key('ahdio270410ayap20hkdooxaadht5s');
//$API->set_username('JohnDoe'); // Envato username
$API->set_api_set('popular:codecanyon'); // Set the API set to request

echo '<pre>';
print_r( $API->request() );
echo '</pre>';
?>


Tutorial image



Step 6 - Testing out the New Features of the Public API


Now that our class is working, it is time to build some examples with the new API features.


Item


To request information about a marketplace item, we must supply the API with an item ID. You can find the item ID within the URL of the marketplace item.



Tutorial image

Now that we have our ID, let’s request the item information:



<?php

include('classes/envato.api.class.php');

$API = new envatoAPI();
$API->set_api_set('item:147510'); // Set the API set to request

echo '<pre>';
print_r( $API->request() );
echo '</pre>';
?>

The following data should be returned:



Array
(
[item] => Array
(
[sales] => 16
[rating] => 5
[cost] => 25.00
[user] => Philo01
[uploaded_on] => Tue Dec 21 03:13:24 +1100 2010
[url] => http://codecanyon.net/item/wordpress-car-dealer/147510
[live_preview_url] => http://s3.envato.com/files/1410781/1_preview.__large_preview.jpg
[thumbnail] => http://s3.envato.com/files/1410780/Car_dealer_avatar.jpg
[tags] => easy to use, easy to setup, car inventory, car dealer, dealership, 2 build in sliders, native post type & taxonomies, over 35 settings, list all your cars, inventory system
[id] => 147510
[item] => WordPress Car Dealer
)

)

Now let’s build a simple page that will display the information above in a way that other users will understand it. First, we need to assign the API data to a variable:



$API = new envatoAPI();
$API->set_api_set('item:147510'); // Set the API set to request

$data = $API->request();

We can now display data from the API in the following format: $data['item']['key'].



<?php

include('classes/envato.api.class.php');

$API = new envatoAPI();
$API->set_api_set('item:147510'); // Set the API set to request

$data = $API->request();

?>

<h2><a href="<?php echo $data['item']['url']; ?>"><?php echo $data['item']['item']; ?></a></h2>
<img src="<?php echo $data['item']['live_preview_url']; ?>" alt="" />
<p>By <?php echo $data['item']['user']; ?>, available for only <?php echo $data['item']['cost']; ?>!</p>

The HTML after the PHP is parsed:



<h2><a href="http://codecanyon.net/item/wordpress-car-dealer/147510">WordPress Car Dealer</a></h2>
<img src="http://s3.envato.com/files/1410781/1_preview.__large_preview.jpg" alt="" />
<p>By Philo01, available for only 25.00!</p>


Tutorial image

item-prices


Instead of creating a new API request to find out more about the item price and license, we can simply add it to the current API set.



$API = new envatoAPI();
$API->set_api_set('item:147510+item-prices:147510'); // Set the API set to request

$data = $API->request();

The API’s response is below:



[item-prices] => Array
(
[0] => Array
(
[licence] => Regular Licence
[price] => 25.00
)

[1] => Array
(
[licence] => Extended License
[price] => 175.00
)

)

Now we can use this returned information in our item page.



<h2><a href="<?php echo $data['item']['url']; ?>"><?php echo $data['item']['item']; ?></a></h2>
<img src="<?php echo $data['item']['live_preview_url']; ?>" alt="" />
<p>By <?php echo $data['item']['user']; ?></p>

<p><strong>Pricing:</strong></p>
<ul>
<?php
// Loop trough all the licenses & prices
foreach( $data['item-prices'] as $p)
{
echo '<li>' . $p['licence'] . ' - ${body}#39; . $p['price'] . '</li>';
}
?>
</ul>


Tutorial image

user-items-by-site


To finish it off, we are going to append the number of items the user has. We can simply add it to the API set.



$API->set_api_set('item:147510+item-prices:147510+user-items-by-site:Philo01');

And the response:



[user-items-by-site] => Array
(
[0] => Array
(
[site] => ThemeForest
[items] => 1
)

[1] => Array
(
[site] => CodeCanyon
[items] => 5
)

)

Let’s add this information to our page.



<h2><a href="<?php echo $data['item']['url']; ?>"><?php echo $data['item']['item']; ?></a></h2>
<img src="<?php echo $data['item']['live_preview_url']; ?>" alt="" />
<p>By <?php echo $data['item']['user']; ?></p>

<p><strong>Pricing:</strong></p>
<ul>
<?php
// Loop trough all the licenses & prices
foreach( $data['item-prices'] as $p)
{
echo '<li>' . $p['licence'] . ' - ${body}#39; . $p['price'] . '</li>';
}
?>
</ul>

<p><strong><?php echo $data['item']['user']; ?> has more items at the Envato Marketplace</strong></p>
<ul>
<?php
// Loop trough all the marketplaces
foreach( $data['user-items-by-site'] as $p)
{
echo '<li>' . $p['items'] . ' at ' . $p['site'] . '</li>';
}
?>
</ul>


Tutorial image



Step 7 – Testing the New Features of the Private API


In order to use the private API features, you will need a marketplace account and a purchase code from one of your customers. We first need to define this information.



$API->set_api_key('ahdio270410ayap20hkdooxaadht5s');
$API->set_username('JohnDoe');

verify-purchase


This will automatically change the API URL from public to the private set. To make things a bit more dynamic, we can create a little form where the user can enter some information to verify his purchase.


Note: When building your own website, don’t use inline styles like this!



<style>
form{
background: #f8f8f8;
width: 300px;
border: 1px solid #e2e2e2;
}
form fieldset{
border: 0;
}
form label{
display: block;
padding: 3px;
}
form input[type=text]{
padding: 3px;
border: 1px solid #d7d7d7;
}
</style>

<h2>Verify Purchase</h2>

<form action="" method="post">
<fieldset>
<p><label>Marketplace Username:</label>
<input type="text" name="username" value="" /></p>

<p><label>Item ID:</label>
<input type="text" name="item_id" value="" /></p>

<p><label>Purchase code:</label>
<input type="text" name="purchase_code" value="" /></p>

<p><input type="submit" name="submit" value="Verify" /></p>
</fieldset>
</form>

This gives us a basic form like so:



Tutorial image

Now let’s get to the PHP part.


First, we need to check whether the form has been submitted with the requisite fields filled. If you are using this on a live website, remember to add more validation to make things more secure.



if(isset($_POST['submit']))
{

// Assign data to variables
$username = $_POST['username'];
$item_id = $_POST['item_id'];
$purchase_code = $_POST['purchase_code'];

// Check if all fields are filled in
if(!empty($username) && !empty($item_id) && !empty($purchase_code))
{
// API Request
}
}

And now the API request.



if(isset($_POST['submit']))
{

// Assign data to variables
$username = $_POST['username'];
$item_id = $_POST['item_id'];
$purchase_code = $_POST['purchase_code'];

// Check if all fields are filled in
if(!empty($username) && !empty($item_id) && !empty($purchase_code))
{
$API = new envatoAPI();
$API->set_api_key('ahdio270410ayap20hkdooxaadht5s');
$API->set_username('JohnDoe');
$API->set_api_set('verify-purchase:' . $purchase_code);

$data = $API->request();
}
}

A valid API response will look like so:



Array
(
[verify-purchase] => Array
(
[licence] => Regular Licence
[item_id] => 147510
[buyer] => EnvatoDemo
[created_at] => Thu Dec 31 08:28:03 +1100 2009
[item_name] => WordPress Car Dealer
)

)

If the request is invalid, the API will return an empty array. If we get a response from the API, it means that the purchase code is correct. In the interest of making it more secure, we’ll match it to the entered item id and username.



if(isset($_POST['submit']))
{

// Assign data to variables
$username = $_POST['username'];
$item_id = $_POST['item_id'];
$purchase_code = $_POST['purchase_code'];

// Check if all fields are filled in
if(!empty($username) && !empty($item_id) && !empty($purchase_code))
{
$API = new envatoAPI();
$API->set_api_key('ahdio270410ayap20hkdooxaadht5s');
$API->set_username('JohnDoe');
$API->set_api_set('verify-purchase:' . $purchase_code);

$data = $API->request();

if(!empty($data))
{
// We got a valid API response let's match the item id and the username
if($data['verify-purchase']['item_id'] == $item_id && $data['verify-purchase']['buyer'] == $username)
{
// Everything seems to be correct! Purchase verified!
// Show some info like purchase date and licence
echo '<p>You have purchased the item on ' . $data['verify-purchase']['created_at'] . ' with the ' . $data['verify-purchase']['licence'] . '!</p>';
}

}
else{
// Response from the API was empty, return error
echo '<p>Sorry, we are unable to verify your purchase.</p>';
}

}
}


Tutorial image

download-purchase


The download-purchase set allows you to download your own purchases without visiting the marketplace.


Note: The download purchase only works when you are using your own API key and a purchase code of a product that you’ve purchased.




<form action="" method="post">
<fieldset>
<p><label>Marketplace Username:</label>
<input type="text" name="username" value="" /></p>

<p><label>Item ID:</label>
<input type="text" name="item_id" value="" /></p>

<p><label>Purchase code:</label>
<input type="text" name="purchase_code" value="" /></p>

<p><input type="checkbox" name="download" value="1" /> Download Item</p>

<p><input type="submit" name="submit" value="Verify" /></p>
</fieldset>
</form>

Now that we have our checkbox, let’s make some changes to the verification process.


We’ll begin by creating a new variable.



// Assign data to variables
$username = $_POST['username'];
$item_id = $_POST['item_id'];
$purchase_code = $_POST['purchase_code'];
$download = (isset($_POST['download'])) ? $_POST['download'] : '';

We also need to make some changes to the remaining code:



if(isset($_POST['submit']))
{

// Assign data to variables
$username = $_POST['username'];
$item_id = $_POST['item_id'];
$purchase_code = $_POST['purchase_code'];
$download = (isset($_POST['download'])) ? $_POST['download'] : '';

// Check if all fields are filled in
if(!empty($username) && !empty($item_id) && !empty($purchase_code))
{
$API = new envatoAPI();
$API->set_api_key('ahdio270410ayap20hkdooxaadht5s');
$API->set_username('JohnDoe');

// If download is checked request the download-purchase set
if(!empty($download))
{
$API->set_api_set('download-purchase:' . $purchase_code);
$data = $API->request();

// Set the header location to the download URL provided by the API
header("Location: " . $data['download-purchase']['download_url']);
}
else
{

// The download checkbox was not checked so verify the purchase
$API->set_api_set('verify-purchase:' . $purchase_code);
$data = $API->request();

if(!empty($data))
{
// We got a valid API response let's match the item id and the username
if($data['verify-purchase']['item_id'] == $item_id && $data['verify-purchase']['buyer'] == $username)
{

// Everything seems to be correct! Purchase verified!
// Show some info like purchase date and licence
echo '<p>You have purchased the item on ' . $data['verify-purchase']['created_at'] . ' with the ' . $data['verify-purchase']['licence'] . '!</p>';
}

}
else
{
// Response from the API was empty, return error
echo '<p>Sorry, we are unable to verify your purchase.</p>';
}
}

}
}

With those changes in place, the response should look like so:



Array
(
[download-purchase] => Array
(
[download_url] => http://download/ourfile.zip?AWSAccessKeyId=12345&Expires=6789&Signature=HAoYF962%53faf
)

)



Conclusion


I hope you’ve learned a bit about Envato’s marketplace API. If so, then put this knowledge to use and build some sweet applications! Thank you so much for reading, and let us know if you have any questions or concerns.


How to Send Text Messages with PHP

How to Send Text Messages with PHP:

Text messaging has become extremely widespread throughout the world — to the point where an increasing number of web applications have integrated SMS to notify users of events, sales or coupons directly through their mobile devices.

In this tutorial, we will cover the fundamentals of sending text messages with PHP.


Overview

Sending a text message (SMS) message is actually pretty easy.

Below is a simplified diagram of how a message can be sent from a web application to a wireless device.



We’ll break this down — one piece at a time:

  • The message is composed using a web application that is stored and executed on a HTTP server and then sent through the internet (“the cloud”) as an email message.

  • The email is received by a Short Message Service Gateway (SMS Gateway), which converts the message from an email message to a SMS message.

  • The SMS message is then handed to a Short Message Service Center (SMSC), which is a server that routes data to specific mobile devices.

  • The message is finally transmitted over the wireless network to the recipient.

Most wireless networks have a SMS gateway through which email messages can be sent as text messages to a mobile device. This is nice, because, from a developer’s standpoint, it is generally free—however, it is of course not a free service for the end user. Fees still apply to the recipient of the message and messages sent via email will be billed as a non-network text message.



Email to SMS

To send a SMS via email, you’ll generally require only two things:

  • The phone number or unique identifier of the mobile device you want to reach.

  • And the wireless network’s domain name (many can be found in this list of email to SMS addresses)

The following convention can be followed for most carriers:

phoneNumber@domainName.com

phoneNumber is the phone number of the mobile device to send the message to, and domainName.com is the address for the network’s SMS Gateway.

To send a text to Mr. Example, you could simply add 3855550168@vtext.com to any email client, type a message and hit send. This will send a text message to phone number +1 (385) 555-0168 on the Verizon Wireless Network.

For example, I’ll send a text message to myself using Gmail.



When my phone receives the message, it should look like so:



Pretty awesome!



PHP’s mail Function

Let’s take things a step further. Using the SMS Gateway, we can send a text message via email using PHP’s mail function. The mail function has the following signature:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

You can read more about it here.



  • $to defines the receiver or receivers of the message. Valid examples include:

    • user@example.com

    • user1@example.com, user2@example.com

    • User <user@example.com>

    • User1 <user1@example.com>, User2 <user2@example.com>



  • $subject is rather self explanatory; it should be a string containing the desired subject. However, SMS do not require a subject.

  • $message is the message to be delivered. As mentioned in the PHP manual, “each line should be separated with a LF (\n). Lines should not be larger than 70 characters.”

To replicate the earlier functionality, we could write the following PHP code:

mail( '3855550168@vtext.com', '', 'Testing' );


A Test Drive

Let’s run a test with PHP to make that sure everything is setup correctly and that the mail function will, in fact, send a text message. Using the following code, we can run:

<?php

var_dump( mail( '##########@vtext.com', '', 'This was sent with PHP.' ) ); // bool(true)

?>

When my phone receives the message, it looks like so:



If you are getting an error, see the troubleshooting section.


As you can see in the image above, the message shows that it is from Gmail. This is because I route all my outgoing messages from my local server through that service. Unfortunately, as of this writing, I have been unsuccessful at altering the From header to reflect an alternate address. It seems that the email headers are stripped and replaced with headers prepared by the SMS gateway. If anyone knows of a workaround, please leave a comment and let the rest of us know!




Adding Usability

The Markup

With the basics out of the way, let’s take this idea and wrap a user interface around it. First we’ll set up a simple form:


<!DOCTYPE html>
<head>
<meta charset="utf-8" />
</head>
<body>
<div id="container">
<h1>Sending SMS with PHP</h1>
<form action="" method="post">
<ul>
<li>
<label for="phoneNumber">Phone Number</label>
<input type="text" name="phoneNumber" id="phoneNumber" placeholder="3855550168" /></li>
<li>
<label for="carrier">Carrier</label>
<input type="text" name="carrier" id="carrier" />
</li>
<li>
<label for="smsMessage">Message</label>
<textarea name="smsMessage" id="smsMessage" cols="45" rows="15"></textarea>
</li>
<li><input type="submit" name="sendMessage" id="sendMessage" value="Send Message" /></li>
</ul>
</form>
</div>
</body>
</html>

The Style

Next we’ll sprinkle in some CSS:


body {
margin: 0;
padding: 3em 0;
color: #fff;
background: #0080d2;
font-family: Georgia, Times New Roman, serif;
}

#container {
width: 600px;
background: #fff;
color: #555;
border: 3px solid #ccc;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
-ms-border-radius: 10px;
border-radius: 10px;
border-top: 3px solid #ddd;
padding: 1em 2em;
margin: 0 auto;
-webkit-box-shadow: 3px 7px 5px #000;
-moz-box-shadow: 3px 7px 5px #000;
-ms-box-shadow: 3px 7px 5px #000;
box-shadow: 3px 7px 5px #000;
}

ul {
list-style: none;
padding: 0;
}

ul > li {
padding: 0.12em 1em
}

label {
display: block;
float: left;
width: 130px;
}

input, textarea {
font-family: Georgia, Serif;
}

This gives us the following simple form:




The Script

The most important part to this is the PHP script. We’ll write that bit of code now:


<?php

if ( isset( $_REQUEST ) && !empty( $_REQUEST ) ) {
if (
isset( $_REQUEST['phoneNumber'], $_REQUEST['carrier'], $_REQUEST['smsMessage'] ) &&
!empty( $_REQUEST['phoneNumber'] ) &&
!empty( $_REQUEST['carrier'] )
) {
$message = wordwrap( $_REQUEST['smsMessage'], 70 );
$to = $_REQUEST['phoneNumber'] . '@' . $_REQUEST['carrier'];
$result = @mail( $to, '', $message );
print 'Message was sent to ' . $to;
} else {
print 'Not all information was submitted.';
}
}

?>
<!DOCTYPE html>

  • The script first checks to see if the form has been submitted.
  • If yes, it checks to see if the phoneNumber, carrier and smsMessage variables were sent. This is useful in the case where there may be more than one form on the page.

  • If phoneNumber, carrier and smsMessage are available and phoneNumber and carrier are not empty, it is okay to attempt to send the message.

  • The message argument in the mail function should be 70 characters in length per line. We can chop the message into 70 character chunks using the wordwrap function.

  • phoneNumber and carrier are concatenated and then the message is sent using the mail function.

  • If data is missing or it cannot be validated, the script simply returns Not all information was submitted.
  • Finally, mail returns a boolean indicating whether it was successful or not. The value is stored in $result in case I needed to verify that the message was in fact sent.


Note: The mail method only notifies whether the message was sent or not. It does not provide a way to check to see if the message was successfully received by the recipient server or mailbox.



The Final Code


<?php

if ( isset( $_REQUEST ) && !empty( $_REQUEST ) ) {
if (
isset( $_REQUEST['phoneNumber'], $_REQUEST['carrier'], $_REQUEST['smsMessage'] ) &&
!empty( $_REQUEST['phoneNumber'] ) &&
!empty( $_REQUEST['carrier'] )
) {
$message = wordwrap( $_REQUEST['smsMessage'], 70 );
$to = $_REQUEST['phoneNumber'] . '@' . $_REQUEST['carrier'];
$result = @mail( $to, '', $message );
print 'Message was sent to ' . $to;
} else {
print 'Not all information was submitted.';
}
}


?>
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<style>
body {
margin: 0;
padding: 3em 0;
color: #fff;
background: #0080d2;
font-family: Georgia, Times New Roman, serif;
}

#container {
width: 600px;
background: #fff;
color: #555;
border: 3px solid #ccc;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
-ms-border-radius: 10px;
border-radius: 10px;
border-top: 3px solid #ddd;
padding: 1em 2em;
margin: 0 auto;
-webkit-box-shadow: 3px 7px 5px #000;
-moz-box-shadow: 3px 7px 5px #000;
-ms-box-shadow: 3px 7px 5px #000;
box-shadow: 3px 7px 5px #000;
}

ul {
list-style: none;
padding: 0;
}

ul > li {
padding: 0.12em 1em
}

label {
display: block;
float: left;
width: 130px;
}

input, textarea {
font-family: Georgia, Serif;
}
</style>
</head>
<body>
<div id="container">
<h1>Sending SMS with PHP</h1>
<form action="" method="post">
<ul>
<li>
<label for="phoneNumber">Phone Number</label>
<input type="text" name="phoneNumber" id="phoneNumber" placeholder="3855550168" /></li>
<li>
<label for="carrier">Carrier</label>
<input type="text" name="carrier" id="carrier" />
</li>
<li>
<label for="smsMessage">Message</label>
<textarea name="smsMessage" id="smsMessage" cols="45" rows="15"></textarea>
</li>
<li><input type="submit" name="sendMessage" id="sendMessage" value="Send Message" /></li>
</ul>
</form>
</div>
</body>
</html>


Troubleshooting

Localhost Error

In order to use the mail function, you must have a mail server running. If you’re running this on a web host, you’re probably okay. But if you are unsure, I recommend talking to an administrator. This also holds true for personal machines. So if you get errors like..


Warning: mail() [function.mail]: Failed to connect to mailserver at 'localhost' port 25, verify your 'SMTP' and 'smtp_port' setting in php.ini or use ini_set() in C:\wamp\www\sms\mail-test.php

…you will have to install and configure a mail server. This is out of the scope of this tutorial. However, if you are working on your local machine, switching to something like XAMPP might solve this problem. Alternatively, try installing Mercury Mail alongside WAMP, MAMP or on a LAMP (or SAMP or OAMP, etc.) system (that’s a lot of ‘AMPs’).

PHPMailer

Another option (which is the method I prefer) is to use PHPMailer. Below is an example of how to use PHPMailer to connect to Gmail’s SMTP server and send the message.

Using it is as simple as including a class in your script.


require 'class.phpmailer.php';

// Instantiate Class
$mail = new PHPMailer();

// Set up SMTP
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->SMTPDebug = 2; // This will print debugging info
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "tls"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Encoding = '7bit'; // SMS uses 7-bit encoding

// Authentication
$mail->Username = "email.address@gmail.com"; // Login
$mail->Password = "password"; // Password

// Compose
$mail->Subject = "Testing"; // Subject (which isn't required)
$mail->Body = "Testing"; // Body of our message

// Send To
$mail->AddAddress( "##########@vtext.com" ); // Where to send it
var_dump( $mail->send() ); // Send!

This should print out something along the lines of:


It may take a little more to set up the connection depending on your situation. If you’re planning on using Gmail, Google has provided information on connecting.



Conclusion

There are a myriad of methods to accomplish the task of sending a SMS through a web application. This method is really meant for low volume messaging (most likely less than 1,000 text messages per month) and developers looking to get their feet wet without forking out cash. Other options include:


  • Using a SMS Gateway Provider

    • Doing a Google search will return plenty of options.

    • Most SMS gateway providers include an API for sending messages through a web application.

    • You can usually sign up for service for a reasonable price, assuming you are planning on sending at least 1,000 SMS message per month.

    • You can rent a short code number.


  • Using a GSM modem

    • This can be a costly and slow way to do it, since you have to buy a modem and have a contract with a wireless network

    • You will also have to use the AT (Hayes) command set.

  • Use a direct connection to a wireless network, which will require some strong negotiating and a whole lot of money.

This tutorial is in no way a comprehensive review of sending tex messages with PHP; but it should get you started! I hope this tutorial has been of interest to you. Thank you so much for reading!


11 More Things I Learned From the jQuery Source

11 More Things I Learned From the jQuery Source:

Not long ago, Paul Irish, who works on jQuery and Google Chrome, recorded an excellent screencast which details ten things he learned from the jQuery source. Well today, he’s back with eleven more helpful notes, tips, and tricks. It’s a must watch.





Where to Find Paul’s Footprint


Quick Tip: Never Type a Vendor Prefix Again

Quick Tip: Never Type a Vendor Prefix Again:

You know the drill quite well. Want to give some section of your website rounded corners with CSS3? Then you’ll require nothing short of three vendor prefixes: webkit, moz, and the W3C recommended form. Isn’t that a huge waste of time — not to mention screen space? What if, instead, we could use a class file? Well, we can! I’ll show you how today.



The Key

If we use a tool like LESS or SASS, we can create our own class files quite easily. Have no idea what I’m talking about? Well, first, review this quick tip. It’ll teach you exactly how to get up and running with Less.


Classes File

Next, we need to create a core classes file that will be used in every project. Feel free to store this file anywhere you wish, though, in the video above, I use our popular (and exclusive) Structurer app.

We’ll do the first one together, but be sure to review the screencast for more details.


.border-radius( @radius: 3px ) {
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
border-radius: @radius;
}

In terms of naming conventions, I’ve found that it’s smartest to use the officially recommended name for your class name — in this case, “border-radius.” To declare variables with Less, we preface them with an @ symbol. In this case, we’re setting a default value of 3px, though, should we need to override that value in our project, it’s a cinch!

#someElement {
.border-radius(10px);
}


Some Examples

At this point, simply rinse and repeat for each property that requires multiple vendor prefixes. Here’s a handful to get you started:

Box Shadow

.box-shadow(
@x : 2px,
@y : 2px,
@blur : 5px,
@spread : 0,
@color : rgba(0,0,0,.6)
) {
 -webkit-box-shadow: @x @y @blur @spread @color;
-moz-box-shadow: @x @y @blur @spread @color;
box-shadow: @x @y @blur @spread @color;
}

Transition

.transition(
@what : all,
@length : 1s,
@easing : ease-in-out
) {
-webkit-transition: @what @length @easing;
-moz-transition: @what @length @easing;
-o-transition: @what @length @easing;
transition: @what @length @easing;
}

Box

.box(
@orient : horizontal,
@pack : center,
@align : center
) {
display: -webkit-box;
display: -moz-box;
display: box;

-webkit-box-orient: @orient;
-moz-box-orient: @orient;
box-orient: @orient;

-webkit-box-pack: @pack;
-moz-box-pack: @pack;
box-pack: @pack;

-webkit-box-align: @align;
-moz-box-align: @align;
box-align: @align;
}

Flex

.flex( @val : 1 ) {
-webkit-box-flex: @val;
-moz-box-flex: @val;
box-flex: @val;
}


Conclusion

I’d love to hear your thoughts on this. If you like the idea, let’s turbo-charge this stylesheet.