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.

curl debugging

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.

//Method which does a basic curl get request
function get_data($url) {
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $data = curl_exec($ch);
        //getinfo gets the data for the request
        $info = curl_getinfo($ch);
        //output the data to get more information.
        return $data;


The useful bit for debugging is the

//curl_getinfo gets information regarding a specific transfer
$info = curl_getinfo($ch);
//output the data to get more information.

The output of this method looks as follows and revealed something to me that was very useful. Response shortened for readability purposes there are about 24 array keys.

    [url] =>
    [content_type] => text/html; charset=UTF-8
    [namelookup_time] => 13.830494
    [connect_time] => 0.049992
    [pretransfer_time] => 0.05002

    [primary_ip] =>
    [redirect_url] =>

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.


Share Button

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *