Tag Archives: web service

Test Your API by making HTTP requests from Hurl.it Software & Sites 07 JUN 2015

Needless to say, services, or web API endpoints, is a must in this day and age of software development, and as such I find myself writing more and more of these for my various systems. Invariably, this means testing them as well, which is precisely where tools allowing you to craft HTTP requests and target them at your API endpoints are so particularly useful.

One of these HTTP request generator tools that I recently stumbled upon thanks to a work colleague (thanks Rory!), is hurl.it, a free little tool brought to us by the guys who build their business on monitoring and measuring your API usage, aka Runscope.

Hurl.it is a simple one page site that allows you to enter a destination URL (with the option to follow redirects if necessary), add authentication and HTTP headers if desired, and of course throw in whatever parameters you need chucked in – before asking you nicely to prove that you are a human and then launching the request, displaying the resulting response in a nice display of headers and body.

Simple, but easy to use and works like a charm.

unicorn vomit hurl.it logo rainbow throw up

Related Link: https://www.hurl.it/ | reCAPTCHA

PHP: Simple method to get data from a JSON Web Service CodeUnit 12 NOV 2012

A lot of web services provide you with some useful JSON formatted data in exchange for a few parameters or just some love, and this is a particularly simple and easy method to grab some of that JSON goodness for use in your own PHP page.

In order to grab the data, we’ll first create a HTTP stream content where we’ll add the all important X-Request-With = XMLHttpRequest header that is used to identify requests as being AJAX requests. At this point you can add any parameters you wish to hit the web service with by adding them as an associative array and wrapping them in a http_build_query() call for the context content.

The next step is to use the classic file_get_contents (this assumes your apache/PHP allows the use of the function to open URLs) to hit the target URL using the newly created context.

This should work, resulting in a JSON object which you can use PHP’s json_decode function to manipulate.

In action:

$opts = array('http' =>
    array(
        'method' => 'POST',
        'header' => "Content-type: application/x-www-form-urlencoded\r\nX-Requested-With: XMLHttpRequest\r\n",
        'content' => http_build_query(array())
        )
    );
$context = stream_context_create($opts);
$result = file_get_contents("http://webservice.org/route/list/key/mykey/format/json", false, $context);
$jsondata = json_decode($result, true);

Nifty.

PHP: How to Create a Simple JSON or XML Web Service CodeUnit 07 DEC 2011

Thanks to Twitter, the idea of accessible, useful, and more importantly, simple web services has really latched on in the world of web development. Bascially all a web service is, is a simple URL that when hit with or without specified parameters, returns some data in either JSON or XML (or quite frankly whatever you want it to return) format.

Here is a quick example to show you just how easy it is to develop a simple, basic web service of your own in PHP:

//database connection
$link = mysql_connect('localhost','username','password') or die('Cannot connect to the DB');
mysql_select_db('db_name',$link) or die('Cannot select the DB');

//format parameters. Note default of JSON for format parameter
$format = key_exists('format', $_POST) ? strtolower($_POST['format']) : 'json';
$format = in_array($format, array('json', 'xml')) ? $format : 'json';
$code = key_exists('code', $_POST) ? strtolower($_POST['code']) : 'survey';
$input = trim($code);

//output variables
$status = 0;
$name = '';
$message = '';

//check if we can find a record in the database
$campaignrs = mysql_query("SELECT `campaign-name` FROM `campaigns` WHERE `campaign-code` = '$input' LIMIT 1");
if (mysql_num_rows($campaignrs) > 0) {
        $campaign = mysql_fetch_assoc($campaignrs);
        $status = 1;
        $message = 'Valid Code';
        $name = $campaign['campaign-name'];
} else {
        $status = 0;
        $message = 'Invalid Code';
        $name = '';
}

//build output array
$output = array('status' => $status, 'message' => $message, 'name' => $name);

/* output in necessary format */
if ($format == 'json') {
    header('Content-type: application/json');
    echo json_encode($output);
} else {
    header('Content-type: text/xml');
    echo '';
    echo '' . $status . '';
    echo '' . $message . '';
    echo '' . $name . '';
    echo '';
}

 /* disconnect from the db */
mysql_close($link);

Simple. The web service provides an object with three keys, namely status, name, and message. You can control the output with the format switch, and the code parameter in this case is what we are using to initiate some sort of result or check.

That should be enough to get you up and running I reckon! :)

Optimize Your Javascript by Minifying with Google’s Closure Compiler CodeUnit 02 MAR 2010

Optimize your Javascript by minifying with Google’s Closure Compiler. Well, that pretty much says it all. By now we all no that there is plenty of scope for reducing the size of one’s Javascript code by replacing bulky variable names with shorter versions and stripping out whitespace, etc., but naturally as one would expect, achieving this optimization by hand is a rather tiresome affair.

Enter the nifty Google Closure Compiler, simply put, a tool for making Javascript download and run faster. It’s not a traditional code compiler mind you, it doesn’t compile source code into machine code but rather compiles Javascript to better Javascript, analyzing, clearing dead code, and rewriting and minimizing what’s left over. It checks syntax, variable references, types and even warns about common javascript pitfalls just for fun.

There are a number ways in which you can set the compiler loose on your code: you can use the open source java command line application, you can simply plug your script into the simple online web application or you can even make use of their full RESTful API.

The benefits of using this great little system do of course not need that much explanation. Firstly, in terms of efficiency, using the Closure Compiler will result in smaller javascript files which in turn means faster loading applications which obviously means reduced bandwidth needs. In terms of code checking, Closure Compiler provides warnings for illegal javascript as well as for potentially dangerous operations, resulting in less buggy scripts and scripts that are simply easier to maintain.

And just in case you were wondering why you should give them a spin, take note that jQuery have moved to the Closure Compiler to produce their minified scripts.

So what are you waiting for? ;)

Related Link: http://code.google.com/closure/compiler/