Fatal PHP error on Opportunity creation via API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • igrp
    Junior Member
    • May 2017
    • 18

    Fatal PHP error on Opportunity creation via API

    Hi !

    Could you please help with the Fatal PHP error on Opportunity creation via API:

    Espo error_log:

    [2018-01-25 15:34:44] Espo.WARNING: E_WARNING: Invalid argument supplied for foreach() {"code":2,"message":"Invalid argument supplied for foreach()","file":"/bhome/part3/****/application/Espo/Services/Record.php","line":521,"context":{"data":"{"stage" :"Closed Won","leadSource":"Web Site","assignedUserId":"5a69a352b353dcc31","assign edUserName":"robot","probability":100,"name":"Robo Market-1000","amount":1100,"amountCurrency":"RUB","closeD ate":"2018-01-25","description"...


    My Internal Error log:
    [Thu Jan 25 23:39:59 2018] [error] [client 62.113.**] PHP Fatal error: Cannot unset string offsets in /bhome/part3/03/***/crm/application/Espo/Services/Record.php on line 571


    JSON body of API request:
    {"stage":"Closed Won","leadSource":"Web Site","assignedUserId":"5a69a352b353dcc31","assign edUserName":"robot","probability":100,"name":"Deal # 12345","amount":1999,"amountCurrency":"RUB","close Date":"2018-01-26","description":"Deal description","accountName":null,"accountId":null," contactsIds":[],"contactsNames":{},"contactsColumns":{},"teams Ids ":[],"teamsNames":{}}


    PS. connection / user-password & token are Ok, no issues on that (query is working fine)

    PPS. EspoCRM v.4.6.0
    Last edited by igrp; 01-28-2018, 09:34 AM.
  • tanya
    Senior Member
    • Jun 2014
    • 4308

    #2
    Hello
    How do you call the API? The code, please.

    Comment

    • igrp
      Junior Member
      • May 2017
      • 18

      #3
      Hi ! The code:

      Code:
              $api = new \GuzzleHttp\Client(['base_uri' => $espoAPIurl]);
      
              $b64 = base64_encode($espoUser . ':' . $espoPass);
      
              $appLog->debug('Trying to connect to EspoCRM with REST on ' . $espoAPIurl . ' User = ' . $espoUser . ' B64 =' . $b64 );
      
              //get Espo token
              $result = $api->get(
                  "App/user",
                  [
                      'auth' => [$espoUser, $espoPass],
                      'headers' => ['Espo-Authorization' => $b64 ]
                  ]);
      
              //check result
              if($result->getStatusCode() != 200) {
                  $appLog->debug('EspoCRM authorization failed (user name or password error). HTTP response = ' . $result->getStatusCode() . '. Terminated.');
                  http_response_code(401);
                  exit;
              }
      
              $appLog->debug('Saving an Opportunity to EspoCRM.');
      
              $bodyToSend = json_encode(
                                      [
                                          'stage' => 'Closed Won',
                                          'leadSource' => 'Web Site',
                                          'assignedUserId' => $espoAuthUserID,
                                          'assignedUserName' => $espoAuthUName,
                                          'probability' => 100,
                                          'name' => 'Deal # 12345',
                                          'amount' => 1999.00,
                                          'amountCurrency' => 'RUB',
                                          'closeDate' => date("Y-m-d"),
                                          'description' => 'Deal description',
                                          'accountName' => null,
                                          'accountId' => null,
                                          'contactsIds' => array(),
                                          'contactsNames' => new ArrayObject(),
                                          'contactsColumns' => new ArrayObject(),
                                          'teamsIds' => array(),
                                          'teamsNames' => new ArrayObject()
                                      ]);
      
              $appLog->debug('Body to send: ' . $bodyToSend );
      
              $result = $api->post(
                  "Opportunity",
                  [
                      'auth' => [$espoUser, $espoPass],
                      'headers' => ['Espo-Authorization' => $b64 ],
                      'body' => $bodyToSend
                  ]);

      Comment

      • tanya
        Senior Member
        • Jun 2014
        • 4308

        #4
        Add to headers "Content-type" => "application/json"

        Comment

        • igrp
          Junior Member
          • May 2017
          • 18

          #5
          Thank you!!! It was really a dumb mistake :-\ from my side.

          In case someone need to solve such kind of error - just change to the following in the above code:
          Code:
                  $result = $api->post(
                      "Opportunity",
                      [
                          'auth' => [$espoUser, $espoPass],
                          'headers' => [    'Espo-Authorization' => $b64,
                                          'Content-type' => 'application/json' ],
                          'body' => $bodyToSend
                      ]);

          Comment

          Working...