/    Sign up×
Community /Pin to ProfileBookmark

Possible to parse JSON from remote application?

Hi all,

I’m currently trying to code up a C program that will generate JSON data and send it to my server to parse. The thing is that this program can run on any machine anywhere (a chat-type program that users can download).

I’m wondering if it’s possible to do this securely with JSON uploads and PHP parsing? This is still in architectural stages so I want to iron out the possibilities before actually coding.

An alternative option is that I can code the chat program to upload data to the MySQL server directly so I don’t need to go through a JSON parser. The only reason I would want to use JSON uploads is because the chat program will have an exposed API. Using JSON as a template for data upload is more programmer-friendly than having them upload with MySQL adds.

Is the alternative option possible? Or is the JSON method?

Stephen

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 15.2010 — Do you mean like using PHP's [url=http://php.net/json_decode]json_decode[/url]() function?
Copy linkTweet thisAlerts:
@NyteauthorNov 15.2010 — Do you mean like using PHP's [url=http://php.net/json_decode]json_decode[/url]() function?[/QUOTE]

That would be to parse the received JSON data on the server, but I'm wondering if it's possible to upload JSON data remotely from a local computer.

Did some more reading and it sounds like I can upload through HTTP or set up a TCP IP socket. I'm also leaning towards JSON-RPC spec. Does this sound possible to do?
Copy linkTweet thisAlerts:
@svidgenNov 15.2010 — Since when did they change the meaning of for to from?[/QUOTE] -- family guy

I imagine you'll need to do some JSON construction and parsing on both ends, ends which will be communicating with each over via HTTP. So, your app will be acting like a browser, to some extent. On the server end, you'll be using json_encode() and json_decode(), or some custom-built functions or methods.

On the client side, you need to find a c/c++ library to perform HTTP, and another to do your encoding and decoding. Or you'll have to write your own. If you're developing on windows, the libraries are there ... you just gotta poke around for 'em. (or look for them on msdn ... )

Point is, the server doesn't know your application isn't a web browser. And your application needs to act like a browser. Unless you're planning on writing a true daemon on the server-side ... are you?
Copy linkTweet thisAlerts:
@svidgenNov 15.2010 — I should note, if you're developing on any other platform, the c/c++ libraries you need are probably there as well. I'm just not sure where you'd find them ?
Copy linkTweet thisAlerts:
@NyteauthorNov 16.2010 — -- family guy

I imagine you'll need to do some JSON construction and parsing on both ends, ends which will be communicating with each over via HTTP. So, your app will be acting like a browser, to some extent. On the server end, you'll be using json_encode() and json_decode(), or some custom-built functions or methods.

On the client side, you need to find a c/c++ library to perform HTTP, and another to do your encoding and decoding. Or you'll have to write your own. If you're developing on windows, the libraries are there ... you just gotta poke around for 'em. (or look for them on msdn ... )

Point is, the server doesn't know your application isn't a web browser. And your application needs to act like a browser. Unless you're planning on writing a true daemon on the server-side ... are you?[/QUOTE]


When you say daemon, do you mean like a background process I have to run to listen in on the HTTP requests? My LAMP webhost should handle all of that already right?

I found C/C++ libraries to initiate HTTP requests so I'm thinking theoretically I can just type in the URL for the PHP page on my server host in the request function and send data to it after encoding as JSON. So yes, this application will sort of be acting like a browser in that it makes requests across the web but hardcoded and very specific.
Copy linkTweet thisAlerts:
@svidgenNov 16.2010 — Yes, a daemon is a process that waits around in the background for an interruption, and then does something with that interruption. And yes, Apache is a daemon. So, if you're aim is to take advantage of that, then you need to code your server-side component(s) as though they're web pages or AJAX requests.

So again, server-side you'll probably find it easiest to use json_encode() and json_decode(). Of course, you'll have to implement your own if you're running PHP < 5.2 or if PECL isn't installed. Check the comments of the PHP doc pages for each function for "your own" implementation if needs be.

On the client side, check for an implementation at http://json.org/ (at the bottom)

In theory, it sounds good. Have fun.
Copy linkTweet thisAlerts:
@NyteauthorNov 22.2010 — Thanks svidgen,

I'm currently working on the server side of things first.

So far, I've written some basic script code using json_decode on the server side to parse json data. I upload this data to my MySQL DB for later retrieval usage.

However, when I coded the server portion, I used an HTML textbox form to input my data (which gets converted into JSON for json_decode). I did it this way just to make sure my server coding was ok. From an architectural standpoint, I would like to design this such that the server "listens" in on incoming JSON data and stores it in my jsondata variable to pass to json_decode. Right now, it looks like my C++ program might be coded by sending JSON data using POST method in an HTTP request to the textbox forms. This is kind of messy in my opinion because I don't actually want textbox forms to be visible to users that visit this site (in fact, this site shouldn't be public and should only be available for the application).

I can think of hacks to get around this... like maybe the application (in its HTTP request) can supply a key code and the server script checks this against its DB. If key is valid, then proceed on with parsing the data that is POSTED to the textbox form from the C++ program. If key is not valid (ie. a random user types in the site URL), then a page is displayed saying invalid access or something.

Does the above brainstorming make sense? Is it actually possible to do all of this? Are there better ways to do this? Should I even be using HTTP (or HTTPS) request or should I switch over to socket layers using TCP IP?

PS: I'm using libcURL library for C++, I'm hoping to extend this so that any programmer can develop an interface to my site using any language they want (as long as it supports HTTP request).
Copy linkTweet thisAlerts:
@svidgenNov 22.2010 — The server-side "form processor" doesn't need an actual form anywhere. Your application just needs to know how to arrange the data in a manner that the server knows how to deal with. To process the input as though it were a standard web form, that means using encoded key=value pairs. To make this compatible with your test page, you'll have to mimik a web form.

I would do this though: Have your client application send "raw" JSON as post data. On the server side, suppose the field you're reading the JSON from is [I]json_input[/I]. Now, do something like this:

[code=php]if (isset($_POST['json_input'])) {
$raw_json = $_POST['json_input'];
} else {
$raw_json = file_get_contents("php://input");
}

$data_structure = json_decode($raw_json);

// process data_structure ... [/code]


Though, you may want to spend some time googling "web services", "soap services", "json services", etc. and maybe looking at the code libraries on json.org.
Copy linkTweet thisAlerts:
@NyteauthorNov 22.2010 — Disregard.
×

Success!

Help @Nyte spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...