0

I don't really understand how this API is supposed to work, as I've never worked with JSON before.

The documentation doesn't give any examples, but it says it the end-points in this API support both POST and GET operations, returning JSON.

My question is, I'm not sure exactly how to implement this, let's say I just want to pull all the data into a simple page such as this:

City: Salem

Zip Code: 97302

etc...

I'm not quite sure where to start with this:

POST http://[your RepMan hostname]/api/v1/account/reputation/current.json

GET http://[your RepMan hostname]/api/v1/account/reputation/current.json

Following is a list of arguments for the POST body or GET query string. All values should be properly encoded as per a normal POST body or GET query string.

| Field      | Ordinality | Datatype | Description
| pid        | 1          | string   | This is your partner ID as provided by us to access the API.
| apiKey     | 1          | string   | This is your API Key as provided by use to access the API.
| srid       | ?          | string   | This is the unique RepMan ID for the account. Either this or customerId must be specified.
| customerId | ?          | string   | This is your unique customer id for the account. Either this or srid must be specified.

For a 200 response, you will receive the following JSON content:

{
account : {
    srid        : "DW5SRB36",
    lastName    : "Morimoto",
    pid         : "SRP",
    customerId  : null,
    firstName   : "Masaharu"
},
company : {
    city        : "New York",
    postalZip   : "10011",
    provState   : "NY",
    name        : "Morimoto",
    address     : "88 10th Ave"
},
visibility : {
    found       : 18,
    missing     : 9
},
reviews : {
    1star       : 5,
    4star       : 37,
    3star       : 44,
    5star       : 66,
    2star       : 5
},
competition : {
    Restaurants in New York : {
        Megu    : 1.82,
        Morimoto: 52.95,
        Matsuri : 18.13,
        Buddakan: 0.93,
        Nobu    : 26.17
    }
},
social : {
    checkins            : 5015,
    twitter_followers   : 8154,
    facebook_likes      : 1134
},
mentions : {
    07-09-2011 : {
        positive    : 0,
        neutral     : 0,
        negative    : 0
    },
    07-07-2011: {
        positive    : 2,
        neutral     : 3,
        negative    : 0
    },
    07-05-2011: {
        positive    : 1,
        neutral     : 2,
         negative   : 0
    },
    07-11-2011: {
        positive    : 2,
        neutral     : 2,
        negative    : 0
    },
    07-06-2011: {
        positive    : 5,
        neutral     : 2,
        negative    : 0
    },
    07-10-2011: {
        positive    : 3,
        neutral     : 4,
        negative    : 0
    },
    07-08-2011: {
        positive    : 1,
        neutral     : 5,
        negative    : 0
    }
}
}
}
Community
  • 1
  • 1
Xhynk
  • 13,513
  • 8
  • 32
  • 69

3 Answers3

5

The first thing to try is experiment with a few requests in your web browser. From there, it should be pretty clear what you need to do.

Start with your base URL:

http://[your RepMan hostname]/api/v1/account/reputation/current.json

Obviously, you'll have to plug in your hostname in place of [your RepMan hostname]. From there, let's add a query string. You've seen these before... they come after the question mark ? in the URL, and contain key/value pairs in the form of key1=value1&key2=value2. You have 4 variables to plugin: pid, apiKey, srid, and customerId. Without knowing what this web service does, it's hard to help you know what values to plug in, but here is an example:

http://example.com/api/v1/account/reputation/current.json?pid=12345&apiKey=asdf&srid=34&customerid=98765

Manually build yourself a working URL with the parameters you want, and try it in your browser. Once you have done that, you will see some text structure come back in JSON format. This is text that is compatible with JavaScript parsers, but is in fact separate from JavaScript.

Now, how do you get this going in PHP? A quick way is to use file_get_contents() and json_decode().

$response = file_get_contents('plug your URL in here');
$responseObject = json_decode($response);
print_r($responseObject);

Basically, file_get_contents() will load the data at that URL, and json_decode() will take the text representation of the object and turn it into a real PHP object. From there you could do echo $responseObject->social->checkins or similar.

Now, you should look into using cURL instead of file_get_contents(). It will give you more control over the request, and gives you easier access to response status codes. This will be important when you want to set a time limit on that request later on, or need to handle failures. Also, make sure that you use urlencode() or http_build_query() to build your query string. That way, reserved characters such as spaces will be converted to their encoded form, such as %20.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • 1
    You are doing God's work, thanks! That's exactly what I needed :) – Xhynk May 28 '13 at 18:06
  • 1
    If you prefer to work with your data as an associative array, just change `json_decode($response);` to `json_decode($response,true);` – TecBrat May 28 '13 at 18:12
2

The URL describes the entity:

http://[your RepMan hostname]/api/v1/account/reputation/current.json - The users current reputation.

Using HTTP GET you can retrive(get) data about an entity.

Using HTTP POST you can submit(post) data to the api about an entity (update or create a new entity).

To do this just use curl as explained in many other places like: php: Get url content (json) with cURL

Community
  • 1
  • 1
Putr
  • 969
  • 10
  • 22
0

This looks like a REST based web-service. There are two HTTP methods the API says you can use:

  • GET
  • POST

GET requests are in a URL that can be for example called from a web-browser like this:

http://host//api/v1/account/reputation/current.json?pid={some partner ID}&nextkey={next value}...

The question mark (?) indicates the parameter list start. Ampersand (&) separates parameters. Parameters are key=value formatted.

POST requests use the same URL but instead of parameters you bundle up the content:

http://host//api/v1/account/reputation/current.json

Then set HTTP header "content-type" as "application/json". Then set HTTP header "accept" as "application/json".

Then, using some software (Apache HTTP client, etc), build and POST a JSON formatted message as:

{
  pid: "my partner ID"
  ...
}

References:

http://en.wikipedia.org/wiki/Query_string

http://en.wikipedia.org/wiki/JSON

What is the difference between a HTTP-Get and HTTP-POST and why is HTTP-POST weaker in terms of security

Community
  • 1
  • 1
Darrell Teague
  • 4,132
  • 1
  • 26
  • 38