Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Configuration

private static $credentials;

private static $httpClient;

public static function getCredentials()
{
return self::$credentials;
Expand All @@ -18,11 +20,17 @@ public static function getDocumentClient()
return self::$documentClient;
}

public static function getHttpClient()
{
return self::$httpClient;
}

public static function configure(Credentials $credentials, array $httpClientConfig = [])
{
self::$credentials = $credentials;

$httpClient = new \GuzzleHttp\Client($httpClientConfig);
self::$httpClient = $httpClient;

$client = new Client($httpClient);
$client->setBaseUri($credentials->getEndpoint());
Expand Down
45 changes: 22 additions & 23 deletions src/Item/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,24 @@ public function download($dest)
return 'Failed to open destination file for writing';
}

$options = [
CURLOPT_HTTPHEADER => [
"Api-Key: $apiKey",
'User-Agent: didww-php-sdk/'.\Didww\Client::sdkVersion(),
'X-DIDWW-API-Version: '.(\Didww\Configuration::getCredentials()->getVersion() ?? '2026-04-16'),
],
CURLOPT_FILE => $destHandle,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_URL => $this->getAttributes()['url'],
CURLOPT_FAILONERROR => true, // HTTP code > 400 will throw curl error
];
$ch = curl_init();
curl_setopt_array($ch, $options);
$return = curl_exec($ch);
$error = false === $return ? curl_error($ch) : null;
unset($ch);
if ($ownHandle) {
try {
\Didww\Configuration::getHttpClient()->request('GET', $this->getAttributes()['url'], [
'headers' => [
'Api-Key' => $apiKey,
'User-Agent' => 'didww-php-sdk/'.\Didww\Client::sdkVersion(),
'X-DIDWW-API-Version' => \Didww\Configuration::getCredentials()->getVersion() ?? '2026-04-16',
],
'sink' => $destHandle,
'allow_redirects' => true,
// HTTP code >= 400 will throw a GuzzleException, same as CURLOPT_FAILONERROR before.
]);
$error = null;
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
$error = $e->getMessage();
}
Comment on lines +122 to +136
// Guzzle's 'sink' stream wrapper already closes the underlying resource once
// the response body has been written to it.
if ($ownHandle && is_resource($destHandle)) {
fclose($destHandle);
}

Expand All @@ -155,7 +156,7 @@ public function downloadAndDecompress($dest)
return $result;
}

$gz = gzopen($tmpFile, 'rb');
$gz = fopen('compress.zlib://'.$tmpFile, 'rb');
if (false === $gz) {
unlink($tmpFile);

Expand All @@ -165,15 +166,13 @@ public function downloadAndDecompress($dest)
$ownHandle = !is_resource($dest);
$destHandle = $ownHandle ? fopen($dest, 'wb') : $dest;
if ($ownHandle && false === $destHandle) {
gzclose($gz);
fclose($gz);
unlink($tmpFile);

return 'Failed to open destination file for writing';
}
while (!gzeof($gz)) {
fwrite($destHandle, gzread($gz, 8192));
}
gzclose($gz);
stream_copy_to_stream($gz, $destHandle);
fclose($gz);
if ($ownHandle) {
fclose($destHandle);
}
Expand Down
Loading