Compiling PHP Extensions

There are lots of reasons why you might like to compile your own PHP extensions. For me those reasons are usually:

  • The extension isn’t available on pecl (e.g. uprofiler)
  • The extension is on pecl, but you need the newest version or a branch with a particular feature or fix in it, perhaps for testing
  • You are fixing an extension yourself (yay, we need more people like you!)

Related: If you followed my previous post on compiling PHP, be aware that in the php/bin/ folder there is a pecl binary that will install extensions correctly for whichever version of PHP it belongs to, so you may not need to read the rest of this post. However if you do, the paths follow on from the examples in that post.

I haven’t seen a really approachable guide anywhere, we tend to speak of extensions in hushed tones, and actually it isn’t particularly tricky so here is my quick how-to guide. Continue reading

PHP and Gearman: Unable to connect after upgrade

I upgraded PHP and related pecl modules on my development machine today, and ran into a problem with Gearman. Actually I ran into more than one! Firstly the challenge of getting the newest pecl version working with a gearman version. Then an error where my existing PHP application couldn’t connect to gearman after upgrade.
Continue reading

Invalid Protected Resource URL in Pecl_Oauth

I had a funny (funny weird, not funny haha) problem the other day when working with pecl_oauth in PHP to talk to a service. I’d gone through all the handshaking steps, got the acces token and was ready to start talking to the service itself. However when I tried to call OAuth::fetch, I got an error message:

Fatal error: Uncaught exception 'OAuthException' with message 'Invalid protected resource url, unable to generate signature base string'

There are two things to notice about this. The first one is that I should be catching exceptions thrown by this code :) The second is that I could see nothing wrong with my url, http://api.local. It turned out, after some experimentation, that what is missing here is a trailing slash, and if I supply http://api.local/, everything works perfectly nicely! I’m unclear if this is intended functionality or not, but if you see this error message and you’re requesting a URL with no path info, make sure you have a trailing slash.

Downgrading a PECL Module

Recently I saw some weirdness in an existing application when I upgraded a PECL module that the application depended on. To figure out if that really was the problem, I wanted to downgrade the module to its previous version. There is no opposite command to “upgrade” but you can instruct pecl to install a specific version of a module, using the -f switch to force pecl to overwrite newer modules.

Downgrading Pecl_OAuth

For me the problems were caused in the switch between default functionality in pecl_oauth 1.1.0 (this isn’t a bug, but is correct behaviour according to the OAuth 1 spec, I just had code that expected the old functionality), so I wanted to put my version back to 1.0.0

pecl install -f oauth-1.0.0

It was easy once I stopped looking for an option called “downgrade” or something like that :) In fact you can use this trick to install all kinds of pecl versions, simply refer to the package as [package name]-[version]. By default pecl won’t let you install packages that aren’t marked “stable”, but you can install beta packages by putting “beta” in place of [version].

Hopefully now I’ve written this I’ll remember next time how to do it!

Deprecated Methods in Pecl_Http

I’m a big fan of pecl_http, which I use quite often as I work so regularly with APIs and on systems where I can get it installed, it’s much nicer than PHP’s curl extension. Recently though I’ve been often seeing output which reads:

Function HttpRequest::addRawPostData() is deprecated

It isn’t obvious from the PHP manual page what I ought to do instead, however further inspection shows that it is recommended to use setBody() instead. This can be used in exactly the same way, and my code seems to work perfectly well with this substitution. If you have any more information about this change, leave me a comment – I’d be interested to hear it.

Authenticating with OAuth from PHP

I’ve been looking into OAuth recently and really like what I see, so I started looking at actually starting to play with something that uses it (and isn’t twitter). In the pursuit of this, I spent some time walking through the process of how to actually authenticate using OAuth, as a client. I chose Yahoo!’s service, because they have some fabulous developer documentation and have a standard OAuth implementation. Although you don’t strictly need any special libraries to handle OAuth, that would be a bit like decoding XML with a regex, so I used the OAuth Package from PECL. For others (including me after I’ve slept), here’s an outline of the process.

Continue reading

Missing pcre.h when installing pecl_oauth

I was playing with pecl_oauth last week (more about that in a later post) and when I tried to install from PECL, it grabbed the files, ran the configure step but stopped with an error status during make. This is bad news for those of us who are ubuntu users rather than compile-happy linux users! Closer inspection showed this line around the point things started to go wrong:

Error: /usr/include/php5/ext/pcre/php_pcre.h:29:18: error: pcre.h: No such file or directory

I didn’t have the header files for pcre installed – in ubuntu the headers are in the -dev packages so I just installed what I needed:

sudo aptitude install libpcre3-dev

Re-attempting the pecl install, everything worked as expected. This is on Ubuntu 10.04 Lucid Lynx, and from reading around you’d want to install the same package in response to this error message, regardless of what you were doing to cause it. Hope this helps someone.

Three Ways to Make a POST Request from PHP

EDIT: A more modern take on this topic is here http://www.lornajane.net/posts/2018/make-a-post-request-from-php-with-guzzle

I’ve been doing a lot of work with services and working with them in various ways from PHP. There are a few different ways to do this, PHP has a curl extension which is useful, and if you can add PECL extensions then pecl_http is a better bet but there are a couple of different ways of using it. This post shows all these side-by-side.

POSTing from PHP Curl

This is pretty straightforward once you get your head around the way the PHP curl extension works, combining various flags with setopt() calls. In this example I’ve got a variable $xml which holds the XML I have prepared to send – I’m going to post the contents of that to flickr’s test method.

$url = 'http://api.flickr.com/services/xmlrpc/';
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

First we initialised the connection, then we set some options using setopt(). These tell PHP that we are making a post request, and that we are sending some data with it, supplying the data. The CURLOPT_RETURNTRANSFER flag tells curl to give us the output as the return value of curl_exec rather than outputting it. Then we make the call and close the connection – the result is in $response.

POSTing from Pecl_Http

Pecl_Http has two interfaces – one procedural and one object-oriented; we’ll start by looking at the former. This is even simpler than in curl, here’s the same script translated for pecl_http:

$url = 'http://api.flickr.com/services/xmlrpc/';

$response = http_post_data($url, $xml);

This extension has a method to expressly post a request, and it can optionally accept data to go with it, very simple and easy.

POSTing from Pecl_Http: the OO interface

Finally let’s see what the OO verison of the extension looks like. Exactly the same call as both the above examples, but using the alternative interface, means our code looks like this:

$url = 'http://api.flickr.com/services/xmlrpc/';

$request = new HTTPRequest($url, HTTP_METH_POST);
$request->setRawPostData($xml);
$request->send();
$response = $request->getResponseBody();

This example is quite a bit longer than the previous one, and you might think this indicates that this approach is more complicated. In some senses that is true and its probably overkill for our extremely trivial example. However it is worth mentioning that the pecl_http extension is extremely flexible and powerful, and can handle some cases that the curl extension can’t. So even if it looks more complicated here, it can still be an excellent choice to implement.

In Conclusion

That was a very fast round-up of three ways you could make an arbitrary web service call from PHP – hopefully these examples are clear and will help anyone just starting to implement something along these lines.