Sackling
Sackling

Reputation: 1820

Adding JSON to PHP file

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": "[email protected]",
                                      "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?

Upvotes: 0

Views: 56

Answers (1)

Naltroc
Naltroc

Reputation: 1007

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.

Upvotes: 1

Related Questions