diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..01ee669 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,23 @@ +name: Build +on: [push,pull_request] +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + php-version: + - '5.6' + - '7.0' + - '7.4' + name: PHP ${{ matrix.php-version }} sample + steps: + - uses: actions/checkout@v3 + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + - run: | + curl -s http://getcomposer.org/installer | php + php composer.phar install + - env: + CONVERT_API_SECRET: ${{ secrets.CONVERTAPI_SECRET }} + run: bin/phpunit diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 96b33ba..0000000 --- a/.travis.yml +++ /dev/null @@ -1,14 +0,0 @@ -language: php -before_script: -- curl -s http://getcomposer.org/installer | php -- php composer.phar install -script: bin/phpunit -php: -- '5.6' -- '7.0' -- '7.2' -- '7.3' -- '7.4' -env: - global: - secure: mXU0rtRvLdr/hBvsb9igcC+7pVdSDZFxd4UbRBDxdkq3kzqtqNiyZgF10E9O9gTsSL2laysVsBqJfyEPHYtH9X6eSD+O9w4ydN8VfprEjF9jH/UZS1YGz7Ik2yxPhT2TpEc1T8/y+P0/AdpbP7zjrAaYIdTtYUAJeVSx26BBPO6tg0XS/JzAVK3C1y8Bw7Wz6hwOnm449uR8YorjMvW4M8oLsDdXRWtfw/A9GIwu/Wek4LXvuFRdOqpJwT7jUbQpFppEONXAM/NUzFleJSZcuLIvYwIUpportdOxV5mUjSspawWYKEOfdGT+xeDeGo0wyJbKfqlgJbPGmhj9K1J0EHGDTWhJVPwrM3mMAwRT/xi1W1boCiWch08qVlsUPJzgtZZSTZ2tMz+0X5KSbUWKKl+bl33ypdkCJqflxkNUYmpvIk5oDL4xNNkjnnjNMoJCrT3Rs1mLKrx0NJCoNS5JLgvfnSvzhfw9jsP+MWRxvMjK3VuMArgyhPw+TU9tdHpaCvVsVBEbwV3pdyAq2eFxsEun0iMnTtrt72vksdCeduZAWjxeQoODgFBBnX0jXBQfkobQVoNH2VoW9J9uB5/4bYMabzlfcqKkT7IyHiQsHyrvEqGvgSjW2rc+EvuZgi8+bjS4dKlScLl+pIe+iqnY999l744HGJHldf3HpQo1MQo= diff --git a/README.md b/README.md index bb6b24c..5a58947 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # ConvertAPI PHP Client [![PHP version](https://badge.fury.io/ph/convertapi%2Fconvertapi-php.svg)](https://packagist.org/packages/convertapi/convertapi-php) -[![Build Status](https://secure.travis-ci.org/ConvertAPI/convertapi-php.svg)](http://travis-ci.org/ConvertAPI/convertapi-php) +[![Build Status](https://github.com/ConvertAPI/convertapi-php/actions/workflows/main.yml/badge.svg)](https://github.com/ConvertAPI/convertapi-php/actions) ## Convert your files with our online file conversion API @@ -64,7 +64,7 @@ $result = ConvertApi::convert('pdf', ['File' => '/path/to/my_file.docx']); $result->getFile()->save('/path/to/save/file.pdf'); # get file contents (without saving the file locally) -$content = $result->getFile()->getContents(); +$contents = $result->getFile()->getContents(); ``` Other result operations: diff --git a/examples/conversions_chaining.php b/examples/conversions_chaining.php index c9af123..2e724d6 100644 --- a/examples/conversions_chaining.php +++ b/examples/conversions_chaining.php @@ -20,7 +20,7 @@ echo "Conversions done. Cost: ${cost}. Total files created: ${count}\n"; -$zipResult = ConvertApi::convert('zip', ['Files' => $jpgResult->getFiles()]); +$zipResult = ConvertApi::convert('zip', ['Files' => $jpgResult->getFiles()], 'any'); $cost = $zipResult->getConversionCost(); $count = count($zipResult->getFiles()); diff --git a/examples/error_handling.php b/examples/error_handling.php new file mode 100644 index 0000000..1cbda86 --- /dev/null +++ b/examples/error_handling.php @@ -0,0 +1,15 @@ + 'files/test.docx', 'converter' => 'DUMMY']); +} catch (\ConvertApi\Error\Api $error) { + echo "Got API error code: " . $error->getCode() . "\n"; + echo $error->getMessage(); +} + diff --git a/lib/ConvertApi/Client.php b/lib/ConvertApi/Client.php index 4e9dfbc..ed4513b 100644 --- a/lib/ConvertApi/Client.php +++ b/lib/ConvertApi/Client.php @@ -28,7 +28,7 @@ public function upload($file_or_resource, $fileName) [ 'Content-Type: application/octet-stream', 'Transfer-Encoding: chunked', - "Content-Disposition: attachment; filename*=UTF-8''" . urlencode($fileName), + "Content-Disposition: attachment; filename*=UTF-8''" . rawurlencode($fileName), ] ); @@ -137,6 +137,9 @@ private function handleCurlError($ch) private function checkResponse($ch, $response) { + if (empty($response)) + throw new Error\Api('API response is empty'); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code == 200) diff --git a/lib/ConvertApi/ConvertApi.php b/lib/ConvertApi/ConvertApi.php index 0cb770b..4ac4f74 100644 --- a/lib/ConvertApi/ConvertApi.php +++ b/lib/ConvertApi/ConvertApi.php @@ -10,7 +10,7 @@ class ConvertApi { // ConvertAPI client version. - const VERSION = '1.5.0'; + const VERSION = '1.6.0'; // @var string The Convert API secret. You can get your secret at https://www.convertapi.com/a public static $apiSecret; @@ -57,6 +57,24 @@ public static function setApiSecret($apiSecret) self::$apiSecret = $apiSecret; } + /** + * @return string The API base used for requests. + */ + public static function getApiBase() + { + return self::$apiBase; + } + + /** + * Sets API base used for requests. + * + * @param string $apiBase + */ + public static function setApiBase($apiBase) + { + self::$apiBase = $apiBase; + } + /** * Perform conversion * diff --git a/tests/ConvertApi/ConvertApiTest.php b/tests/ConvertApi/ConvertApiTest.php index 91e36a2..4429f40 100644 --- a/tests/ConvertApi/ConvertApiTest.php +++ b/tests/ConvertApi/ConvertApiTest.php @@ -12,6 +12,7 @@ protected function setUp() { // Save original values so that we can restore them after running tests $this->origApiSecret = ConvertApi::getApiSecret(); + $this->origApiBase = ConvertApi::getApiBase(); $this->origUploadTimeout = ConvertApi::$uploadTimeout; ConvertApi::setApiSecret(getenv('CONVERT_API_SECRET')); @@ -21,6 +22,7 @@ protected function tearDown() { // Restore original values ConvertApi::setApiSecret($this->origApiSecret); + ConvertApi::setApiBase($this->origApiBase); ConvertApi::$uploadTimeout = $this->origUploadTimeout; } @@ -28,6 +30,9 @@ public function testConfigurationAccessors() { ConvertApi::setApiSecret('test-secret'); $this->assertEquals('test-secret', ConvertApi::getApiSecret()); + + ConvertApi::setApiBase('https://foo.bar'); + $this->assertEquals('https://foo.bar', ConvertApi::getApiBase()); } public function testClient() @@ -91,6 +96,16 @@ public function testConvertWithFileUpload() $this->assertEquals('custom.pdf', $result->getFile()->getFileName()); } + public function testConvertWithFileUploadAndSpaces() + { + $fileUpload = new \ConvertApi\FileUpload('examples/files/test.docx', 'test space ačiū.docx'); + $params = ['File' => $fileUpload]; + + $result = ConvertApi::convert('pdf', $params); + + $this->assertEquals('test space ačiū.pdf', $result->getFile()->getFileName()); + } + public function testConvertWithFileResourceUpload() { $fp = fopen('examples/files/test.docx', 'rb'); @@ -117,7 +132,7 @@ public function testConvertWithMultipleFiles() 'Files' => ['examples/files/test.pdf', 'examples/files/test.pdf'] ]; - $result = ConvertApi::convert('zip', $params); + $result = ConvertApi::convert('zip', $params, 'any'); $this->assertEquals('test.zip', $result->getFile()->getFileName()); } @@ -126,7 +141,7 @@ public function testConvertWithUrl() { $params = ['Url' => 'https://www.convertapi.com']; - $result = ConvertApi::convert('pdf', $params); + $result = ConvertApi::convert('pdf', $params, 'web'); $this->assertInternalType('int', $result->getFile()->getFileSize()); } @@ -139,7 +154,7 @@ public function testChainedConversion() $params = ['Files' => $result->getFiles()]; - $result = ConvertApi::convert('zip', $params); + $result = ConvertApi::convert('zip', $params, 'any'); $this->assertEquals('test.zip', $result->getFile()->getFileName()); }