0

I have been working with an API where I'm calling it from a wordpress custom plugin. One of the response parameters that i need is Uid which is a long integer

This is what i'm getting on postmon and what is correct and i need to get this value on php too

But when I try to print same response on php it rounds the number somehow??

Here is what i get on php side which is wrong!

Im using wp_remote_post method from wordpress. I was thinking if there is a way to manually assign that Uid to string not integer to avoid rounding.

my Code:

    // Create new endpoint to get all products Data
add_action('rest_api_init', 'eos_store_rest_ajax_endpoint');

function eos_store_rest_ajax_endpoint()
{
    register_rest_route(
        'eos-store',
        'products',
        [
            'methods'               => 'GET',
            'permission_callback'   => '__return_true',
            'callback'              => 'eos_store_rest_ajax_callback'
        ]
    );
}

function eos_store_rest_ajax_callback()
{
    $url = "https://host:port/pospal-api2/openapi/v1/productOpenApi/queryProductPages";
    $response = wp_remote_post(
        $url,
        array(
    'method' => 'POST',
    'headers' => array(
        'data-signature' => 'DATA-Signature',
        'time-stamp' => '1641903969',
        'Content-Type' => 'application/json;charset=UTF-8'
    ),
    'body' => json_encode(array( 'appId' => 'APPID' )),
    'cookies' => array()
    )
    );

    if (is_wp_error($response)) {
        $error_message = $response->get_error_message();
    
        return  "Something went wrong: $error_message";
    } else {
        return $response
    }
}
  • It wont change automatically. share your code. – Vel Jan 12 '22 at 06:04
  • Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Jan 12 '22 at 06:08
  • I have edited the question and added my code sample – Bashir Yousufy Jan 12 '22 at 06:14
  • When an integer value in JSON is outside of the PHP integer range, then it will automatically be converted into a float, and at that point you lose precision. The `JSON_BIGINT_AS_STRING` flag would have to be set in the `json_decode` call (wherever that actually happens), to treat them as string values instead. – CBroe Jan 12 '22 at 07:34
  • Actually the problem was not with php side, it was actually from Javascript while parsing the json response body. Since Javascript does not support big Integer normally and while parsing big integer from json it loses its precision. – Bashir Yousufy Jan 13 '22 at 02:27
  • Accepted answer on this StackOverFlow question helped me temporary fix my issue. https://stackoverflow.com/questions/20408537/parsing-big-numbers-in-json-to-strings – Bashir Yousufy Jan 13 '22 at 02:30

1 Answers1

0

Actually the problem was not with php side, it was actually from Javascript while parsing the json response body. Since Javascript does not support big Integer normally and while parsing big integer from json it loses its precision.

Accepted answer on this StackOverFlow question helped me temporary fix my issue. https://stackoverflow.com/a/20408845/13858082