How to debug slow curl requests in php

I ran into some trouble recently with WordPress where the admin area of the blog on my local development machine was taking between 5-15 seconds to load. It made working in the admin area slow. I had a hunch something fishy was going on but not a clue what it was. I turned to xhprof and ran a quick profile of the module. If you have never used xhprof its a great tool for profiling your PHP applications and there is a great post on the Inviqa tech portal about how to get started with it. The results of the profiling where interesting and pointed me towards the fact that something was misconfigured or needed debugging with curl. Click on the image to see it full size.

So its clear from the profiling that there was a problem with curl_exec. What exactly is wrong it not clear but my next approach was to write up a sample curl request with PHP so I could debug and try and figure out what was going on. Some procedural PHP for debugging purposes.

The namelookup_time was the culprit which pointed me in the direction of an issue with dns resolution. Taking a closer look at the development machine it turned out the nameserver set in /etc/resolv.conf was not responding correctly and changing it to a better and faster name-server meant responses that were quicker and under a second.

The values returned from curl_getinfo are very useful and can point you in the right direction when trying to debug a curl request. For a full list of values returned by curl_getinfo take a look at the PHP manual page and in particular the return values. Curl in PHP is a wrapper around the c libraries so take a look at this curl page which has more information on what each of the array keys is all about.

I hope that helps you when debugging your slow PHP curl requests. If you have any similar issues and would like to share them please leave a comment.