Tag Archives: api

Random User Generator

RandomUser is a new API that returns a JSON object with a randomly generated persona—complete with name, avatar and email address—for your testing purposes. The site suggests using it for design mockups, but the fact that it’s an API opens up plenty of possibilities for more programmatic uses. If you need to fill in a couple dozen users to test an application, you could write a script to populate the database with ones pulled from RandomUser.


The API is neat, but a little limited so far. Hopefully it will be expanded in the future with some additional info.

Custom JavaScript Twitter Widgets in an API 1.1 World

My Twitter WidgetContinuing their gradual shutdown of old APIs (following the launch of version 1.1 of their API), Twitter recently pulled the plug on their old-style widgets and the unauthenticated search API. This means if you had a fancy custom-designed JavaScript widget to show off your latest tweets, it’s not going to work anymore. The only officially supported options are the new widgets or a server-side solution with OAuth authentication.

What can you do if you don’t like either option?

Jason Mayes hacked together a clever bit of JavaScript that loads up one of the new Twitter widgets, scrapes the content out, and reformats it into nicer unstyled HTML. Then you can style it however you want in your stylesheet.

I’m using this right now on my personal blog, since Twitter’s new widgets don’t look very good when they’re crammed into a narrow sidebar.

Post to Twitter From a PHP Script: 2013 Edition

Back in 2009, I wrote a post on how to write a simple PHP script to call on the Twitter API and update your status. Despite its popularity, the information hasn’t been relevant in some time. (Things certainly have changed since then!) The Twitter API has changed a lot over the years, and it’s not so simple that you can get a newbie up and running with a few lines of code.

The mandatory usage of OAuth tokens, rather than a simple username and password combination, for API requests has greatly strengthened account security, but it’s one of the prime hurdles complicating the process. More recently, XML support was removed in favor of JSON, URL structures changed to include an API version, and authentication is now required for every request.

Fortunately, you don’t have to deal with the little details. You can use a library that does the heavy lifting for you, rather than reinventing the wheel. Sure, there are resources to learn how to do it the hard way, but I assume that you want a quicker solution if you’re reading this.

Step 1: Download tmhOAuth

Download the tmhOAuth library from GitHub. This package will handle interactions with the Twitter API once you include it from your script. (It requires at least PHP 5.1.2 and the cURL extension.)

Download tmhOAuth

Continue reading →

Twitter API Terms Revision Ignites Controversy

Twitter recently announced the next version of the REST API that powers the many apps that hook into the popular social media service, a move which sparked much criticism among users and developers alike. Some of the changes include:

  • Every API endpoint will require authentication, through OAth
  • Rate limiting is done per endpoint
  • The revised display guidelines will be more actively enforced. They want a more uniform style.
  • Developers will have to “work with Twitter” if they have more than an arbitrary number of users. As an app developer, you will need “[Twitter’s] permission if your application will require more than 100,000 individual user tokens.” Applications that already have over 100,000 tokens will have their fuzzy limit set at 200% of their current token count.

The last point, especially, has been controversial, as it seems suspiciously like a ploy to disadvantage third-party Twitter clients and push users toward the official app. Whether that is the case or not, it could conceivably inconvenience the developers of popular apps like Tweetbot and Echofon.

Hopefully things will work out okay. Twitter’s own app is utterly mediocre in comparison to the better third-party clients, chiefly Tweetbot. Twitter would be shooting themselves in the foot if they put the kibosh on the software that some of the more influential users prefer. Third-party clients are what made Twitter what it is today, after all.

Accepting Credit Cards Online With Stripe

Historically, processing monetary transactions online has been a major pain. Conniving financial institutions would force you to pay tons of extra fees, requiring merchant accounts and other junk. Then Stripe came along, bringing some sanity to the e-commerce world.

With Stripe, you get a no-hassle API and minimal fees. 2.9% + 30 cents, payed out to any bank account. The only requirement is that you set up HTTPS for customers’ safety. (Technically, there is one other thing that may be an issue: Stripe is only available to US merchants for the time being.)

Nettuts+ has an excellent tutorial on how to set up a Stripe account and process transactions through the API: So You Want to Accept Credit Cards Online?

