Friday, February 15, 2013

Sideproject(s)

Busy, busy, busy

The Cimbic has been on hold for a while. I've been "too busy" doing other stuff - like working and watching Netflix. But I haven't been completely idle with my hobby. I've made a little library with handy PHP classes.

jaxToolbox - library

It all started with need to make cURL-requests from script. Sure, there were the PHP-functions, but that produced ugly code. I wanted a wrapper around them and so the first version of jaxToolbox was born.

Curlifier

Object oriented wrapper for making cURL-requests and receiving the response. Curlifier supports method chaining, post- and get-requests, cookies and custom user-agents. It also provides interface to validate responses header and body against given regexpes.
$curl = new \jaxToolbox\lib\Curlifier();
$curl->request(array('url' => 'http://localhost/');
$restponseBody = $curl->getResponseBody();
$hasBob = $curl->bodyMatchesExpression('/Bob/i');
User doesn't have to know any CURL_OPT_* constants to modify requests, all (that I needed) have been wrapped to neat and clear methods. See the Wiki for more complete API.
$curl
    ->setUrl('http://localhost')
    ->setGet(array('foo' => 'bar'))
    ->setPost(array('name' => 'Bart'))
    ->setCookie('nameOfCookie' => 'VALUE_OF_COOKIE')
    ->request();
Curlifier gives the user the liberty of choosing to use various setters or passing all settings as parameter for the request()-method. Settings set via setters are permanent and are present with all request()'s where the parameters given to request() will affect only that single request.
$curl->setGet(array('one' => '1'))
    ->request();
//Is the same (for this request) as:
$curl->request(array(
    'get' => array('one' => '1')
)); 
The power of the two different approaches comes when you need to perform multiple requests:
$curl->setUrl('http://localhost/pageApp.php')
    ->setGet(array('query' => 'Bob%')); //will be used with all requests
for ($i = 0; $i < 10 ; $i++)
{
    $curl->request(array(
        'get' => array( 'page' => $i ) //localhost/pageApp.php?query=Bob%&page=$i
    ));
 
    if ($curl->getHttpCode() !== 200)
        break;
}
Curlifier is not complete. Features on To-do-list include:

  • Support for multiple Cookies
  • Support for "cookie jar"
  • Auto-parsing responses "Set-cookie" -headers and setting cookie accordingly
  • Returning PHP-array from JSON response
  • Returning PHP-object/array from XML response


StringEnumerator

Class that can be used to iterate trough all possible enumerations of given character-pool and string length. (Brute forcing with PHP, anyone?)

Character pool is given simply as string like "abcd1234", all generated strings will contain only those characters. Length of strings is given as integer and all generated strings will be that long.
$permutator = \jaxToolbox\lib\StringPermutator('abcdefghijklmnopqrstuvwxyz', 4);
while ($permutator->hasNext())
{
    echo $permutator->getNext(), "\n";
}

Code above will output:
aaaa
baaa
caaa
daaa

zzzx
zzzy
zzzz
On to-do-list is:

  • Support for multibyte-characters
  • Version that iterates anagrams of given string

ConsoleRequest

PHP supports also console-scripts but in my opinion the way it takes the arguments is - hard. To ease creating console-applications I made ConsoleRequest. It takes the arguments from the $argv-global and serves them as associative array or through getter.

Parameters are passed to script like arguments (or flags) in linux console applications.

Examples:
"-u root -p password" -> array( 'u' => 'root', 'p' =>'password' )
"-qwe" -> array ( 'q' => true, 'w' => true, 'e' => true )
"-ap 2" -> array( 'a' => true, 'p' => '2' )
"--help" -> array( 'help' => true )
"--output result.txt" -> array( 'output' => 'result.txt' )
Or any combination of those.
<?php
$request = \jaxToolbox\lib\ConsoleRequest($argv);
echo $request->get('hello'); //if parameter "--hello" is given, will echo it's value
var_dump($request->getArguments()); //will output all arguments passed to script

Update 2013-02-15

Curlifier now has support for multi-cookie and JSON/XML response parsing.

No comments:

Post a Comment