Saturday, February 16, 2013

PHP's PECL extension (for HttpRequest) worked on website but not command line (CLI)?

I'm running PHP on my Mac under nginx and FastCGI... and that's great and fine. I used Macports to set that all up.

Well, Macs have PHP in another, default location: /usr/bin/php, not Macport's /opt/local/bin/php. When I installed the PECL extensions using Macports (after installing PHP), it installed them to the PHP at /opt/local/bin/php, not /usr/bin/php.

I have a PHP script that makes HTTP requests, and it may be a long process, so I spin up PHP on the command-line to do it in the background. Took me forever to figure out that that PHP binary was different from the one used by nginx when I loaded up my dev site.

The CLI version of PHP which I was running for this didn't have PECL installed. A quick, dirty way to fix this:

  1. sudo mv /usr/bin/php /usr/bin/php.bak
  2. sudo ls -s /opt/local/bin/php /usr/bin/php

That's right! Make php a symlink! Why? Well, I'm not sure. I tried simply doing the "mv" to kind of "hide" the binary from sh, but it was giving me "php - command not found" errors, even though /opt/local/bin is was in the PATH. Why didn't it look there? I have no idea. I'm not very proficient with unix stuff. But the symlink was enough to trick it and it's working great now.