From bda2df5e9630cace6d4a4bd3517473a2a1c4ee12 Mon Sep 17 00:00:00 2001 From: Oleksandr Knyga Date: Fri, 28 Nov 2014 15:40:31 +0200 Subject: [PATCH 1/9] composer --- .gitignore | 5 + ConvertAPI/Abstract2Image.php | 57 ++++++++ ConvertAPI/Abstract2PDF.php | 57 ++++++++ ConvertAPI/Abstract2PowerPoint.php | 42 ++++++ ConvertAPI/ConvertAPI.php | 212 +++++++++++++++++++++++++++++ ConvertAPI/Email2PDF.php | 27 ++++ ConvertAPI/Excel2PDF.php | 27 ++++ ConvertAPI/Jnt2PDF.php | 26 ++++ ConvertAPI/Lotus2PDF.php | 28 ++++ ConvertAPI/OpenOffice2PDF.php | 34 +++++ ConvertAPI/Pdf2Image.php | 26 ++++ ConvertAPI/Pdf2PowerPoint.php | 26 ++++ ConvertAPI/PostScript2PDF.php | 26 ++++ ConvertAPI/PowerPoint2PDF.php | 27 ++++ ConvertAPI/Project2PDF.php | 26 ++++ ConvertAPI/Publisher2PDF.php | 26 ++++ ConvertAPI/RichText2PDF.php | 26 ++++ ConvertAPI/Snp2PDF.php | 26 ++++ ConvertAPI/Text2PDF.php | 26 ++++ ConvertAPI/Visio2PDF.php | 26 ++++ ConvertAPI/Web2Image.php | 26 ++++ ConvertAPI/Web2PDF.php | 26 ++++ ConvertAPI/Word2PDF.php | 28 ++++ ConvertAPI/Xps2PDF.php | 26 ++++ LICENSE.md | 165 ++++++++++++++++++++++ README.md | 36 +++++ composer.json | 21 +++ example/example.php | 13 ++ example/input.txt | 1 + 29 files changed, 1118 insertions(+) create mode 100644 .gitignore create mode 100644 ConvertAPI/Abstract2Image.php create mode 100644 ConvertAPI/Abstract2PDF.php create mode 100644 ConvertAPI/Abstract2PowerPoint.php create mode 100644 ConvertAPI/ConvertAPI.php create mode 100644 ConvertAPI/Email2PDF.php create mode 100644 ConvertAPI/Excel2PDF.php create mode 100644 ConvertAPI/Jnt2PDF.php create mode 100644 ConvertAPI/Lotus2PDF.php create mode 100644 ConvertAPI/OpenOffice2PDF.php create mode 100644 ConvertAPI/Pdf2Image.php create mode 100644 ConvertAPI/Pdf2PowerPoint.php create mode 100644 ConvertAPI/PostScript2PDF.php create mode 100644 ConvertAPI/PowerPoint2PDF.php create mode 100644 ConvertAPI/Project2PDF.php create mode 100644 ConvertAPI/Publisher2PDF.php create mode 100644 ConvertAPI/RichText2PDF.php create mode 100644 ConvertAPI/Snp2PDF.php create mode 100644 ConvertAPI/Text2PDF.php create mode 100644 ConvertAPI/Visio2PDF.php create mode 100644 ConvertAPI/Web2Image.php create mode 100644 ConvertAPI/Web2PDF.php create mode 100644 ConvertAPI/Word2PDF.php create mode 100644 ConvertAPI/Xps2PDF.php create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 composer.json create mode 100644 example/example.php create mode 100644 example/input.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae03290 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/bin/ +/vendor/ +/composer.lock +/composer.phar +/example/output.pdf \ No newline at end of file diff --git a/ConvertAPI/Abstract2Image.php b/ConvertAPI/Abstract2Image.php new file mode 100644 index 0000000..872b5ba --- /dev/null +++ b/ConvertAPI/Abstract2Image.php @@ -0,0 +1,57 @@ +_additionalParameters[$name] = $value; + } else { + throw new \Exception($name.' must be a string.'); + } + break; + case 'OutputFormat': + if (is_string($value) && in_array($value, array('png', 'jpg', 'tif'))) { + $this->_additionalParameters[$name] = $value; + } else { + throw new \Exception($name.' must be "png", "jpg" or "tif".'); + } + break; + case 'AlternativeParser': case 'StoreFile': + if (is_bool($value)) { + $this->_additionalParameters[$name] = $value; + } else { + throw new \Exception($name.' must be a boolean value.'); + } + break; + case 'Timeout': + if (is_int($value) && $value >= 5 && $value <= 600) { + $this->_additionalParameters[$name] = $value; + } else { + throw new \Exception($name.' must be an integer between 5 and 600.'); + } + break; + } + + } + +} \ No newline at end of file diff --git a/ConvertAPI/Abstract2PDF.php b/ConvertAPI/Abstract2PDF.php new file mode 100644 index 0000000..cdccccc --- /dev/null +++ b/ConvertAPI/Abstract2PDF.php @@ -0,0 +1,57 @@ +_additionalParameters[$name] = $value; + } else { + throw new \Exception($name.' must be a string.'); + } + break; + case 'OutputFormat': + if (is_string($value) && in_array($value, array('pdf', 'pdfa', 'png', 'jpg', 'tif'))) { + $this->_additionalParameters[$name] = $value; + } else { + throw new \Exception($name.' must be "pdf", "pdfa", "png", "jpg" or "tif".'); + } + break; + case 'AlternativeParser': case 'StoreFile': + if (is_bool($value)) { + $this->_additionalParameters[$name] = $value; + } else { + throw new \Exception($name.' must be a boolean value.'); + } + break; + case 'Timeout': + if (is_int($value) && $value >= 5 && $value <= 600) { + $this->_additionalParameters[$name] = $value; + } else { + throw new \Exception($name.' must be an integer between 5 and 600.'); + } + break; + } + + } + +} \ No newline at end of file diff --git a/ConvertAPI/Abstract2PowerPoint.php b/ConvertAPI/Abstract2PowerPoint.php new file mode 100644 index 0000000..01154a9 --- /dev/null +++ b/ConvertAPI/Abstract2PowerPoint.php @@ -0,0 +1,42 @@ +_additionalParameters[$name] = $value; + } else { + throw new \Exception($name.' must be a boolean value.'); + } + break; + case 'Timeout': + if (is_int($value) && $value >= 5 && $value <= 600) { + $this->_additionalParameters[$name] = $value; + } else { + throw new \Exception($name.' must be an integer between 5 and 600.'); + } + break; + } + + } + +} \ No newline at end of file diff --git a/ConvertAPI/ConvertAPI.php b/ConvertAPI/ConvertAPI.php new file mode 100644 index 0000000..66215b8 --- /dev/null +++ b/ConvertAPI/ConvertAPI.php @@ -0,0 +1,212 @@ +. +# + +namespace ConvertAPI; + + /** + * Abstract class for interacting with the convertapi.com APIs. Should be + * extended in order to support each of the available convertapi.com conversion + * methods. + * + * @see http://convertapi.com/ + */ +abstract class ConvertAPI { + + /** + * API key to use when making requests to convertapi.com APIs. + */ + public $apiKey = null; + + /** + * Additional parameters to send to convertapi.com when carrying out a Word to + * PDF conversion. + */ + protected $_additionalParameters = array(); + + /** + * An array of valid input file formats, or a string representing a URL. Will + * be checked before conversion and therefore must be populated by concrete + * classes. + */ + protected $_validInputFormats = array(); + + /* Magic methods. */ + + /** + * Constructor. Optionally sets the API key to use for calls to convertapi.com. + * + * @param string $apiKey Optional convertapi.com API key to use. + */ + public function __construct($apiKey = null) { + + if (!isset($this->_apiUrl)) { + throw new \Exception('Child classes of ConvertAPI must specify a value for $this->_apiUrl.'); + } + + if ($apiKey != null) { + $this->apiKey = $apiKey; + } + + } + + /* Public methods. */ + /** + * Concrete classes must provide a convert method: a method which sends the + * request to convertapi.com and deals with the response. + * + * @param string $inputFilename Full path of file to convert. + * @param string $outputFilename Full path of file to write with converted document. + */ + public function convert($inputFilename, $outputFilename = null) { + + // Check input file (if it's an array of local file extensions)... + $urlInput = false; + if (is_array($this->_validInputFormats)) { + $inputFilenameChunks = explode('.', $inputFilename); + if (in_array(array_pop($inputFilenameChunks), $this->_validInputFormats)) { + if (!is_readable($inputFilename)) { + throw new \Exception('Input file is not readable.'); + } + } else { + throw new \Exception('Invalid input file type.'); + } + } else if ($this->_validInputFormats == 'url') { + if (preg_match('/^https?:\/\//', $inputFilename)) { + $urlInput = true; + } else { + throw new \Exception('Invalid input URL.'); + } + } else { + throw new \Exception('Invalid input format identifier.'); + } + + // Check output file... + if ($outputFilename !== null) { + if (!((file_exists($outputFilename) && is_writable($outputFilename)) || is_writable(dirname($outputFilename)))) { + throw new \Exception('Output file target is not writable.'); + } + } + + // Do conversion... + try { + $convertResponse = $this->_apiRequest($inputFilename, $urlInput); + if ($outputFilename !== null) { + if (file_put_contents($outputFilename, $convertResponse['document'])) { + unset($convertResponse['document']); + return $convertResponse; + } else { + throw new \Exception('Error writing output file.'); + } + } else { + return $convertResponse['document']; + } + } catch (\Exception $e) { + throw $e; + } + + } + + /* Protected methods. */ + + /** + * Send a request to the API. + * + * @param string $filename Full path of file to convert. + * @return array Array containing request details and binary data. See above. + */ + protected function _apiRequest($filename, $urlInput = false) { + + + if (function_exists('curl_init')) { + + // Set the source filename or URL... + if ($urlInput == true) { + $postFields = array('CUrl' => $filename); + } else { + if (is_readable($filename)) { + $postFields = array('File' => new \CurlFile($filename)); + } else { + throw new \Exception('File does not exist or is not readable.'); + } + } + + // Build the rest of the post fields array... + if ($this->apiKey !== null) { + $postFields['ApiKey'] = $this->apiKey; + } + if (isset($this->_additionalParameters) && is_array($this->_additionalParameters)) { + foreach ($this->_additionalParameters AS $key => $value) { + if ($value !== null) { + $postFields[$key] = $value; + } + } + } + + // Carry out the cURL request... + $curlHandle = curl_init(); + curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curlHandle, CURLOPT_POST, true); + curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postFields); + curl_setopt($curlHandle, CURLOPT_URL, $this->_apiUrl); + curl_setopt($curlHandle, CURLOPT_HEADER, true); + $curlReturn = curl_exec($curlHandle); + + // Split the response into headers and body (usually document)... + $curlReturnArray = explode("\r\n\r\n", $curlReturn); + + // Check headers and return the document... + $headers = explode("\r\n", $curlReturnArray[1]); + if ($headers[0] == 'HTTP/1.1 200 OK') { + $returnArray = array('document' => $curlReturnArray[2]); + foreach ($headers AS $headerLine) { + $headerParts = explode(': ', $headerLine); + switch ($headerParts[0]) { + case 'InputFormat': $returnArray['input'] = $headerParts[1]; break; + case 'OutputFormat': $returnArray['output'] = $headerParts[1]; break; + case 'CreditsCost': $returnArray['cost'] = $headerParts[1]; break; + case 'FileSize': $returnArray['size'] = $headerParts[1]; break; + } + } + return $returnArray; + } else { + throw new \Exception('Error converting document: '.trim(array_shift(explode("\n", $curlReturnArray[1])))); + } + + } else { + throw new \Exception('Unable to init cURL. Check PHP is compiled with cURL support.'); + } + + } + + /* Abstract methods. */ + + /** + * Magic setter method. Concrete classes must define this to handle the + * _additionalParametersvariable. It should check and set all valid additional + * parameters for the given API. + * + * @param string $name Name of the additional parameter to set. + * @param string $value Value to set the parameter to. + */ + abstract public function __set($name, $value); + +} diff --git a/ConvertAPI/Email2PDF.php b/ConvertAPI/Email2PDF.php new file mode 100644 index 0000000..d622cdd --- /dev/null +++ b/ConvertAPI/Email2PDF.php @@ -0,0 +1,27 @@ + + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/README.md b/README.md new file mode 100644 index 0000000..21f1ad8 --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +convertapi-php +============== + + PHP library for the [convertapi.com](http://www.convertapi.com) REST API. + +ConvertApi is an online file conversion service for creating PDF and Images from various source files, such as Word documents, web pages and raw HTML. converapi-php provides a set of classes for using the ConvertApi service with PHP. + +Development of this library is not in any way connected with Baltsoft Software. + +Using +----- + +Using the library is simple: + +1. Include the required concrete class ( include('Text2PDF.php'); ) +2. Instantiate the class ( $text2pdf = new ConvertAPI\Text2Pdf(); ) +3. Either: + 1. Carry out the conversion using filesystem path for output ( $text2pdf->convert('/path/to/intput/file.txt', '/path/to/output/file.pdf'); ) + 2. Carry out the conversion using function return for output ( $text2pdf->convert('/path/to/intput/file.txt'); ) + +The conversion will write the converted document to the given filesystem path and return verious details about the conversion (depending on the response from ConvertApi), or return a binary string representation of the document. + +See example/example.php for a working example. + +Extending +--------- + +The library currently supports all the conversions available via convertapi.com. + +Addition of new conversions is simply a matter of extending the abstract ConvertAPI class and + +a) defining: +* $_apiUrl - the URL of the ConvertApi endpoint (see http://www.convertapi.com/ for details) +* $_validInputFormats - an array of document types (extensions) which can be converted + +b) completing function __set() to handle additional valid parameters for the conversion being defined. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6019420 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "confuzzledduck/convertapi-php", + "type": "application", + "description": "PHP library for the convertapi.com REST API.", + "keywords": ["convertapi", "curl", "knyga", "confuzzledduck"], + "homepage": "https://github.com/confuzzledduck/convertapi-php", + "license": "GNU", + "authors": [ + { + "name": "ConfuzzledDuck" + } + ], + "minimum-stability": "stable", + "require": { + "php": ">= 5.4", + "lib-curl": "*" + }, + "autoload": { + "psr-0": { "ConvertAPI\\": "." } + } +} \ No newline at end of file diff --git a/example/example.php b/example/example.php new file mode 100644 index 0000000..d6ea25f --- /dev/null +++ b/example/example.php @@ -0,0 +1,13 @@ +convert(__DIR__.'/input.txt', __DIR__.'/output.pdf'); +} catch (Exception $e) { + var_dump($e->getMessage()); +} \ No newline at end of file diff --git a/example/input.txt b/example/input.txt new file mode 100644 index 0000000..e4b52d7 --- /dev/null +++ b/example/input.txt @@ -0,0 +1 @@ +This file will be converted into a PDF document. \ No newline at end of file From ea8f15f03fa0f507d3cec702478af6f97a618500 Mon Sep 17 00:00:00 2001 From: knyga Date: Fri, 28 Nov 2014 15:47:51 +0200 Subject: [PATCH 2/9] Update README.md --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 21f1ad8..31afda5 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,34 @@ ConvertApi is an online file conversion service for creating PDF and Images from Development of this library is not in any way connected with Baltsoft Software. +Installation via [Composer](http://getcomposer.org/) +----- + + * Install Composer to your project root: + ```bash + curl -sS https://getcomposer.org/installer | php + ``` + + * Add a `composer.json` file to your project: + ```json + { + "repositories": [ + { + "url": "https://github.com/knyga/convertapi-php", + "type": "git" + } + ], + "require": { + "confuzzledduck/convertapi-php": "dev-master" + } + } + ``` + + * Run the Composer installer: + ```bash + php composer.phar install + ``` + Using ----- From 410b9d5621cf0e6e6fc35add10b4270ec617087c Mon Sep 17 00:00:00 2001 From: Oleksandr Knyga Date: Fri, 28 Nov 2014 15:48:31 +0200 Subject: [PATCH 3/9] example php --- example/example.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/example/example.php b/example/example.php index 5d675b4..9f59b2a 100644 --- a/example/example.php +++ b/example/example.php @@ -1,22 +1,12 @@ >>>>>> 5b4972ba305a6a0362d6fa7198cfc62e824ecb57 $text2pdf->convert(__DIR__.'/input.txt', __DIR__.'/output.pdf'); } catch (Exception $e) { var_dump($e->getMessage()); From bff6905fc8c3dbba470da02d464546b277977362 Mon Sep 17 00:00:00 2001 From: Oleksandr Knyga Date: Sat, 29 Nov 2014 02:33:56 +0200 Subject: [PATCH 4/9] postFields for convert --- ConvertAPI/ConvertAPI.php | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/ConvertAPI/ConvertAPI.php b/ConvertAPI/ConvertAPI.php index 66215b8..edf9132 100644 --- a/ConvertAPI/ConvertAPI.php +++ b/ConvertAPI/ConvertAPI.php @@ -75,10 +75,10 @@ public function __construct($apiKey = null) { * * @param string $inputFilename Full path of file to convert. * @param string $outputFilename Full path of file to write with converted document. + * @param array $postFields Basic post options for api request. */ - public function convert($inputFilename, $outputFilename = null) { - - // Check input file (if it's an array of local file extensions)... + public function convert($inputFilename, $outputFilename = null, $postFields = array()) { + // Check input file (if it's an array of local file extensions)... $urlInput = false; if (is_array($this->_validInputFormats)) { $inputFilenameChunks = explode('.', $inputFilename); @@ -99,16 +99,16 @@ public function convert($inputFilename, $outputFilename = null) { throw new \Exception('Invalid input format identifier.'); } - // Check output file... + // Check output file... if ($outputFilename !== null) { if (!((file_exists($outputFilename) && is_writable($outputFilename)) || is_writable(dirname($outputFilename)))) { throw new \Exception('Output file target is not writable.'); } } - // Do conversion... + // Do conversion... try { - $convertResponse = $this->_apiRequest($inputFilename, $urlInput); + $convertResponse = $this->_apiRequest($inputFilename, $urlInput, $postFields); if ($outputFilename !== null) { if (file_put_contents($outputFilename, $convertResponse['document'])) { unset($convertResponse['document']); @@ -131,25 +131,23 @@ public function convert($inputFilename, $outputFilename = null) { * Send a request to the API. * * @param string $filename Full path of file to convert. + * @param array $postFields Basic post options for api request. * @return array Array containing request details and binary data. See above. */ - protected function _apiRequest($filename, $urlInput = false) { - - + protected function _apiRequest($filename, $urlInput = false, $postFields = array()) { if (function_exists('curl_init')) { - - // Set the source filename or URL... + // Set the source filename or URL... if ($urlInput == true) { - $postFields = array('CUrl' => $filename); + $postFields['CUrl'] = $filename; } else { if (is_readable($filename)) { - $postFields = array('File' => new \CurlFile($filename)); + $postFields['File'] = new \CurlFile($filename); } else { throw new \Exception('File does not exist or is not readable.'); } } - // Build the rest of the post fields array... + // Build the rest of the post fields array... if ($this->apiKey !== null) { $postFields['ApiKey'] = $this->apiKey; } @@ -161,7 +159,7 @@ protected function _apiRequest($filename, $urlInput = false) { } } - // Carry out the cURL request... + // Carry out the cURL request... $curlHandle = curl_init(); curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlHandle, CURLOPT_POST, true); @@ -170,10 +168,10 @@ protected function _apiRequest($filename, $urlInput = false) { curl_setopt($curlHandle, CURLOPT_HEADER, true); $curlReturn = curl_exec($curlHandle); - // Split the response into headers and body (usually document)... + // Split the response into headers and body (usually document)... $curlReturnArray = explode("\r\n\r\n", $curlReturn); - // Check headers and return the document... + // Check headers and return the document... $headers = explode("\r\n", $curlReturnArray[1]); if ($headers[0] == 'HTTP/1.1 200 OK') { $returnArray = array('document' => $curlReturnArray[2]); @@ -209,4 +207,4 @@ protected function _apiRequest($filename, $urlInput = false) { */ abstract public function __set($name, $value); -} +} \ No newline at end of file From f06328e748fc061f8752233f3d28e864e6ab975e Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 23 Dec 2014 02:33:50 +0200 Subject: [PATCH 5/9] ppttoimage --- ConvertAPI/PowerPoint2Image.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ConvertAPI/PowerPoint2Image.php diff --git a/ConvertAPI/PowerPoint2Image.php b/ConvertAPI/PowerPoint2Image.php new file mode 100644 index 0000000..4c67e8a --- /dev/null +++ b/ConvertAPI/PowerPoint2Image.php @@ -0,0 +1,27 @@ + Date: Sat, 3 Jan 2015 13:23:58 -0800 Subject: [PATCH 6/9] Update composer.json --- composer.json | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 6019420..d30f740 100644 --- a/composer.json +++ b/composer.json @@ -1,13 +1,17 @@ { - "name": "confuzzledduck/convertapi-php", + "name": "knyga/convertapi-php", "type": "application", "description": "PHP library for the convertapi.com REST API.", "keywords": ["convertapi", "curl", "knyga", "confuzzledduck"], - "homepage": "https://github.com/confuzzledduck/convertapi-php", + "homepage": "https://github.com/knyga/convertapi-php", "license": "GNU", "authors": [ { "name": "ConfuzzledDuck" + }, + { + "name": "Oleksandr Knyga", + "email": "oleksandrknyga@gmail.com" } ], "minimum-stability": "stable", @@ -18,4 +22,4 @@ "autoload": { "psr-0": { "ConvertAPI\\": "." } } -} \ No newline at end of file +} From 15cc8d6d60197d2b582adf3245f3386da88fec4d Mon Sep 17 00:00:00 2001 From: knyga Date: Sat, 3 Jan 2015 13:26:38 -0800 Subject: [PATCH 7/9] Update README.md --- README.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/README.md b/README.md index 31afda5..7e6e014 100644 --- a/README.md +++ b/README.md @@ -18,14 +18,8 @@ Installation via [Composer](http://getcomposer.org/) * Add a `composer.json` file to your project: ```json { - "repositories": [ - { - "url": "https://github.com/knyga/convertapi-php", - "type": "git" - } - ], "require": { - "confuzzledduck/convertapi-php": "dev-master" + "knyga/convertapi-php": "dev-master" } } ``` From bc5a384e1706cce19eb5912e779f0170c9908e80 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 26 Jul 2015 18:54:35 +0300 Subject: [PATCH 8/9] PLATFORM-29-Change-http-to-https-for-convert-api. Done. --- ConvertAPI/ConvertAPI.php | 9 ++++++++- ConvertAPI/Email2PDF.php | 4 +++- ConvertAPI/Excel2PDF.php | 4 +++- ConvertAPI/Jnt2PDF.php | 4 +++- ConvertAPI/Lotus2PDF.php | 4 +++- ConvertAPI/OpenOffice2PDF.php | 4 +++- ConvertAPI/Pdf2Image.php | 4 +++- ConvertAPI/Pdf2PowerPoint.php | 4 +++- ConvertAPI/PostScript2PDF.php | 4 +++- ConvertAPI/PowerPoint2Image.php | 4 +++- ConvertAPI/PowerPoint2PDF.php | 4 +++- ConvertAPI/Project2PDF.php | 4 +++- ConvertAPI/Publisher2PDF.php | 4 +++- ConvertAPI/RichText2PDF.php | 4 +++- ConvertAPI/Snp2PDF.php | 4 +++- ConvertAPI/Text2PDF.php | 4 +++- ConvertAPI/Visio2PDF.php | 4 +++- ConvertAPI/Web2Image.php | 4 +++- ConvertAPI/Web2PDF.php | 4 +++- ConvertAPI/Word2PDF.php | 4 +++- ConvertAPI/Xps2PDF.php | 4 +++- 21 files changed, 68 insertions(+), 21 deletions(-) diff --git a/ConvertAPI/ConvertAPI.php b/ConvertAPI/ConvertAPI.php index edf9132..2072829 100644 --- a/ConvertAPI/ConvertAPI.php +++ b/ConvertAPI/ConvertAPI.php @@ -61,7 +61,14 @@ public function __construct($apiKey = null) { if (!isset($this->_apiUrl)) { throw new \Exception('Child classes of ConvertAPI must specify a value for $this->_apiUrl.'); } - + + if (!isset($this->_https)) { + throw new \Exception('Child classes of ConvertAPI must specify a value for $this->_https.'); + } + + $schema = $this->_https ? 'https:' : 'http:'; + $this->_apiUrl = $schema . $this->_apiUrl; + if ($apiKey != null) { $this->apiKey = $apiKey; } diff --git a/ConvertAPI/Email2PDF.php b/ConvertAPI/Email2PDF.php index d622cdd..e7b943c 100644 --- a/ConvertAPI/Email2PDF.php +++ b/ConvertAPI/Email2PDF.php @@ -12,10 +12,12 @@ */ class Email2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Email2Pdf'; + protected $_apiUrl = '//do.convertapi.com/Email2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/Excel2PDF.php b/ConvertAPI/Excel2PDF.php index bca7c4a..11e3564 100644 --- a/ConvertAPI/Excel2PDF.php +++ b/ConvertAPI/Excel2PDF.php @@ -12,10 +12,12 @@ */ class Excel2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Excel2Pdf'; + protected $_apiUrl = '//do.convertapi.com/Excel2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/Jnt2PDF.php b/ConvertAPI/Jnt2PDF.php index 672e526..2695972 100644 --- a/ConvertAPI/Jnt2PDF.php +++ b/ConvertAPI/Jnt2PDF.php @@ -12,10 +12,12 @@ */ class Jnt2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Jnt2Pdf'; + protected $_apiUrl = '//do.convertapi.com/Jnt2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/Lotus2PDF.php b/ConvertAPI/Lotus2PDF.php index 85aa590..2f274a1 100644 --- a/ConvertAPI/Lotus2PDF.php +++ b/ConvertAPI/Lotus2PDF.php @@ -12,10 +12,12 @@ */ class Email2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Lotus2Pdf'; + protected $_apiUrl = '//do.convertapi.com/Lotus2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/OpenOffice2PDF.php b/ConvertAPI/OpenOffice2PDF.php index 370abe9..71d155f 100644 --- a/ConvertAPI/OpenOffice2PDF.php +++ b/ConvertAPI/OpenOffice2PDF.php @@ -12,10 +12,12 @@ */ class OpenOffice2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/OpenOffice2Pdf'; + protected $_apiUrl = '//do.convertapi.com/OpenOffice2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/Pdf2Image.php b/ConvertAPI/Pdf2Image.php index c6e5852..96b02b1 100644 --- a/ConvertAPI/Pdf2Image.php +++ b/ConvertAPI/Pdf2Image.php @@ -12,10 +12,12 @@ */ class Pdf2Image extends Abstract2Image { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Pdf2Image'; + protected $_apiUrl = '//do.convertapi.com/Pdf2Image'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/Pdf2PowerPoint.php b/ConvertAPI/Pdf2PowerPoint.php index 9f19145..3099d3f 100644 --- a/ConvertAPI/Pdf2PowerPoint.php +++ b/ConvertAPI/Pdf2PowerPoint.php @@ -12,10 +12,12 @@ */ class Pdf2PowerPoint extends Abstract2PowerPoint { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Pdf2PowerPoint'; + protected $_apiUrl = '//do.convertapi.com/Pdf2PowerPoint'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/PostScript2PDF.php b/ConvertAPI/PostScript2PDF.php index f318f16..a571623 100644 --- a/ConvertAPI/PostScript2PDF.php +++ b/ConvertAPI/PostScript2PDF.php @@ -12,10 +12,12 @@ */ class PostScript2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/PostScript2Pdf'; + protected $_apiUrl = '//do.convertapi.com/PostScript2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/PowerPoint2Image.php b/ConvertAPI/PowerPoint2Image.php index 4c67e8a..3f0d25e 100644 --- a/ConvertAPI/PowerPoint2Image.php +++ b/ConvertAPI/PowerPoint2Image.php @@ -12,10 +12,12 @@ */ class PowerPoint2Image extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/PowerPoint2Image'; + protected $_apiUrl = '//do.convertapi.com/PowerPoint2Image'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/PowerPoint2PDF.php b/ConvertAPI/PowerPoint2PDF.php index a4bcaac..548ada5 100644 --- a/ConvertAPI/PowerPoint2PDF.php +++ b/ConvertAPI/PowerPoint2PDF.php @@ -12,10 +12,12 @@ */ class PowerPoint2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/PowerPoint2Pdf'; + protected $_apiUrl = '//do.convertapi.com/PowerPoint2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/Project2PDF.php b/ConvertAPI/Project2PDF.php index 2888714..ab67bd5 100644 --- a/ConvertAPI/Project2PDF.php +++ b/ConvertAPI/Project2PDF.php @@ -12,10 +12,12 @@ */ class Project2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Project2Pdf'; + protected $_apiUrl = '//do.convertapi.com/Project2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/Publisher2PDF.php b/ConvertAPI/Publisher2PDF.php index 40e3b51..41c8f55 100644 --- a/ConvertAPI/Publisher2PDF.php +++ b/ConvertAPI/Publisher2PDF.php @@ -12,10 +12,12 @@ */ class Publisher2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Publisher2Pdf'; + protected $_apiUrl = '//do.convertapi.com/Publisher2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/RichText2PDF.php b/ConvertAPI/RichText2PDF.php index 804ffa2..92a4e08 100644 --- a/ConvertAPI/RichText2PDF.php +++ b/ConvertAPI/RichText2PDF.php @@ -12,10 +12,12 @@ */ class RichText2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/RichText2Pdf'; + protected $_apiUrl = '//do.convertapi.com/RichText2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/Snp2PDF.php b/ConvertAPI/Snp2PDF.php index b160838..961559d 100644 --- a/ConvertAPI/Snp2PDF.php +++ b/ConvertAPI/Snp2PDF.php @@ -12,10 +12,12 @@ */ class Snp2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Snp2Pdf'; + protected $_apiUrl = '//do.convertapi.com/Snp2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/Text2PDF.php b/ConvertAPI/Text2PDF.php index 42f02c6..0dd5b8a 100644 --- a/ConvertAPI/Text2PDF.php +++ b/ConvertAPI/Text2PDF.php @@ -12,10 +12,12 @@ */ class Text2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Text2Pdf'; + protected $_apiUrl = '//do.convertapi.com/Text2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/Visio2PDF.php b/ConvertAPI/Visio2PDF.php index 79e32d4..223edb4 100644 --- a/ConvertAPI/Visio2PDF.php +++ b/ConvertAPI/Visio2PDF.php @@ -12,10 +12,12 @@ */ class Visio2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Visio2Pdf'; + protected $_apiUrl = '//do.convertapi.com/Visio2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/Web2Image.php b/ConvertAPI/Web2Image.php index 5ee3a14..59e7c1d 100644 --- a/ConvertAPI/Web2Image.php +++ b/ConvertAPI/Web2Image.php @@ -12,10 +12,12 @@ */ class Web2Image extends Abstract2Image { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Web2Image'; + protected $_apiUrl = '//do.convertapi.com/Web2Image'; /** * A string indicating that the valid input format is a URL for this diff --git a/ConvertAPI/Web2PDF.php b/ConvertAPI/Web2PDF.php index 056b365..b882144 100644 --- a/ConvertAPI/Web2PDF.php +++ b/ConvertAPI/Web2PDF.php @@ -12,10 +12,12 @@ */ class Web2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Web2Pdf'; + protected $_apiUrl = '//do.convertapi.com/Web2Pdf'; /** * An string indicating that the valid input format is a URL for this diff --git a/ConvertAPI/Word2PDF.php b/ConvertAPI/Word2PDF.php index 1e42f80..c7c4250 100644 --- a/ConvertAPI/Word2PDF.php +++ b/ConvertAPI/Word2PDF.php @@ -12,10 +12,12 @@ */ class Word2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Word2Pdf'; + protected $_apiUrl = '//do.convertapi.com/Word2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the diff --git a/ConvertAPI/Xps2PDF.php b/ConvertAPI/Xps2PDF.php index cfbaf1b..3eb7bb3 100644 --- a/ConvertAPI/Xps2PDF.php +++ b/ConvertAPI/Xps2PDF.php @@ -12,10 +12,12 @@ */ class Xps2Pdf extends Abstract2Pdf { + protected $_https = true; + /** * URL of the appropriate convertapi.com API. */ - protected $_apiUrl = 'http://do.convertapi.com/Xps2Pdf'; + protected $_apiUrl = '//do.convertapi.com/Xps2Pdf'; /** * An array of valid input file formats for this conversion. Overrides the From ea1e385b2c2a42f58bf02afd3e02475cd64a25ed Mon Sep 17 00:00:00 2001 From: G00DWIN Date: Thu, 17 Sep 2015 23:32:34 +0400 Subject: [PATCH 9/9] add uploading files via curlopt_file --- .gitignore | 4 +- ConvertAPI/ConvertAPI.php | 235 ++++++++++++++++++++------------------ 2 files changed, 124 insertions(+), 115 deletions(-) diff --git a/.gitignore b/.gitignore index ae03290..bb887de 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ /vendor/ /composer.lock /composer.phar -/example/output.pdf \ No newline at end of file +/example/output.pdf + +/.idea/ \ No newline at end of file diff --git a/ConvertAPI/ConvertAPI.php b/ConvertAPI/ConvertAPI.php index 2072829..7e1448d 100644 --- a/ConvertAPI/ConvertAPI.php +++ b/ConvertAPI/ConvertAPI.php @@ -22,42 +22,43 @@ namespace ConvertAPI; - /** - * Abstract class for interacting with the convertapi.com APIs. Should be - * extended in order to support each of the available convertapi.com conversion - * methods. - * - * @see http://convertapi.com/ - */ +/** + * Abstract class for interacting with the convertapi.com APIs. Should be + * extended in order to support each of the available convertapi.com conversion + * methods. + * + * @see http://convertapi.com/ + */ abstract class ConvertAPI { - /** - * API key to use when making requests to convertapi.com APIs. - */ + /** + * API key to use when making requests to convertapi.com APIs. + */ public $apiKey = null; - /** - * Additional parameters to send to convertapi.com when carrying out a Word to - * PDF conversion. - */ + /** + * Additional parameters to send to convertapi.com when carrying out a Word to + * PDF conversion. + */ protected $_additionalParameters = array(); - /** - * An array of valid input file formats, or a string representing a URL. Will - * be checked before conversion and therefore must be populated by concrete - * classes. - */ + /** + * An array of valid input file formats, or a string representing a URL. Will + * be checked before conversion and therefore must be populated by concrete + * classes. + */ protected $_validInputFormats = array(); - /* Magic methods. */ + /* Magic methods. */ - /** - * Constructor. Optionally sets the API key to use for calls to convertapi.com. - * - * @param string $apiKey Optional convertapi.com API key to use. - */ + /** + * Constructor. Optionally sets the API key to use for calls to convertapi.com. + * + * @param string $apiKey Optional convertapi.com API key to use. + * @throws \Exception + */ public function __construct($apiKey = null) { - + if (!isset($this->_apiUrl)) { throw new \Exception('Child classes of ConvertAPI must specify a value for $this->_apiUrl.'); } @@ -72,20 +73,22 @@ public function __construct($apiKey = null) { if ($apiKey != null) { $this->apiKey = $apiKey; } - + } - /* Public methods. */ - /** - * Concrete classes must provide a convert method: a method which sends the - * request to convertapi.com and deals with the response. - * - * @param string $inputFilename Full path of file to convert. - * @param string $outputFilename Full path of file to write with converted document. - * @param array $postFields Basic post options for api request. - */ + /* Public methods. */ + /** + * Concrete classes must provide a convert method: a method which sends the + * request to convertapi.com and deals with the response. + * + * @param string $inputFilename Full path of file to convert. + * @param string $outputFilename Full path of file to write with converted document. + * @param array $postFields Basic post options for api request. + * @return Array|bool Returns curl info if $outputFilename = null or true + * @throws \Exception + */ public function convert($inputFilename, $outputFilename = null, $postFields = array()) { - // Check input file (if it's an array of local file extensions)... + // Check input file (if it's an array of local file extensions)... $urlInput = false; if (is_array($this->_validInputFormats)) { $inputFilenameChunks = explode('.', $inputFilename); @@ -106,112 +109,116 @@ public function convert($inputFilename, $outputFilename = null, $postFields = ar throw new \Exception('Invalid input format identifier.'); } - // Check output file... - if ($outputFilename !== null) { - if (!((file_exists($outputFilename) && is_writable($outputFilename)) || is_writable(dirname($outputFilename)))) { - throw new \Exception('Output file target is not writable.'); - } - } - // Do conversion... try { - $convertResponse = $this->_apiRequest($inputFilename, $urlInput, $postFields); - if ($outputFilename !== null) { - if (file_put_contents($outputFilename, $convertResponse['document'])) { - unset($convertResponse['document']); - return $convertResponse; - } else { - throw new \Exception('Error writing output file.'); - } - } else { - return $convertResponse['document']; - } + $convertResponse = $this->_apiRequest($inputFilename, $outputFilename, $urlInput, $postFields); + return $convertResponse; } catch (\Exception $e) { throw $e; } } - /* Protected methods. */ + /* Protected methods. */ - /** - * Send a request to the API. - * - * @param string $filename Full path of file to convert. - * @param array $postFields Basic post options for api request. - * @return array Array containing request details and binary data. See above. - */ - protected function _apiRequest($filename, $urlInput = false, $postFields = array()) { + /** + * Send a request to the API. + * + * @param string $inputFile Full path of file to convert. + * @param string|bool $outputFile + * @param string|bool $urlInput + * @param array $postFields Basic post options for api request. + * @return array|string Array containing request details and binary data or path to file. See above. + * @throws \Exception + */ + protected function _apiRequest($inputFile, $outputFile = false , $urlInput = false, $postFields = array()) { if (function_exists('curl_init')) { - // Set the source filename or URL... + // Set the source filename or URL... if ($urlInput == true) { - $postFields['CUrl'] = $filename; + $postFields['CUrl'] = $inputFile; } else { - if (is_readable($filename)) { - $postFields['File'] = new \CurlFile($filename); + if (is_readable($inputFile)) { + $postFields['File'] = new \CurlFile($inputFile); } else { throw new \Exception('File does not exist or is not readable.'); } } - // Build the rest of the post fields array... - if ($this->apiKey !== null) { - $postFields['ApiKey'] = $this->apiKey; - } - if (isset($this->_additionalParameters) && is_array($this->_additionalParameters)) { - foreach ($this->_additionalParameters AS $key => $value) { - if ($value !== null) { - $postFields[$key] = $value; - } + // Build the rest of the post fields array... + if ($this->apiKey !== null) { + $postFields['ApiKey'] = $this->apiKey; + } + if (isset($this->_additionalParameters) && is_array($this->_additionalParameters)) { + foreach ($this->_additionalParameters AS $key => $value) { + if ($value !== null) { + $postFields[$key] = $value; } } + } + + // Carry out the cURL request... + $curlHandle = curl_init(); + curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curlHandle, CURLOPT_POST, true); + curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postFields); + curl_setopt($curlHandle, CURLOPT_URL, $this->_apiUrl); + + if ($outputFile) { + // Check output file... + + if (!((file_exists($outputFile) && is_writable($outputFile)) || is_writable(dirname($outputFile)))) { + throw new \Exception('Output file target is not writable.'); + } - // Carry out the cURL request... - $curlHandle = curl_init(); - curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curlHandle, CURLOPT_POST, true); - curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postFields); - curl_setopt($curlHandle, CURLOPT_URL, $this->_apiUrl); + $outFile = fopen($outputFile, 'w'); + curl_setopt($curlHandle, CURLOPT_FILE, $outFile); + curl_setopt($curlHandle, CURLOPT_HEADER, false); + } else { curl_setopt($curlHandle, CURLOPT_HEADER, true); - $curlReturn = curl_exec($curlHandle); - - // Split the response into headers and body (usually document)... - $curlReturnArray = explode("\r\n\r\n", $curlReturn); - - // Check headers and return the document... - $headers = explode("\r\n", $curlReturnArray[1]); - if ($headers[0] == 'HTTP/1.1 200 OK') { - $returnArray = array('document' => $curlReturnArray[2]); - foreach ($headers AS $headerLine) { - $headerParts = explode(': ', $headerLine); - switch ($headerParts[0]) { - case 'InputFormat': $returnArray['input'] = $headerParts[1]; break; - case 'OutputFormat': $returnArray['output'] = $headerParts[1]; break; - case 'CreditsCost': $returnArray['cost'] = $headerParts[1]; break; - case 'FileSize': $returnArray['size'] = $headerParts[1]; break; - } + } + $curlReturn = curl_exec($curlHandle); + + if ($outputFile) { + return $curlReturn; + } + + // Split the response into headers and body (usually document)... + $curlReturnArray = explode("\r\n\r\n", $curlReturn); + + // Check headers and return the document... + $headers = explode("\r\n", $curlReturnArray[1]); + if ($headers[0] == 'HTTP/1.1 200 OK') { + $returnArray = array('document' => $curlReturnArray[2]); + foreach ($headers AS $headerLine) { + $headerParts = explode(': ', $headerLine); + switch ($headerParts[0]) { + case 'InputFormat': $returnArray['input'] = $headerParts[1]; break; + case 'OutputFormat': $returnArray['output'] = $headerParts[1]; break; + case 'CreditsCost': $returnArray['cost'] = $headerParts[1]; break; + case 'FileSize': $returnArray['size'] = $headerParts[1]; break; } - return $returnArray; - } else { - throw new \Exception('Error converting document: '.trim(array_shift(explode("\n", $curlReturnArray[1])))); } + return $returnArray; + } else { + throw new \Exception('Error converting document: '.trim(array_shift(explode("\n", $curlReturnArray[1])))); + } } else { throw new \Exception('Unable to init cURL. Check PHP is compiled with cURL support.'); } - + } - /* Abstract methods. */ - - /** - * Magic setter method. Concrete classes must define this to handle the - * _additionalParametersvariable. It should check and set all valid additional - * parameters for the given API. - * - * @param string $name Name of the additional parameter to set. - * @param string $value Value to set the parameter to. - */ + /* Abstract methods. */ + + /** + * Magic setter method. Concrete classes must define this to handle the + * _additionalParametersvariable. It should check and set all valid additional + * parameters for the given API. + * + * @param string $name Name of the additional parameter to set. + * @param string $value Value to set the parameter to. + */ abstract public function __set($name, $value); } \ No newline at end of file