Skip to content

ApiClient fatals with "Undefined constant CURL_LOCK_DATA_CONNECT" on PHP builds against libcurl < 7.57 #229

Description

@plopesc

Summary

Since 0.0.72, ApiClient::__construct() unconditionally runs:

if (self::$shareHandle === null) {
    self::$shareHandle = curl_share_init();
    curl_share_setopt(self::$shareHandle, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
}

PHP only defines CURL_LOCK_DATA_CONNECT when the curl extension was compiled against libcurl 7.57.0 or newer. On older builds (for example PHP 8.3.30 on Ubuntu 16.04 with libcurl 7.47.0, still common on managed hosting) every client instantiation throws:

Error: Undefined constant "CyberSource\CURL_LOCK_DATA_CONNECT" in CyberSource\ApiClient->__construct() (lib/ApiClient.php:117)

This makes the SDK unusable on those platforms, and it is a hard failure rather than a degraded one, even though connection sharing is only an optimization. The same code is still present on master and in 0.0.73–0.0.75.

Steps to reproduce

  1. Use PHP whose curl_version()['version'] is below 7.57.0 (php -r 'var_dump(defined("CURL_LOCK_DATA_CONNECT"));' prints false).
  2. Install cybersource/rest-client-php 0.0.72 or later.
  3. new \CyberSource\ApiClient($config, $merchantConfig);

Proposed fix

Guard the share handle on the constant and only attach it when it exists:

if (self::$shareHandle === null && defined('CURL_LOCK_DATA_CONNECT')) {
    self::$shareHandle = curl_share_init();
    curl_share_setopt(self::$shareHandle, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
}

and in callApi():

if (self::$shareHandle !== null) {
    curl_setopt($curl, CURLOPT_SHARE, self::$shareHandle);
}

Newer builds keep connection sharing; older builds fall back to one connection per request instead of failing. Happy to open a PR with this change.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions