When curl is refusing to work as you expect…

…you probably need to look at the many options curl offers. But let me start from the beginning.


    $curl = curl_init();

    $url = 'http://api.somewebsite.com/whatever';
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($curl);

was returning an empty string, while

    $result = file_get_contents($url);

worked fine. Now I’m not the biggest fan of opening an URL using file_get_contents(), so I tried some of the many options and finally found the one that solved my problem.

    $curl = curl_init();

    $url = 'http://api.somewebsite.com/whatever';
    curl_setopt($curl, CURLOPT_URL, $url);
    // important option goes here...
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($curl);

Pitfall: php_admin_flag vs. php_flag

Today I was almost going crazy about the PHP ini setting “display_errors”, which just couldn’t be convinced to be “On”. After some searching on the Internet without a result (“Must be an undiscovered bug … dammit”) I was about to give up. Hey, what do we have an error log for? But then good-old intuition stroke again – how about the vhost configuration?


php_admin_flag display_errors off

Who the f…? Well, me of course! I copied a bunch of these statements when taking another vhost configuration file as template, without having a closer look.

“php_admin_flag” is surely nice for hosters etc., because it sets the PHP ini value once and for all. That’s probably tricky, because ini_set() will simply return false, with no more information.

Note to myself: if setting ini values via vhost is really needed, better use “php_flag” instead. This way the value can still be overwritten.

Getting the subclass name in the parent class

A mistake that I keep making – I have a class that extends another class, and I need to know the class name of that subclass. However the function where I need the name is in the parent class. In this case the magic constant __CLASS__ won’t work, as it will return the name of the class where it is found. get_class() won’t work, too. Not without an important difference:

abstract class Foo
   public function getClassName()
      return __CLASS__;
     // or return get_class();

   public function getSubClassName()
      return get_class($this); // the reference $this is important here

class Bar extends Foo {};

$bar = new Bar();

echo '1. ' . $bar->getClassName();
echo '<br>';
echo '2. ' . $bar->getSubClassName();

This will result in

1. Foo
2. Bar

The first function call returns the (in my case) incorrect name of the parent class, while the 2nd function works as I need it.

Updating XAMPP without a headache

After some years the time has come to update my local WAMP stack to a more recent PHP version. I also made good experiences with WAMP (the other cool stack for Windows), but I’ll stick with XAMPP and install 1.8.2. This will also make things easier.

  1. Rename the old xampp installation directory C:\xampp (or where you installed it) to C:\_xampp
  2. Install new XAMPP under C:\xampp
  3. Copy all (needed) files/projects from C:\_xampp\htdocs to C:\xampp\htdocs
  4. Copy all (needed) databases from C:\_xampp\mysql\data to C:\xampp\mysql\data. EDIT: Don’t do that if your databases contain InnoDB tables. Use mysqldump instead.
  5. Check differences between both php.ini files, located in C:\xampp\php
  6. If you made changes on your MySQL configuration, don’t forget to check C:\xampp\mysql\bin\my.ini as well
  7. check your PHP Extensions in php/ext: you will need new versions of any additional PHP extensions (e.g. libpdf). Keep in mind that PHP 5.4 has been compiled using VC9 instead VC6 as in PHP 5.3 when chasing for downloads!
  8. Take over changes in C:\xampp\apache\conf\httpd.conf – most likely you’ll have some VirtualHost configurations
  9. Run the security check in XAMPP (http://localhost/security/index.php) to set the MySQL password to the same password you used in the old XAMPP installation (if you’re using a password)
  10. Be happy! You now shouldn’t need to change any path configurations, passwords etc. to run your web applications on your new PHP 5.4 stack!

The actual migration from PHP 5.3 to 5.4 shouldn’t be a problem, as there aren’t that many compatibility issues. I didn’t experience any PHP issues so far.

I was rather concerned about just copying the MySQL files – but it worked like a charm and is definitely quicker than doing mysqldumps and loads on many big databases – UNLESS you don’t have InnoDB tables in your databases. In this case better use mysqldump. In general it’s a good idea to keep an eye on the mysql_error.log in the data directory.

Good thing about this approach: you still have your old XAMPP installation at hand which you can reuse by simply renaming the two directories. Always be backed up!

PHP mail() on Windows

Setting up an email server on Windows can be a real pain. Hardly worth the effort when all I want to do is using PHP’s mail() function.

I found a nice workaround when I updated my XAMPP installation: http://glob.com.au/sendmail/. All you need is an SMTP server somewhere that you can use, because sendmail.exe will just forward the emails to it.

You still need to configure it though:

  1. Configure your SMTP in the sendmail.ini (located usually under C:\xampp\sendmail)
  2. In the php.ini, ensure you have the following set up
    [mail function]
    sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

That’s it. Now you can quickly test it (below script more or less stolen from php.net):

$name = "Sender"; 
$recipient = "your@email.de"; 
$email = "recipients@email.de"; 
$mail_body = "The text for the mail...";
$subject = "Subject for reviever";
$header = "From: ". $name . " &lt;" . $email . "&gt;\r\n";

mail($recipient, $subject, $mail_body, $header);

If everything is set up correctly, you should notice that the execution of the script takes a moment. If it runs through fast, something did not work – or you have a really fast machine & connection.

Note: although I had some config errors the first time the error.log in the sendmail directory was not populated with any messages. I don’t know when this file will actually be used.

Create your own PHP extension (on Windows)

Have you ever thought about writing your own PHP extension? There are surely a few pro’s and con’s about creating PHP extensions to solve problems, rather than write PHP libraries. But this is not the question here. I was curious. So come on Internet, let’s see what you got for me…



Looks a bit outdated, but that’s not the problem here for me. As a matter of fact, I’m currently still using PHP 5.3.5 on my dev for a list of reasons. And I’m using Windows and I don’t complain (I know my colleagues think I’m weird). Luckily both tutorials cover the topic on how to create the extension on Windows for PHP 5.3.5. So much win! And the best: it really works!

Basically both tutorials provide you with everything you need. I had to get over two pitfalls though:

  1. php5ts.lib is not in the source code directory I just extracted, but in my WAMP stack already – in my case I located it in C:\xampp\php\dev
  2. error C2371: ‘socklen_t’ : redefinition; different basic types¬†– I really have no idea why this error came up during compilation. However I was able to “solve” *cough* the problem by commenting out lines 149 to 151 in the php.h:
    #ifndef HAVE_SOCKLEN_T
    typedef unsigned int socklen_t;

    Surely not the ideal solution, but as I just wanted to try it out I don’t mind at the moment.