phpperformancecurlphp-curl

Curl API request much slower than Postman and .NET HTTP Client


I am working with a 3rd party dev agency to build an app, for which they are using PHP. They are making a request to an external API using the PHP Curl library.

The problem is it seems significantly slower than when calling via Postman OR using .NET Http client. Could someone please help on this.

Curl library: 7.64.0 PHP version: 8.1

private function request(string $url, $data) {

    // Remove new lines
    $fields = array();
    foreach ($data as $key => $value) {
        $value = str_replace(array("\r\n", "\r", "\n"), "", $value);
        array_push($fields, $key . '=' . urlencode($value));
    }

    // API call to Tourplan
    $dataText = implode('&', $fields);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_ENCODING, '');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $dataText);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
    curl_setopt($ch, CURLOPT_TIMEOUT, 300); //timeout in seconds
    $result = curl_exec($ch);
    unset($dataText);


    if (!is_bool($result) && !empty($result) && $result) {
        // Convert from text/xml to SimpleXMLElement and then to stdClass
        return json_decode(json_encode(simplexml_load_string($result, "SimpleXMLElement", LIBXML_NOCDATA)));
    } else {
        $this->logfile($data['XML'], 'request'.$logNom);
        $this->logfile($result, 'response'.$logNom);
        $this->log_file(curlerror($ch), 'error'.$logNom);
    }

    return false;
}

API Request via .NET Http client enter image description here

Processing time within app monitoring network call (Curl request response takes up 99% of this time) enter image description here

Log file from app enter image description here

Log file from Curl request:

Array
(
    [url] => ----------Redacted------
    [content_type] => text/xml;charset=UTF-8
    [http_code] => 200
    [header_size] => 268
    [request_size] => 189
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 2.362271
    [namelookup_time] => 0.000847
    [connect_time] => 0.273085
    [pretransfer_time] => 0.825333
    [size_upload] => 1489
    [size_download] => 57727
    [speed_download] => 24439
    [speed_upload] => 630
    [download_content_length] => -1
    [upload_content_length] => 1489
    [starttransfer_time] => 1.0992
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 
    [certinfo] => Array
        (
        )

    [primary_port] => 0
    [local_ip] => 
    [local_port] => 0
    [http_version] => 2
    [protocol] => 2
    [ssl_verifyresult] => 0
    [scheme] => HTTPS
    [appconnect_time_us] => 825272
    [connect_time_us] => 273085
    [namelookup_time_us] => 847
    [pretransfer_time_us] => 825333
    [redirect_time_us] => 0
    [starttransfer_time_us] => 1099200
    [total_time_us] => 2362271
)

Solution

  • I have an update on this. It turns out that upgrading to a later version of the curl library resolved this. This was catalysed by finding this issue on Github:

    https://github.com/php/php-src/issues/10119

    The dev agency tested a version of the API request on a server with curl 7.80 and it reduced the response time by more than 50%. Hope this helps anyone else experiencing this issue.

    The times are now inline with our tests using .NET and HTTPClient.