0

I'm trying to make a facebook app around the game Miscrits: World Adventure The idea is to make a PVP ladder.

In order for this to work I want the app to get the player information automatically. The info is stored here: http://miscrits.brokenbulbstudios.com/player/info (unique to each player) The result that it returns looks like this > http://pennygasm.com/info.htm

there is a bold area and I have separated the info to make it easier to read. I need each of the bold values to be stored in a variable. so that I can do some calculations and create a player rank etc.

The player would visit my app and click "Join the Ladder" then it would get the info from the miscrits url. I'm guessing the player would need to return periodically to update their information? it could auto update when the user visits the page...

Anyway... how do I turn that information into some variables?

1

3 Answers 3

1

Those strings look like valid JSON. In PHP 5.2+ you can use json_decode() to convert a json string into objects/arrays chock full of variables :)

see this:

http://php.net/manual/en/function.json-decode.php

and try this:

// grab the JSON data and stuff it in a variable
$json = file_get_contents("http://miscrits.brokenbulbstudios.com/player/info", true);  
// decode to an associative array using json_decode()
$decoded = json_decode($json, true);
//check out what's in there
print_r($decoded);
Sign up to request clarification or add additional context in comments.

4 Comments

That is returning an associative array with variables in it. You are going to have to have access to the JSON data and you will likely have to pass in the proper query i.e. ?PID=12345 or something specified in their API
ok I'll try running it through a facebook app that should be allowed access. if not im not sure what their API requires.
Ok still within FB it wont work. I think its calling the following URL: miscrits.brokenbulbstudios.com/player/… still doesnt explain why I can get the proper response without entering the session ID, etc...
I'm moving off topic now so I might start a new question
1

The data looks to be JSON, so you should just be able to use a JSON parser to read that data. PHP has one in the form of json_decode, which returns an associative array with the data.

First of all, however, you should familiarize yourself with JSON and its syntax before working with the data contained in it. If you already know JavaScript or Python, you should recognize this as object/array notation and dictionaries/lists, respectively.


An example:

$myvar = json_decode('{
    "hello" : 1
    "there" : [ "a", "b", "c" ]
}');

... would become, in PHP:

$myvar = array(
    "hello" => 1,
    "there" => array("a", "b", "c")
);

... after a json_decode.

Comments

0

The result that is returned is called JSON (http://www.json.org/ or http://en.wikipedia.org/wiki/JSON). Javascript has a function called eval that can be used to evaluate the string into a JSON object. In fact you can use eval to evaluate any string into code. This would require code like the following:

eval('var YourVariableName ='+ReturnedJSONData); 

You could then acces the with code like s:

YourVariableName.JSONPropertyName;

I wouldn't be supprised though if the API your using couldn't return JSONP (http://en.wikipedia.org/wiki/JSON) that could be evaluated automatically into JSON.

2 Comments

I think he wants to do it in PHP, not in JavaScript. Also, eval is dangerous and you should always use JSON.parse.
I agree and I believe you can find the code to do this here github.com/douglascrockford/JSON-js. Thanks for the feedback rfw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.