This article explains the basics of using PHP SDK classes and methods. In this article I’ll use the Comparison API and show how to compare two documents from a GroupDocs account. Find information about others methods in the Swagger explorer.
Requirements
- PHP 5.3
- Apache ModRewrite
- PHP Curl extension
- PHP Sockets extension (php_sockets.dll)
- GroupDocs PHP SDK
- composer.phar (Download from http://getcomposer.org/download/ or use the included version)
- FatFree framework (https://github.com/bcosca/fatfree)
Initial Installations
-
Learn how to install the Fatfree framework from their Readme: https://github.com/bcosca/fatfree
-
Copy all src folders to the web root folder.
-
Configure composer.jsonto use the required PHP SDK version.
-
To download the required version of PHP SDK with the composer, set this setting to composer.json:
{ "require": { "groupdocs/groupdocs-php": "v1.2.4" } }
-
-
Open a console, cd to the web root folder and run this command:
php composer.phar install
(This downloads the GroupDocs PHP SDK into vendor folder and creates autoload.php).
-
Restart Apache.
Creating the SampleEarlier I wrote about how to configure a sample development environment in the article Implementation of Comparison API sample with GroupDocs PHP SDK. Today let’s discuss a very useful function: file convertion. That way, I won’t hold up an explanation of structure of the project and other general moments. You probably think that file conversion is very difficult and that you must write a tonne of code, and study a lot of documentation about file formats and how to convert them. Luckily, you are wrong: everything is already done by the GroupDocs team. Everything you need to do is just create a form for entering a couple parameters and a few lines of code to take the parameters from the form and transfer them to the Convert method. Let’s look at the part of the sample that will be seen by the user, the form. The screenshot shows how many fields and what data we need. The Client ID and Private Key comes from the GroupDocs account, the file GUID from user storage in GroupDocs. Then chose which type to convert the document to from the list. To make this form, create a .htm file in the “templates” folder in the root folder of the project. Last time we called it Sample1 so lets call the new sample Sample2 Here is code of that file:```
In the code you’ll find comments for each line of code and like the template file, the Controller is very similar to the previous sample. OK let’s look at key moments:
-
Here is a declaration of main variables and getting entered data from the form:
F3::set('userId', ''); F3::set('privateKey', ''); F3::set('fileId', ''); F3::set('convert\_type', ''); $clientId = F3::get('POST\["client\_id"\]'); $privateKey = F3::get('POST\["private\_key"\]'); $fileId = F3::get('POST\["fileId"\]'); $convert\_type = F3::get('POST\["convert\_type"\]');
-
After we get the entered data, we need to create an Async Api object so we can make request to the GroupDocs API. To create this object, we must create a GroupDocsRequestSigner object and transfer the Private Key to it. Then, that we must take this object and transfer it to the ApiClient object. The code for that looks like this:
$signer = new GroupDocsRequestSigner($privateKey); //Create apiClient object $apiClient = new APIClient($signer); //Create AsyncApi object $api = new AsyncApi($apiClient);
-
And here is the moment of conversion. To convert the file, we need to take the AsyncApi and use its Convertmethod with the data provided via the form:
$convert = $api->Convert($clientId, $fileId, $convert\_type, null, null, null, null, null)
As you can see, conversion is a one code line operation - all the dirty work has already done by GroupDocs team. After we convert the file, we’ll check the conversion results and obtaining conversion result info such as job_id and request status. But we can’t receive the new converted file GUID, name and so on at once. To obtain these data, we need to take the received job_id and transfer it to the GetJobDocuments method of our AsyncApi object. This method returns all the info we want. In this sample we generate an iframe which will show the converted file. For that reason we need the new file’s GUID. The code looks like so:
if($convert->status == "Ok") {
//Delay necessary that the inquiry would manage to be processed
sleep(5);
//Make request to api for get document info by job id
$jobInfo = $api->GetJobDocuments($clientId, $convert->result->job\_id);
//Get file guid
$guid = $jobInfo->result->inputs\[0\]->outputs\[0\]->guid;
//Generating iframe
$iframe = '<iframe src="https://apps.groupdocs.com/document-viewer/embed/' . $guid . '" frameborder="0" width="100%" height="600"></iframe>';
}
//If request was successfull - set url variable for template
return F3::set('iframe', $iframe);
Lastly, we only need to transfer data which we want to show to the template:
F3::set('userId', $clientId);
F3::set('privateKey', $privateKey);
F3::set('fileId', $fileId);
F3::set('convert\_type', $convert\_type);
// Process template
echo Template::serve('sample2.htm');