Web-Hulk: what is file_get_contents an array and json - combine that with REST and you got the bulk power of php
Topics

Combining technologies like JSON, REST and php - guarana for your site

When you think of JSON and REST, what are you thinking of? Are you thinking of heros and leftover food? They might be associated, but that's not it. REST stands for Representational state transfer and JSON is simply a way to note down objects in computer engineering. As the image I picked suggests that the concepts are new, let me say this: it is old (good) stuff put into a new packing. So don't let the terms frighten you off. If you look at the code on Wikipedia you will see that JSON is a way of creating order. It makes objects readable, not only for humans, but also for machines. I think it to be myth that whatever machines can read ought to be creepy stuff for humans. I was talking to my son this morning. He had told us, that if you understand the underlying principle of writing code you have got it, and the rest (not REST) just slides in with ease. I asked him to elaborate on this, and he said that understanding the principles of OOP is essential because everything, even an array, is an object.

Let's first look at arrays (don't forget the sneak-peak-button below):

Hello World!

In php you can do all kinds of pranks and stuff, so let's convert this into an array:

Let' have a peek into this object:

Array
(
    [0] => Hello World!
)

Don't work too hard. PHP is the coolest stuff around and a lot of intelligent people have already worked on making things run smooth. Now encode it via PHP and create json notation by using: json_encode($an_array)

["Hello World!"]

Now doing it with a multi dimensional array:

Simply with print_r:

Array
(
    [0] => Array
        (
            [blog] => www.quicksprout.com
            [name] => Neil Patel
        )

)

With print_r and json_encode:

[{"blog":"www.quicksprout.com","name":"Neil Patel"}]

Now let's take a look at REST. Forget the terminology in which the concept is wrapped up. Rest is all about communication, think of it as one worker shouting to another: "Hey, John, I need the hammer. Did you see it." And John answers:"Look there, you're sitting on it." So that's what it is in a nut-shell, one website interacting, communicating with another. Another wording would be web service. Think of it as any kind of service being offered and not being restricted to the web.

So how does Rest work?

The following script takes the order:

Combining technologies like JSON, REST and php - guarana for your site

You give me 5 and I return +1: That makes 6

If you urlencode and json_encode your array, you can pass that, too, to another site.




submit to reddit





Sneak peek of the server side code