0

when trying to json_encode() an array, array not encoding my array is :

$jsonData = array(
        'addressId' => $addid,
        'paymentMethod' => 2,
        'transactionId' => $orderid
);
print_r($jsonData);

it prints :

Array ( [addressId] => 28 [paymentMethod] => 2 [transactionId] => ORDS63375431 )

and json encoding:

$jsonDataEncoded = json_encode($jsonData);
echo $jsonDataEncoded;

it prints :

 {"addressId":"

what i am doing wrong?

here is my full code:

$addid = '<script>document.write($.session.get("addid"));</script>';
    $orderid = $_POST["ORDERID"];
    $accessToken = $_COOKIE['accessToken'];
    $url = 'http://bookwise.co.in/app/api/carts/checkout';
    $ch = curl_init($url);
    $jsonData = array(
        'addressId' => $addid,
        'paymentMethod' => 2,
        'transactionId' => $orderid
    );
    print_r($jsonData);
    $jsonDataEncoded = json_encode($jsonData);
    echo $jsonDataEncoded;
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','X-API-KEY: '.$accessToken.'')); 
    $result = curl_exec($ch);
  • Just tested it online and it works just fine, here's what I'm getting : `string(65) "{"addressId":28,"paymentMethod":2,"transactionId":"ORDS63375431"}"` – teeyo Feb 19 '18 at 12:24
  • `var_dump($jsonData)` please. let's see if you have any invisible characters in there. If not, then we are going to have to close this one as `Off-topic: Cannot Reproduce` – mickmackusa Feb 19 '18 at 12:26
  • @teeyo i am editing my full code just check –  Feb 19 '18 at 12:26
  • @teeyo when i tried json_encode($jsonData, true) getting response but it give me script value which {"addressId":"\u003Cscript\u003Edocument.write($.session.get(\"addid\"));\u003C\/script\u003E","paymentMethod":2,"transactionId":"ORDS63375431"}{"status":false,"message":"Invalid Address"} –  Feb 19 '18 at 12:30
  • 1
    I tried your code online, it seems to properly encode your array to JSON. See - http://codepad.org/8AT2hS4Z – gauravparmar Feb 19 '18 at 12:31
  • 1
    You have invisible script tags in your `$addid` value. – mickmackusa Feb 19 '18 at 12:32
  • like I said before, your code is fine :) ... I did the same as @gauravparmar and it worked perfectly, try it yourself : http://sandbox.onlinephpfunctions.com/code/ec7cba9e491f2a058fc3e2003e6a00038936d81f – teeyo Feb 19 '18 at 12:32
  • The content for `json_encode` must be utf8 encoded. – odan Feb 19 '18 at 12:33
  • @teeyo but why it showing me script value in json and it showing fine in array –  Feb 19 '18 at 12:33
  • have you tried: `$addid=$_SESSION['addid'];`? (and don't forget `session_start()` at the top of your script) If this fixes it, I'll post an answer. – mickmackusa Feb 19 '18 at 12:34
  • @mickmackusa i can not use this you can see my question : https://stackoverflow.com/questions/48863960/get-session-data-by-id –  Feb 19 '18 at 12:36
  • Then I guess you are going to need to do a SJAX request like Rory said – mickmackusa Feb 19 '18 at 12:46
  • The more I read about this matter, the more it seems you have made a rod for your own back. https://stackoverflow.com/a/45212059/2943403 – mickmackusa Feb 19 '18 at 12:59
  • What are you willing to do to remedy your situation? Are you going to copy your js session storage to server-side session storage so that you can access it? – mickmackusa Feb 19 '18 at 13:07
  • @mickmackusa yes exactly. –  Feb 19 '18 at 13:20
  • If you are going to port your js session data to server side storage, then you will have an easy time repairing your snippet. Please remove this question and post a question if you have issues after you have reconfigured things. – mickmackusa Feb 19 '18 at 13:22

0 Answers0