March 2011

PHP Script Execution Timer Class

< Often for testing the efficiency of some PHP code we use script execution calculations such as the microtime function. What is infinitely more useful however is the ability to time the execution of all, some, or multiple sections of code or series of lines.
Unfortunately I cant remember the origins of this timer class – but this code allows you to stop, start, pause and resume timing of specific sections of your php code. Thank you, whoever you may be.

Download the class here

Available methods:

start() – start/resume the timer
stop() – stop/pause the timer
reset() – reset the timer
get([$format]) Which can defaults to Timer::SECONDS but can also be Timer::MILLISECONDS or Timer::MICROSECONDS

Usage example:

$timer1 = new Timer();
$timer2 = new Timer();

$timer1->start();
// do some code

// calculate the time it takes to run a function
$timer2->start();
functionX();
$timer2->stop();

$timer1->stop();

print $timer1->get();
print $timer2->get();

PHP Script Execution Timer Class Read More »

Image Placeholders

placekitten

A quick and simple service for obtaining pictures of kittens for use as placeholders in your designs or code.

Simply place the required image size after their url (for example http://placekitten/200/300) and you will be rewarded with a cute placeholder image. Great for quick real code mockups…

Check it out here

Image Placeholders Read More »

cURL in PHP to access protected sites

So I was trying to use the FaceBook PHP-SDK and ran into an issue. As the cURL was pointing to an HTTPS source I was getting this error:
Failed: Error Number: 60. Reason: SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

The problem was that cURL hasnt been configured to trust the servers HTTPS certificate, as by default cURL is not setup to trust any of the Certificate Authorities (CAs)
Browsers dont have this issue as the browser developers were kind enough to include a list of default CAs, however this doesnt help us out at all…

A quick fix is to simply configure cURL to accept any server certificate. Obviously from a security point of view this isnt great, but if you are not passing sensitive information back and forth you should be ok.

Before calling curl_exec() add the following code:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
This causes cURL to blindly accept an server certificate without doing any verification with the CA that issued it.

The Proper Fix
The proper fix is slightly more involved so I plan to cover it at a later stage. If you cant wait that long research the curlopt_cainfo parameter, and obtaining (and saving) a CA certificate to enable cURL to trust it.

cURL in PHP to access protected sites Read More »