2

I got a "Fatal error: Call to undefined function: json_encode()" on my php file which is hosted on an ovh server. Here is the code:

function get_paypload_giftcard($partnerId, $gcRequestId, $currencyCode, $gcAmount)
{
    $amount = trim($gcAmount);
    $payload = array(
    "creationRequestId" => $gcRequestId,
    "partnerId" => $partnerId,
    "value" =>
    array(
        "currencyCode" => $currencyCode,
        "amount" => floatval($amount)
    )
    );
    return json_encode($payload); 
}

How to solve the problem? Is there any other alternative than json_encode?

3
  • what version of PHP? Is the json PHP extension installed and enabled? php.net/manual/en/function.json-encode.php lists the pre-requisites for using the function. And php.net/manual/en/json.setup.php shows how to get it working. Normally it's installed by default in PHP 5.2 and up. So presumably either your PHP version is ancient, or it's been disabled on purpose for some reason. Commented Oct 12, 2017 at 15:54
  • What version of PHP are you using? Commented Oct 12, 2017 at 15:54
  • yeah the version was the problem its solve now ty Commented Oct 12, 2017 at 16:01

1 Answer 1

1

json_encode() requires php >= 5.2. It sounds like you're server is running something earlier than that (you can check your php version with the phpversion()) function.

Assuming that is the case, you'll need to use a library that mimics the functionality. A quick google search indicates there are bunch of these.

Using the one from https://boutell.com/scripts/jsonwrapper.html

function get_paypload_giftcard($partnerId, $gcRequestId, $currencyCode, $gcAmount)
{
    require_once '/path/to/jsonwrapper.php';

    $amount = trim($gcAmount);
    $payload = array(
    "creationRequestId" => $gcRequestId,
    "partnerId" => $partnerId,
    "value" =>
    array(
        "currencyCode" => $currencyCode,
        "amount" => floatval($amount)
    )
    );
    return json_encode($payload); 
}

Since this library just checks for the existence of the builtin function and adds it if it is missing it should work after requiring it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer i updated the php version to 5.6 on the OVH server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.