Announcement

Collapse
No announcement yet.

Updating the currency rate by API

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Updating the currency rate by API

    Please help to update currency rates by API.
    I read the documentation, but I just can't implement getting the exchange rate using the API. Please show working examples, at least for the manual introduction of courses. If there are examples of receiving courses from a source, it will be great.

    1.I found an example of how to make a similar solution through the PHP, but it will not work, because when I change the course directly in the database, it does not change in the CRM. Only if you change the course in the CRM will change in the database.

    2. I created the EspoApiClient.php file on the hosting at the root folder. The CRM is located in the "/crm" folder.
    Then I created a file reates.php with this code and placed it at the root. API User has rights to edit Currency Rates


    <?php

    require_once('EspoApiClient.php');

    $url = 'https://my_site.com/crm/';
    $apiKey = '2e5ca0f738***********9a6efc60684e9';

    $client = new EspoApiClient($url);
    $client->setApiKey($apiKey);

    $response = $client->request('PUT', 'Currency', [
    'EUR' => '7777',
    'USD' => '9999',
    ]);
    ?>


    I am trying to call by link but to no avail
    https://my_site.com/reates.php


    3. Request to update specific rates: PUT api/v1/CurrencyRate, with JSON payload.
    Payload example:

    {
    "EUR": 1.11,
    "UAH": 0.037
    }

    Are there any more detailed instructions on how to do this and where to insert this code ??

    ---------------------------------------------------

    PUT api/v1/CurrencyRate HTTP/1.1
    Content-Type: application/json; charset=UTF-8"

    {
    "EUR": 1.11,
    "UAH": 0.037
    }

    -------------------------------------------------------
    Is that how it should be?
    I do not understand in what format the file with this code should be and where to place it for it to work correctly.
    There is no Currency entity in the Workflow to make the PUT request. http://prntscr.com/v2lysi
    Last edited by sobolevfff; 10-19-2020, 08:39 PM.

  • #2
    In the code you provided you call 'Currency' rather than 'CurrencyRate' as you should.

    Comment


    • #3
      Fixed, but then this error


      [19-Oct-2020 20:42:36 UTC] PHP Fatal error: Uncaught Exception: EspoClient: Unknown Error in /home/aaaaaaa/public_html/EspoApiClient.php:135
      Stack trace:
      #0 /home/aaaaaaaa/public_html/rates.php(12): EspoApiClient->request('PUT', 'CurrencyRate', Array)
      #1 {main}
      thrown in /home/aaaaaaaaaa/public_html/EspoApiClient.php on line 135

      Comment


      • #4
        Hello,
        just my 2 cents, field name begin lower : currencyRate ?

        Comment


        • #5
          <?php
          require_once('EspoApiClient.php');

          $url = 'https://aaaaaaaa.com/crm/';
          $apiKey = '2e5ca****************c60684e9';

          $client = new EspoApiClient($url);
          $client->setApiKey($apiKey);

          $response = $client->request('PUT', 'currencyRate', [
          'EUR' => '7777', // line 11
          'USD' => '9999',
          ]);
          ?>


          1. Error. Maybe I need to convert to JSON format first?

          [20-Oct-2020 06:15:22 UTC] PHP Fatal error: Uncaught Exception: EspoClient: Unknown Error in /home/aaaaaaaa/public_html/EspoApiClient.php:135
          Stack trace:
          #0 /home/aaaaaaaa/public_html/rates.php(11): EspoApiClient->request('PUT', 'currencyRate', Array)
          #1 {main}
          thrown in /home/aaaaaaaa/public_html/EspoApiClient.php on line 135.

          -------------------------------------------------------------------------

          2. Such code from the documentation also throws an error. The user has the rights to edit the Lead.

          $response = $client->request('POST', 'Lead', [
          'firstName' => 'Test',
          'lastName' => 'Hello',
          ]);


          [20-Oct-2020 07:08:33 UTC] PHP Fatal error: Uncaught Exception: EspoClient: Unknown Error in /home/aaaaaaaaa/public_html/EspoApiClient.php:135
          Stack trace:
          #0 /home/aaaaaaaa/public_html/lead.php(11): EspoApiClient->request('POST', 'Lead', Array)
          #1 {main}
          thrown in /home/aaaaaaaa/public_html/EspoApiClient.php on line 135

          ---------------------------------------------------------------------------
          3. Such a code from the documentation does not generate an error, but it does not create a new lead either.


          try {
          $response = $client->request('POST', 'Lead', [
          'firstName' => 'Test',
          'lastName' => 'Hello',
          ]);
          } catch (\Exception $e) {
          $errorCode = $e->getCode();
          }

          ------------------------------------------------------------------------

          4. I just can't figure out why manual changes in the MySql database are not displayed in the CRM and disappear after Rebuilding?


          after that, I clean the cache, Rebuild the CRM and again the old course both in the CRM and in the database

          Last edited by sobolevfff; 10-20-2020, 07:15 AM.

          Comment


          • #6
            Hello,
            only one table : Currency
            with field id, deleted, rate

            so maybe

            $response = $client->request('POST', 'Currency', [
            'id' => 'EUR'
            'rate' => 1234
            ]);

            try with only one value .. if ok then you can make 2 POST.

            Maybe

            Comment


            • #7
              This is progress, but still with an error.

              PHP Parse error: syntax error, unexpected ''rate'' (T_CONSTANT_ENCAPSED_STRING), expecting ']' in /home/aaaaaaaa.com/public_html/rates.php on line 12

              Comment


              • #8
                Post your script :
                is see here a missing ,

                'id' => 'EUR' , <--
                'rate' => 1234

                and you can maybe use this : https://github.com/Rackbeat/php-currency-api
                easly in a jobs
                Last edited by item; 10-20-2020, 01:24 PM.

                Comment


                • #9
                  I put a comma but it didn't help.

                  PHP Fatal error: Uncaught Exception: EspoClient: Unknown Error in /home/aaaaaaa/public_html/EspoApiClient.php:135
                  Stack trace:
                  #0 /home/aaaaaaaa/public_html/rates.php(11): EspoApiClient->request('PUT', 'Currency', Array)
                  #1 {main}
                  thrown in /home/aaaaaaaa/public_html/EspoApiClient.php on line 135

                  Comment


                  • #10
                    PHP Code:

                    try {
                        
                    $response $client->request(
                            
                    'PUT',
                            
                    'CurrencyRate',
                            [
                    'EUR' => 1.19]
                        );
                    } catch (
                    \Exception $e) {
                        
                    $errorCode $e->getCode();


                    If an error occurs, check your espo log (data/log). It could be that your user does not have permissions to change currencies.

                    Comment


                    • #11
                      Thanks. Now there are no errors, but nothing works or changes.
                      I tried such options, there are no errors, but there are no changes in the course either.
                      Nothing changes in MySql either

                      'CurrencyRate'
                      'currencyRate'
                      'currency'

                      User rights

                      Comment


                      • #12
                        What is your base currency (Administration > Currency)? What values do you send?

                        What is your EspoCRM version?

                        Comment


                        • #13
                          I send 1.7777

                          Current version: 5.9.4

                          Comment


                          • #14
                            Maybe the point is that everything is located on the same hosting, but in different folders?

                            Comment


                            • #15
                              Your code is wrong. Please check my snippet above. Change 'currency' => 'CurrencyRate'

                              Comment

                              Working...
                              X