The most difficult part is probably purchasing an SSL certificate and setting your server up to use HTTPS. Some hosts sell certificates, and may be able to configure your server for you. If you’re on your own, though, RapidSSL seems to be a good option. (They’re affordable and on the trust list of most browsers.)

So You Want to Accept Credit Cards Online? [Nettuts+]

Proxying Web APIs with NGINX

Here’s a cool thing I bet you didn’t know could be done with the NGINX server: proxying APIs from web services. Why would you want to do that? Well, for starters, you can avoid running into cross-domain scripting issues.

Your client-side JavaScript can query an API that doesn’t offer JSON-P support, by having it pass through your server first. You can even cache the results for awhile, so you don’t run into rate limit issues. And if JSON-P is a necessity, you can transform the API response from vanilla JSON to JSON-P by echoing some additional content into the request.

You can read how to do all this in an informative blog post by the founder of DuckDuckGo. It’s pretty much a matter of adding a location block that uses the proxy_pass function to pass the request along.

nginx JSON hacks [Gabriel Weinberg]

Building an iPhone App to Parse the Twitter API with NSXMLParser

iOS has a simple event-based XML parser built in, which makes it fairly easy to do less involved parsing operations without having to load up a third-party framework. This tutorial will show you how to build a simple iPhone application that will download an XML feed from Twitter containing a user’s tweets, and then display them with a pretty UI. (You could easily adapt this to parse other XML documents, such as RSS feeds.)

Continue reading →

Cache Data with the WordPress Transients API

WP Engineer had an interesting post recently about a WordPress “Transients API” that is used for caching bits of data temporarily. I often use the Options API to cache things from external servers, such as Twitter statuses, so I don’t hit Twitter’s servers more often than necessary (which would slow down page loads). The Transients API is similar, but with the addition of an expiration field. This makes it a much easier solution, even without its other added benefit.

Also of note is that Transients are inherently sped up by caching plugins, where normal options are not. A memcached plugin, for example, would make WordPress store transient values in fast memory instead of in the database. For this reason, transients should be used to store any data that is expected to expire, or which can expire at any time.

It’s simple enough to use the API. You just call the set_transient(), get_transient() and delete_transient() functions where appropriate. You can read up on the usage over at WP Engineer or the WordPress Codex.

Generate QR Codes On-the-Fly With the Google Chart API

You’ve probably seen a QR code before, even if you didn’t know what it was at the time. It’s a little square matrix barcode that can be read by either a specialized scanner or a cellphone with the right software. UPS puts QR codes on their packages for tracking purposes, and many Android phones come with QR readers preinstalled for easily sharing links to apps.

A QR code can contain any sort of textual information, which will be decoded by a QR reader. A web address, a simple message, a phone number, etc.. A good QR reader should figure out what the decrypted data is and act upon it accordingly. If it’s a web address, it will display the web page. If it’s a phone number, it should display the number and offer to call it.

Users of the iPhone or fourth-generation iPod Touch can use a free app like QR Reader to scan QR codes.

What can you use a QR code for? There are plenty of possible applications. Magazines could print QR codes that let you quickly jump to a web page. (Anyone remember the CueCat?) Advertisements could have QR codes that offer more information. You could put a QR on your business card. Want to move a web page you’re reading from your computer to your phone? Create a quick QR matrix and scan it right off the screen!

Now for the fun part… How do you make your own QR codes?

Continue reading →

Authenticating Users With Twitter OAuth

If you’ve ever played around with the Twitter API, you’ll know that many functions require authentication with either a username/password combination or OAuth. Soon Twitter will be turning off basic authentication for security reasons, in favor of the more complex OAuth protocol. There are plenty of benefits, for Twitter and for users and for developers, but the transition will be a bit of a pain.

Fortunately, Net.Tuts+ has put together a tutorial explaining how to implement OAuth authentication in a Twitter application.

It’s a more complicated than it should be to implement the authentication dance (but it’s very easy for the end user, thankfully). There is one plus to using one of the Twitter OAuth libraries: it’s easier to handle API requests. Need to get a user timeline? If you’re already authenticated, you can just do this:

$nettuts_timeline = $twitteroauth->get('statuses/user_timeline', array('screen_name' => 'nettuts'));

No worrying about cURL or file_get_contents(), the library’s classes take care of all of the boring stuff for you.