Rest API v4_1 is not working with my new setup i.e. php 8.0, SuiteCRM 7.13

Worked for me too. I wonder why this solution hasn’t made it into the code yet. Moreover leaves me wondering how salesagility is testing their versions.
This call fails at any time with php8.x.
Don’t they have automated testing?

Considering that 7.14.x only works with PHP 8.1 and 8.2

@jyotip which fix didn’t work the one I used or # 10091?

@cperrot same question for you. If #10091 doesn’t solve it, it will save me some time testing it.

this worked for me. It is php8 compatible.

1 Like

Hi @pstevens

I tried both the solutions. Kindly check and let me know if I am missing anything-

For # 10091 - I made the below changes in order to fix this bug-

In service/core/REST/SugarRestJSON.php

in service/v4/SugarWebServiceImplv4.php

In below code-

  $url = "{site_url}/service/v4_1/rest.php";
  function call($method, $parameters, $url)
    {
      ob_start();
      $curl_request = curl_init();

      curl_setopt($curl_request, CURLOPT_URL, $url);
      curl_setopt($curl_request, CURLOPT_POST, 1);
      curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
      curl_setopt($curl_request, CURLOPT_HEADER, 1);
      curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

      $jsonEncodedData = json_encode($parameters);

      $post = array(
          "method" => $method,
          "input_type" => "JSON",
          "response_type" => "JSON",
          "rest_data" => $jsonEncodedData
      );

      curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
      $result = curl_exec($curl_request);
      curl_close($curl_request);

      $result = explode("\r\n\r\n", $result, 2);
      $response = json_decode($result[1]);
      ob_end_flush();

      return $response;
    }

  //login -----------------------------------------
  $login_parameters = array(
      "user_auth" => array(
          "user_name" => 'xxxxx',
          "password" => md5('xxxxx') ,
          "version" => "1"
      ) ,
      "application_name" => "RestTest",
      "name_value_list" => array() ,
  );

  $login_result = call("login", $login_parameters, $url);
   
  echo  $session_id = $login_result->id;

I tried both but none is able to fetch the session ID.
“application_name” => “RestTest”, and “application” => “RestTest”,

Solution 2 -
In service/core/REST/SugarRestJSON.php file

I replaced this line $res = call_user_func_array(array( $this->implementation, $method),$data); to $res = $this->implementation->{$method}(…array_values($data)); but getting same error.

Kindly assist if I am missing anything.

Thanks

I don’t believe you’re supposed to submit the password md5 encoded, just plain text.

I tried without md5 just now but getting the same error as before :frowning:

I posted my JSON output above. Compare it to yours see if anything is different. It does work with that one line change I posted.

I am using “application_name” => “RestTest”, and in your JSON it’s [application_name] => Mautic

What error are you getting?

Hi @pstevens

Warning: Undefined array key 1 in /var/www/html/testapi.php on line 37

Warning: Attempt to read property “id” on null in /var/www/html/testapi.php on line 56

What’s line #37 and #56 in your testapi.php?

#37 - $response = json_decode($result[1]);

#56 - echo $session_id = $login_result->id;

You seem to be using a $method variable without initializing it - so the request fails.

In your code, I advise you to always check the return of functions (such as a failed login function returning false or null, no point in trying to access data as if it had returned an object).

1 Like

HI,

I tried initializing the $method variable but I am unable to resolve the issue.

@pstevens can you please share the working code?

Thanks in advance

Here’s an example in the documentation, I think if you modify that to your needs it will work with my suggested change to replace the call_user_func_array()

Hi @pstevens,

I noticed that the serve() function is not returning anything at all, in my working code if I return my name from this function it prints my name but if I return my name with a new version from the serve() function it is not returning anything.

Hello All

kindly assist on this above issue.

Thanks