0

I am trying to add a JSON script to a php file in my sites admin. My goal is to have the JSON run when the order status is change to 3 (shipped).

I am pretty sure I am going about this all wrong but I am not sure what to do yet. here is my code:

          if ( ($check_status['orders_status'] != $status) && $check_status['orders_status'] == 3) { ?>

              <script>
               POST https://api.yotpo.com/oauth/token


                {
                  "client_id": "### Your client_id ###",
                  "client_secret": "### Your client_secret ###",
                  "grant_type": "client_credentials"
                }


                  POST https://api.yotpo.com/myapi/purchases

                    {
                        "validate_data": true,
                                      "platform": "general",
                                      "utoken": "### YOUR UTOKEN ###",
                                      "email": "client@abc.com",
                                      "customer_name": "bob",
                                      "order_id": "order_1",
                                      "order_date": "2010-10-14",
                                      "currency_iso": "USD",
                                      "products": {
                        "SKUaaa12": {
                            "url": "http://example_product_url1.com",
                                          "name": "product1",
                                          "image": "http://images2.fanpop.com/image/photos/13300000/A1.jpg",
                                          "description": "this is the description of a product",
                                          "price": "100",
                                          "specs": {
                                "upc": "USB",
                                             "isbn": "thingy"
                                          },
                                          "product_tags": "books"
                      }
                     }
                    }
</script>

     <?php     } ?>

First of all, there is nothing in my code that says hey, this is JSON besides the tag.

do I need to have the json in a sepearate json file? Or do I need to convert this script to php?

Sackling
  • 1,780
  • 5
  • 37
  • 71

1 Answers1

1

First of all, Nikita is correct that JSON does not run - it is not script. It is a standardized way to store information.

PHP has native JSON handling functions and can easily take existing objects or arrays and convert them to JSON.

<?php

$json = json_encode($my_data);

?>

<input type="hidden" name="post_data" <?php echo 'value="'.$json.'" ?> />

Then when you send this variable $json to the next page, you'll unpack it like so

$my_data = json_decode($_POST['post_data']);

This is a pure PHP implementation, though JavaScript does nice functions to stringify to/from json as well.

Naltroc
  • 989
  • 1
  • 14
  • 34
  • Thanks for laying that out for me to understand a bit better. in the example about would $my_data be set to = the json data itself as text? e.g. $my_date = ""client_id": "### Your client_id ###", "client_secret": "### Your client_secret ###""; – Sackling May 16 '17 at 19:17
  • And secondly how do I post to https://api.yotpo.com/oauth/token instead of just the same server I am on? – Sackling May 16 '17 at 19:37
  • Yes, the return value of the `json_encode([$value])` function is a string of jason formatted data. Its full reference is here http://php.net/manual/en/function.json-encode.php To make posts to other sites not on the same server, you should look at cURL. It is similar to AJAX, which is used by javascript, but runs with PHP and has a lot more power. http://php.net/manual/en/curl.examples-basic.php – Naltroc May 16 '17 at 19:55