PHP Library

If you’re a PHP developer, you will need to get a client for Kommo. Client was written for library PHP League OAuth 2.0 Client, which is also shared openly.

Everything begins the same as in the article ‘Example by steps’ with the registration of an integration. Here it will be explained how to create a simple integration with a prepared client (the same example can be found in the Kommo client in file example.php):

  1. You can add a client in your project with the help of Composer with the command composer require amocrm/oauth2-amocrm
  2. Open file example.php to adapt it for your integration
  3. In variable $provider object of the client is stored, you need to send keys that you get after the registration of the integration and also indicate redirectUri in the constructor of this object (it should be the same as the one that you indicated when you registered an app). For the correct work of Redirect URl in the settings of the Integration, the path to example.php should be the same as on your domain. Redirect URl should be valid and accessible from outbound via protocol HTTPs address.
  4. All logic of the application can be done in strings that we used in the example of a request to method api/v2/account;
  5. Please note that in the example for storing keys the plain file is used with path and a name indicated in the first strings of the example.php: define(‘TOKEN_FILE’, DIRECTORY_SEPARATOR . ‘tmp’ . DIRECTORY_SEPARATOR . ‘token_info.json’) For saving the key use the function saveToken.
    The example that is shown is simplified, it doesn’t have an example of working with several users and accounts, because it saves all keys in one file. It’s required to change the mechanism of storing to a more reliable one, that is used in your application. Used files should be inaccessible for requests from a third-party to prevent keys from being stolen.

Now, when you send a request to the script example.php for the first time, you will see the button of authorization, when you click on that you will see the modal window for getting access to Kommo.

You give the access to the required account, the modal window closes and the parent window will refresh, indicating the name of the user from which you authorized the integration in Kommo.

Now, if you request a file with GET request=1 – example.php?request=1,the request to the method api/v2/account will be done and the ID and the name of the account which you have granted access to, will be shown.