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();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->request(array('url' => 'http://localhost/');
$restponseBody = $curl->getResponseBody();
$hasBob = $curl->bodyMatchesExpression('/Bob/i');
$curlCurlifier 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.
->setUrl('http://localhost')
->setGet(array('foo' => 'bar'))
->setPost(array('name' => 'Bart'))
->setCookie('nameOfCookie' => 'VALUE_OF_COOKIE')
->request();
$curl->setGet(array('one' => '1'))The power of the two different approaches comes when you need to perform multiple requests:
->request();
//Is the same (for this request) as:
$curl->request(array(
'get' => array('one' => '1')
));
$curl->setUrl('http://localhost/pageApp.php')Curlifier is not complete. Features on To-do-list include:
->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;
}
Support for multiple Cookies- Support for "cookie jar"
- Auto-parsing responses "Set-cookie" -headers and setting cookie accordingly
Returning PHP-array from JSON responseReturning 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:
aaaaOn to-do-list is:
baaa
caaa
daaa
…
zzzx
zzzy
zzzz
- 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' )Or any combination of those.
"-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' )
<?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
No comments:
Post a Comment