Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Hasin Hayder:
Creating a slack bot using PHP to monitor your envato sales
Oct 05, 2015 @ 10:43:03

Hasin Hayder has posted a tutorial to his site showing you how to create a bot for Slack that will monitor your sales on Envato connected via their API.

Slack is awesome. Slack is the de-facto of modern team communication and I am pretty sure that this is not the first time you’re hearing something like this about slack. But if you’re really never used slack, oh boy, I feel pity for you. Anyway, in this article we’re going to make a slack bot that will monitor the sales in our envato account and notify from time to time as soon as someone purchased an item. Pretty useful, eh?

He shows how to make a new channel in your Slack instance (he uses "#sales") and how to add in a named webhook to connect the bot to. He then generates a token on the Envato service to access their API, giving it the needed permissions. He includes the simple curl-based script to make the request to the Envato API to get the latest results and then turn around and post it back to the Slack channel.

tagged: slack bot envato sales api channel automate tutorial

Link: http://hasin.me/2015/10/05/creating-a-slack-bot-using-php-to-monitor-your-envato-sales/

SitePoint PHP Blog:
Conquering Instagram with PHP and the Instagram API
Sep 22, 2015 @ 12:57:21

The SitePoint PHP blog has a tutorial posted that shows you how to "conquer" the Instagram API in your PHP application and integrate it into your application with the help of the Guzzle HTTP client.

Instagram’s API allows us to interact with data such as user info, media (photos and videos), likes, comments, and tags. For example, you can search for media around a specific location and filter the results by time. The API also allows us to post comments or like specific media. For now, only uploading media isn’t supported. You can always look at the API endpoints documentation if you want to know about the full functionality.

They start by talking about the two types of API calls (authenticated/unautheticated) and the limits that are included with each. From there the tutorial shows you how to register a new application, get a new client ID and how to use the API console to test out requests. Next they help you get the [Guzzle]((http://guzzle.readthedocs.org/en/latest/) library installed and connect it to the Instagram API with your client credentials. They integrate this into a simple Slim application using Twig as a templating layer to allow for the OAuth approval process, fetching an access token for the later requests.

With the connection made, they show you how to make requests to do thing like make tag searches, get user feed information (and searches), geographic searches and handling pagination. They also include a link to a PHP Instagram library that can help simplify the process, complete with code examples.

tagged: tutorial instagram api introduction library oauth slim twig

Link: http://www.sitepoint.com/conquering-instagram-with-php-and-the-instagram-api/

Michael Mikowski:
RESTful APIs, the big lie
Sep 09, 2015 @ 11:19:31

Michael Mikowski has a post to his site that suggests that a RESTful API is a big lie and that the concept should "rest in piece" and be replaced with something he calls a "JSON-pure API".

If you have read an internet developer resume or job posting in the past 10 years, then you might be forgiven if you think that RESTful APIs are gifts bestowed from the heavens by The One True Web Developer Deity. RESTful APIs are everywhere. Even the marketing folks are pushing them in sales material intended for CEOs and Human Resources-type folks. So how good of an idea are RESTful APIs really?

He starts with a look at where the concepts for a RESTful API originally came from and defines some of the most common concepts around them (verbs, request/response, etc). He then suggests that they're "pretty awful" and lists some of the larger problems he sees with them:

  • Problem #1: There is little agreement on what a RESTful API is
  • Problem #2: The REST vocabulary is not fully supported
  • Problem #3: The REST vocabulary is not rich enough for APIs
  • Problem #4: RESTful APIs are very hard to debug
  • Problem #5: RESTful APIs are usually tied to HTTP

He suggests that the way to move forward is to migrate to the "JSON-pure API" methodology, fixing most of the problems he listed. He describes this kind of API and how it simplifies the entire process and makes it "more reliable, easier to use, easier to port, and easier to debug."

tagged: restful rest api jsonpure problem lie opinion

Link: http://mmikowski.github.io/the_lie/

SitePoint PHP Blog:
Voice controlled PHP apps with API.ai
Aug 31, 2015 @ 09:36:14

The SitePoint PHP blog has posted a tutorial showing you how to help your applications understand natural language with the help of the Api.ai service.

In this tutorial we’ll be looking into Api.ai, an API that lets us build apps which understand natural language, much like Siri. It can accept either text or speech as input, which it then parses and returns a JSON string that can be interpreted by the code that we write.

They start by explaining some of the concepts you'll need to know in using the service: agents, entities, intents, contexts, aliases and domains (each with a summary and examples). In their example, they show how to get the current time from any given places using their API. He shows you how to set up a free account and create a new agent. He then uses Guzzle (the HTTP client library) to make the request to the API with your API and subscription keys. They show the result of a query for "What's the current time in Barcelona Spain?" and how to extract the "location" value from the results. With this they then search the Google API for the local time of "Barcelona Spain". With the time in hand, they then use the responsive-voice.js library to convert the text to speech at the click of a button.

With all these concepts under your belt, they also walk you through a second application, a currency converter. It uses the same workflow as the previous example with the exception of using the CurrencyLayer.com API to perform the financial conversion. All code needed for this example is included as well. If you'd like to just jump to the end, they've also shared the complete code for both examples over on Github.

tagged: voice control api apiai tutorial localtime currency convert

Link: http://www.sitepoint.com/voice-controlled-php-apps-with-api-ai/

SitePoint PHP Blog:
Using the Selenium Web Driver API with PHPUnit
Aug 24, 2015 @ 12:54:56

The SitePoint PHP blog has posted a tutorial showing you how to use the Selenium web driver API from inside of your PHPUnit tests. Selenium is an automation tool that makes testing frontends of applications simpler.

Previously, we demonstrated using Selenium with PHPUnit and used a user subscription form example throughout the article. In this one, we are going to explore Facebook’s webdriver package for emulating a browser. PHPUnit partially supports the Selenium WebDriver API and the work is still in progress. One of the most popular WebDriver API implementations is the Facebook/webdriver package. We will try to accomplish the same validation tests from the previous article using this package.

They help you get the Facebook package installed (via Composer) and create a first simple test class. They create an instance of the RemoteWebDriver object and point it at their local application. A test is then created to navigate to a form, fill in a bit of data and submit it. The results are then checked for a string ("Everything is Good!") to pass the test. He also shows how to have the driver wait for an element to load, possibly one that uses an AJAX request. The post finishes off with a look at some of the other interaction methods (drag and drop, handling popups) and how to run the tests in a "headless" mode not requiring a browser to execute through your system.

tagged: selenium phpunit webdriver api tutorial frontend testing

Link: http://www.sitepoint.com/using-the-selenium-web-driver-api-with-phpunit/

PHP Town Hall Podcast:
Episode 44: API Madness!
Aug 13, 2015 @ 09:48:52

The PHP Town Hall podcast, hosted by Ben Edmunds (and normally Phil Sturgeon), has posted their latest episode today - Episode #44: API Madness

In this episode Ben joined by Mike Stowe (a Developer Relations Manager at MuleSoft) and Amanda Folson (a Developer Evangelist at PagerDuty) to talk about working developers and best practices/concerns and come of their own experiences with APIs.

You can listen to this episode either through the in-page video player, using the in-page audio player or you can download the mp3 for offline listening. If you enjoy the show, be sure to subscribe to their feed too!

tagged: phptownhall ep44 benedmunds mikestowe amanda api madness

Link: http://phptownhall.com/blog/2015/07/22/episode-44-api-madness/

Philip Brown:
Dealing with Exceptions in a Laravel API application
Aug 10, 2015 @ 11:57:43

In a post to his site Philip Brown shows a helpful way to manage API exceptions in a Laravel-based API application. In an API, exceptions are particularly important as they can be a hint to what the problem is and make it easier to return the correct error code to the client.

Exceptions are a very important method for controlling the execution flow of an application. When an application request diverges from the happy path, it’s often important that you halt execution immediately and take another course of action.

In today’s tutorial I’m going to show you how I structure my Laravel API applications to use Exceptions. This structure will make it very easy to return detailed and descriptive error responses from your API, as well as make testing your code a lot easier.

He starts with a brief introduction to HTTP status codes and their role in the interaction between client and server. He then gets into the "exception foundation" and how it will work, providing some basic common functionality (like throwing a 404 when a record isn't found, regardless of the type). He creates a configuration file to define the default error messages, an abstract Exception the custom instances can extend. He creates several of these as an example, such as a "UserNotFound" exception that extends the base "NotFound" exception class. He works with Laravel's own exception handlers and includes the code to catch a few different types inside.

tagged: exception laravel api application custom base handler tutorial

Link: http://culttt.com/2015/08/10/dealing-with-exceptions-in-a-laravel-api-application/

SitePoint PHP Blog:
WP API and OAuth – Using WordPress without WordPress
Jul 16, 2015 @ 13:08:54

The SitePoint PHP blog has posted a tutorial showing you how to "use WordPress without WordPress" via a basic RESTish API installed via plugin. The article focuses on using the OAuth authentication method to connect a client to the WP instance, linked to a system user via generated tokens.

In this tutorial, we’ll learn how to install and use WP-API with OAuth – a WordPress plugin which uses REST-like API endpoints to allow reading of WP content to unauthenticated users, and writing of WP content to users who authenticate via OAuth (or via Cookies for themes and plugins). Using the plugin isn’t very straightforward, and the prerequisite list is quite long, so this post was written to make it simple and relatively approachable (as long as you’re in control of your own server).

The tutorial walks you through the steps to get a WordPress instance installed (via a git clone) and setting it up to work with Homestead Improved. He then installs the "wp-cli" tool to get the OAuth1 plugin needed to make things work correctly and how to use it to generate the needed key and secret for the OAuth connection. He then makes a simple script that uses the Guzzle HTTP client and it's OAuth handling to make the OAuth request for a token, call the callback page and return the bearer token for the remainder of the requests. Finally he creates a simple page that uses this token to submit a new article via the API and views it in the WordPress interface.

tagged: wordpress api tutorial oauth guzzle oauth1 wpcli rest

Link: http://www.sitepoint.com/wp-api-and-oauth-using-wordpress-without-wordpress/

Mattias Noback:
Refactoring the Cat API client (3 Part Series)
Jul 16, 2015 @ 11:25:54

Mattias Noback has posted a three part series of tutorial articles around the refactoring of a "CatApi" class. These articles take the class from a jumbled mess of functionality with both direct file access and remote requests mixed in into something much more maintainable and flexible.

t turned out, creating a video tutorial isn't working well for me. I really like writing, and speaking in public, but I'm not very happy about recording videos. I almost never watch videos myself as well, so... the video tutorial I was talking about won't be there. Sorry! To make it up with you, what follows is a series of blog posts, covering the same material as I intended to cover in the first episodes of the tutorial.

In part one he introduces the current state of the "CapApi" class and some of the problems with it, both in testing and in structure. He does some basic refactoring to split out some of the logic here and moves on to part two. In the second part of the series he focuses on refactoring the HTTP request and the local file system functionality into abstract, injectable objects. Finally in part three he adds in some verification around the data being passed back and forth between objects including both simple checking and the use of value objects.

tagged: refactor api class series part1 part2 part3 filesystem http request xml validation

Link: http://php-and-symfony.matthiasnoback.nl/2015/07/refactoring-the-cat-api-client-part-1/

Sameer Borate:
Accessing WordPress data using the new REST api
Jul 16, 2015 @ 09:53:57

Sameer Borate has posted an article showing you how to use the WordPress REST API (set up by this plugin) to access the data housed inside your WP installation.

WordPress is without doubt the most used CMS system around. Various sources peg the usage around 20-30% of all web sites. Whatever the correct figure, there is no doubt that the collective content of WordPress sites is enormously large. However almost all content is virtually held in independent WordPress sites with no way to easily access a sites content programmatically. [...] As WordPress is moving towards becoming a fully-fledged application framework, we need new APIs. At present a REST api plugin is available to access your site’s data in simple JSON format, including users, posts, taxonomies and more.

He walks you through the installation of the plugin and how to make a request to the REST API's test endpoint to ensure everything's functioning correctly. He also includes an example request that fetches the contents of a post by it's ID. The tutorial wraps up with a look at authentication and how the plugin provides two kinds of handling: basic authentication (HTTP Auth) and OAuth. You can find out more about the structure and functionality of the API on the project's website.

tagged: wordpress rest api tutorial installation setup plugin

Link: http://www.codediesel.com/wordpress/accessing-wordpress-data-using-the-new-rest-api/