From d617d9512327d93d8961e26cd7e8d41de29ab4ca Mon Sep 17 00:00:00 2001 From: Paulius Petkus Date: Tue, 16 Jul 2024 17:09:37 +0300 Subject: [PATCH 1/5] Updated licence --- LICENSE | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index 27d98f4..8a433e3 100644 --- a/LICENSE +++ b/LICENSE @@ -1,10 +1,10 @@ ConvertAPI https://www.convertapi.com -(c) 2011-2018 Baltsoft +(c) 2011-2024 ConvertApi ConvertAPI.PHP https://github.com/ConvertAPI/convertapi-php -Copyright (c) 2018 Tomas Rutkauskas +Copyright (c) 2024 ConvertApi The MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: From 12ee17210c0d9ce313cf9ed325765a9924dcfa98 Mon Sep 17 00:00:00 2001 From: laurynas-convertapi <38224044+laurynas-convertapi@users.noreply.github.com> Date: Sat, 14 Sep 2024 09:12:21 +0300 Subject: [PATCH 2/5] Remove alternative converter support (#61) Co-authored-by: Laurynas Butkus --- examples/alternative_converter.php | 19 ------------------- examples/error_handling.php | 2 +- lib/ConvertApi/Task.php | 15 +-------------- tests/ConvertApi/ConvertApiTest.php | 9 --------- 4 files changed, 2 insertions(+), 43 deletions(-) delete mode 100644 examples/alternative_converter.php diff --git a/examples/alternative_converter.php b/examples/alternative_converter.php deleted file mode 100644 index a2fd9bf..0000000 --- a/examples/alternative_converter.php +++ /dev/null @@ -1,19 +0,0 @@ - $upload, 'converter' => 'openoffice']); -$savedFiles = $result->saveFiles($dir); - -echo "The PDF saved to:\n"; -print_r($savedFiles); diff --git a/examples/error_handling.php b/examples/error_handling.php index 1cbda86..ae0f0c9 100644 --- a/examples/error_handling.php +++ b/examples/error_handling.php @@ -7,7 +7,7 @@ ConvertApi::setApiSecret(getenv('CONVERT_API_SECRET')); try { - $result = ConvertApi::convert('png', ['File' => 'files/test.docx', 'converter' => 'DUMMY']); + $result = ConvertApi::convert('svg', ['File' => 'files/test.docx']); } catch (\ConvertApi\Error\Api $error) { echo "Got API error code: " . $error->getCode() . "\n"; echo $error->getMessage(); diff --git a/lib/ConvertApi/Task.php b/lib/ConvertApi/Task.php index fc47805..27f6ea8 100644 --- a/lib/ConvertApi/Task.php +++ b/lib/ConvertApi/Task.php @@ -36,9 +36,7 @@ function run() } $fromFormat = $this->fromFormat ?: $this->detectFormat($params); - $converter = $this->detectConverter($params); - $converterPath = $converter ? "/converter/{$converter}" : ''; - $path = 'convert/' . $fromFormat . '/to/' . $this->toFormat . $converterPath; + $path = 'convert/' . $fromFormat . '/to/' . $this->toFormat; $response = ConvertApi::client()->post($path, $params, $readTimeout); @@ -97,15 +95,4 @@ private function detectFormat($params) return $detector->run(); } - - private function detectConverter($params) - { - $keys = array_keys($params); - - foreach ($keys as $key) - if (strtolower($key) == 'converter') - return $params[$key]; - - return; - } } diff --git a/tests/ConvertApi/ConvertApiTest.php b/tests/ConvertApi/ConvertApiTest.php index 17fb76e..cc71731 100644 --- a/tests/ConvertApi/ConvertApiTest.php +++ b/tests/ConvertApi/ConvertApiTest.php @@ -117,15 +117,6 @@ public function testConvertWithStoreFileFalse() $this->assertEquals('test.pdf', $result->getFile()->getFileName()); } - public function testConvertWithAltnativeConverter() - { - $params = ['File' => 'examples/files/test.docx', 'converter' => 'openoffice']; - - $result = ConvertApi::convert('pdf', $params); - - $this->assertEquals('test.pdf', $result->getFile()->getFileName()); - } - public function testConvertWithFileUpload() { $fileUpload = new \ConvertApi\FileUpload('examples/files/test.docx', 'custom.docx'); From 4a4ad69eec0d6a84c9eccaeb55832da1e9afd7f2 Mon Sep 17 00:00:00 2001 From: laurynas-convertapi <38224044+laurynas-convertapi@users.noreply.github.com> Date: Sat, 14 Sep 2024 11:07:45 +0300 Subject: [PATCH 3/5] Update usage example (#63) --- README.md | 4 ++-- tests/ConvertApi/ConvertApiTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5aa4ab2..c24ab0d 100644 --- a/README.md +++ b/README.md @@ -111,12 +111,12 @@ $result = ConvertApi::convert( ### User information -You can always check your remaining seconds amount programmatically by fetching [user information](https://www.convertapi.com/doc/user). +You can always check your usage by fetching [user information](https://www.convertapi.com/doc/user). ```php $info = ConvertApi::getUser(); -echo $info['SecondsLeft']; +echo $info['ConversionsConsumed']; ``` ### Alternative domain diff --git a/tests/ConvertApi/ConvertApiTest.php b/tests/ConvertApi/ConvertApiTest.php index cc71731..c08fb96 100644 --- a/tests/ConvertApi/ConvertApiTest.php +++ b/tests/ConvertApi/ConvertApiTest.php @@ -45,7 +45,7 @@ public function testGetUser() $user_info = ConvertApi::getUser(); $this->assertIsArray($user_info); - $this->assertArrayHasKey('SecondsLeft', $user_info); + $this->assertArrayHasKey('ConversionsConsumed', $user_info); } public function testUploadFile() From 3c59e01070b6c2732476512e5139027179bc27bf Mon Sep 17 00:00:00 2001 From: laurynas-convertapi <38224044+laurynas-convertapi@users.noreply.github.com> Date: Sat, 14 Sep 2024 11:25:13 +0300 Subject: [PATCH 4/5] Switch to authorization header (#62) --- README.md | 4 ++-- examples/conversions_chaining.php | 4 ++-- examples/convert_stream.php | 4 ++-- examples/convert_url_to_pdf.php | 4 ++-- examples/convert_word_to_pdf_and_png.php | 4 ++-- examples/create_pdf_thumbnail.php | 4 ++-- examples/error_handling.php | 4 ++-- examples/retrieve_user_information.php | 4 ++-- examples/split_and_merge_pdf.php | 4 ++-- lib/ConvertApi/Client.php | 7 +++++-- lib/ConvertApi/ConvertApi.php | 18 +++++++++--------- tests/ConvertApi/ConvertApiTest.php | 10 +++++----- 12 files changed, 37 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index c24ab0d..eee3349 100644 --- a/README.md +++ b/README.md @@ -44,12 +44,12 @@ If you use Composer, these dependencies should be handled automatically. If you ### Configuration -You can get your secret at https://www.convertapi.com/a +You can get your API credentials at https://www.convertapi.com/a ```php use \ConvertApi\ConvertApi; -ConvertApi::setApiSecret('your-api-secret'); +ConvertApi::setApiCredentials('your-api-secret-or-token'); ``` ### File conversion diff --git a/examples/conversions_chaining.php b/examples/conversions_chaining.php index de4593f..8bbebf4 100644 --- a/examples/conversions_chaining.php +++ b/examples/conversions_chaining.php @@ -3,8 +3,8 @@ use \ConvertApi\ConvertApi; -# set your api secret -ConvertApi::setApiSecret(getenv('CONVERT_API_SECRET')); +# set your api secret or token +ConvertApi::setApiCredentials(getenv('CONVERT_API_SECRET')); # Short example of conversions chaining, the PDF pages extracted and saved as separated JPGs and then ZIP'ed # https://www.convertapi.com/doc/chaining diff --git a/examples/convert_stream.php b/examples/convert_stream.php index 227b7d9..c3e0481 100644 --- a/examples/convert_stream.php +++ b/examples/convert_stream.php @@ -3,8 +3,8 @@ use \ConvertApi\ConvertApi; -# set your api secret -ConvertApi::setApiSecret(getenv('CONVERT_API_SECRET')); +# set your api secret or token +ConvertApi::setApiCredentials(getenv('CONVERT_API_SECRET')); # Example of converting content stream to PDF # https://www.convertapi.com/txt-to-pdf diff --git a/examples/convert_url_to_pdf.php b/examples/convert_url_to_pdf.php index 34c9ff0..aa06962 100644 --- a/examples/convert_url_to_pdf.php +++ b/examples/convert_url_to_pdf.php @@ -3,8 +3,8 @@ use \ConvertApi\ConvertApi; -# set your api secret -ConvertApi::setApiSecret(getenv('CONVERT_API_SECRET')); +# set your api secret or token +ConvertApi::setApiCredentials(getenv('CONVERT_API_SECRET')); # Example of converting Web Page URL to PDF file # https://www.convertapi.com/web-to-pdf diff --git a/examples/convert_word_to_pdf_and_png.php b/examples/convert_word_to_pdf_and_png.php index 1ce2ed3..3c83ac8 100644 --- a/examples/convert_word_to_pdf_and_png.php +++ b/examples/convert_word_to_pdf_and_png.php @@ -3,8 +3,8 @@ use \ConvertApi\ConvertApi; -# set your api secret -ConvertApi::setApiSecret(getenv('CONVERT_API_SECRET')); +# set your api secret or token +ConvertApi::setApiCredentials(getenv('CONVERT_API_SECRET')); # Example of saving Word docx to PDF and to PNG # https://www.convertapi.com/docx-to-pdf diff --git a/examples/create_pdf_thumbnail.php b/examples/create_pdf_thumbnail.php index a111eb5..4cd271e 100644 --- a/examples/create_pdf_thumbnail.php +++ b/examples/create_pdf_thumbnail.php @@ -3,8 +3,8 @@ use \ConvertApi\ConvertApi; -# set your api secret -ConvertApi::setApiSecret(getenv('CONVERT_API_SECRET')); +# set your api secret or token +ConvertApi::setApiCredentials(getenv('CONVERT_API_SECRET')); # Example of extracting first page from PDF and then chaining conversion PDF page to JPG. # https://www.convertapi.com/pdf-to-extract diff --git a/examples/error_handling.php b/examples/error_handling.php index ae0f0c9..16dcf06 100644 --- a/examples/error_handling.php +++ b/examples/error_handling.php @@ -3,8 +3,8 @@ use \ConvertApi\ConvertApi; -# set your api secret -ConvertApi::setApiSecret(getenv('CONVERT_API_SECRET')); +# set your api secret or token +ConvertApi::setApiCredentials(getenv('CONVERT_API_SECRET')); try { $result = ConvertApi::convert('svg', ['File' => 'files/test.docx']); diff --git a/examples/retrieve_user_information.php b/examples/retrieve_user_information.php index 77d0d50..d8db60c 100644 --- a/examples/retrieve_user_information.php +++ b/examples/retrieve_user_information.php @@ -3,8 +3,8 @@ use \ConvertApi\ConvertApi; -# set your api secret -ConvertApi::setApiSecret(getenv('CONVERT_API_SECRET')); +# set your api secret or token +ConvertApi::setApiCredentials(getenv('CONVERT_API_SECRET')); # Retrieve user information # https://www.convertapi.com/doc/user diff --git a/examples/split_and_merge_pdf.php b/examples/split_and_merge_pdf.php index a8a20b1..69171cf 100644 --- a/examples/split_and_merge_pdf.php +++ b/examples/split_and_merge_pdf.php @@ -3,8 +3,8 @@ use \ConvertApi\ConvertApi; -# set your api secret -ConvertApi::setApiSecret(getenv('CONVERT_API_SECRET')); +# set your api secret or token +ConvertApi::setApiCredentials(getenv('CONVERT_API_SECRET')); # Example of extracting first and last pages from PDF and then merging them back to new PDF. # https://www.convertapi.com/pdf-to-split diff --git a/lib/ConvertApi/Client.php b/lib/ConvertApi/Client.php index 64e2caf..0ec1c42 100644 --- a/lib/ConvertApi/Client.php +++ b/lib/ConvertApi/Client.php @@ -169,12 +169,15 @@ private function parseResponse($response) private function url($path) { - return ConvertApi::$apiBase . $path . '?secret=' . ConvertApi::getApiSecret(); + return ConvertApi::$apiBase . $path; } private function defaultHeaders() { - return ['Accept: application/json']; + return [ + 'Accept: application/json', + 'Authorization: Bearer ' . ConvertApi::getApiCredentials(), + ]; } private function userAgent() diff --git a/lib/ConvertApi/ConvertApi.php b/lib/ConvertApi/ConvertApi.php index 0b0547c..fed6c0b 100644 --- a/lib/ConvertApi/ConvertApi.php +++ b/lib/ConvertApi/ConvertApi.php @@ -12,8 +12,8 @@ class ConvertApi // ConvertAPI client version. const VERSION = '2.0.1'; - // @var string The Convert API secret. You can get your secret at https://www.convertapi.com/a - public static $apiSecret; + // @var string The Convert API credentials. You can get your credentials at https://www.convertapi.com/a + public static $apiCredentials; // @var string The base URL for the Convert API public static $apiBase = 'https://v2.convertapi.com/'; @@ -40,21 +40,21 @@ class ConvertApi private static $_client; /** - * @return string The API secret used for requests. + * @return string The API credentials used for requests. */ - public static function getApiSecret() + public static function getApiCredentials() { - return self::$apiSecret; + return self::$apiCredentials; } /** - * Sets API secret used for requests. + * Sets API secret or token used for requests. * - * @param string $apiSecret + * @param string $apiCredentials */ - public static function setApiSecret($apiSecret) + public static function setApiCredentials($apiCredentials) { - self::$apiSecret = $apiSecret; + self::$apiCredentials = $apiCredentials; } /** diff --git a/tests/ConvertApi/ConvertApiTest.php b/tests/ConvertApi/ConvertApiTest.php index c08fb96..ac97f38 100644 --- a/tests/ConvertApi/ConvertApiTest.php +++ b/tests/ConvertApi/ConvertApiTest.php @@ -11,25 +11,25 @@ class ConvertApiTest extends \PHPUnit\Framework\TestCase protected function setUp(): void { // Save original values so that we can restore them after running tests - $this->origApiSecret = ConvertApi::getApiSecret(); + $this->origApiCredentials = ConvertApi::getApiCredentials(); $this->origApiBase = ConvertApi::getApiBase(); $this->origUploadTimeout = ConvertApi::$uploadTimeout; - ConvertApi::setApiSecret(getenv('CONVERT_API_SECRET')); + ConvertApi::setApiCredentials(getenv('CONVERT_API_SECRET')); } protected function tearDown(): void { // Restore original values - ConvertApi::setApiSecret($this->origApiSecret); + ConvertApi::setApiCredentials($this->origApiCredentials); ConvertApi::setApiBase($this->origApiBase); ConvertApi::$uploadTimeout = $this->origUploadTimeout; } public function testConfigurationAccessors() { - ConvertApi::setApiSecret('test-secret'); - $this->assertEquals('test-secret', ConvertApi::getApiSecret()); + ConvertApi::setApiCredentials('test-secret'); + $this->assertEquals('test-secret', ConvertApi::getApiCredentials()); ConvertApi::setApiBase('https://foo.bar'); $this->assertEquals('https://foo.bar', ConvertApi::getApiBase()); From 568344d082a524e490b73f0cb72891a10b295f81 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sat, 14 Sep 2024 11:49:40 +0300 Subject: [PATCH 5/5] Bump version --- lib/ConvertApi/ConvertApi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ConvertApi/ConvertApi.php b/lib/ConvertApi/ConvertApi.php index fed6c0b..3b48f7c 100644 --- a/lib/ConvertApi/ConvertApi.php +++ b/lib/ConvertApi/ConvertApi.php @@ -10,7 +10,7 @@ class ConvertApi { // ConvertAPI client version. - const VERSION = '2.0.1'; + const VERSION = '3.0.0'; // @var string The Convert API credentials. You can get your credentials at https://www.convertapi.com/a public static $apiCredentials;