5

I am trying to get the amount of reputation that a user has. I have established that I need to use this url: https://api.stackexchange.com/2.1/users/1272330?order=desc&sort=reputation&site=stackoverflow, but where do I go from there?

I have a knowledge of PHP, HTML, JavaScript and a bit of jQuery.

I have looked around extensively and cannot find out how to handle the response (or get one) from a script.

0

2 Answers 2

4

"I have established that I need to use this url: https://api.stackexchange.com/2.1/users/1272330?order=desc&sort=reputation&site=stackoverflow ..."

That is the correct path to use, but the order and sort query string parameters are unnecessary since you will always be receiving only one item in the response.

"I have a knowledge of PHP, HTML, JavaScript and a bit of jQuery."

The Stack Exchange API can be accessed using both PHP and JavaScript. You will need to decide whether API requests should be made by the client or the server.

"... how to handle the response (or get one) from a script."

As mentioned in my previous sentence, it really depends on which language you will be using - the approach used in each case will differ greatly. In PHP, requests against the API will likely be synchronous and use PHP's cURL functions. In JavaScript, however, your requests will most likely be asynchronous and must be made with XMLHttpRequest (or JSONP if your browser does not support CORS).

In both cases, the actual response will be in JSON format.

Instead of interacting directly with the API, I would highly recommend using one of the existing libraries / wrappers. I have written a library for PHP that is compatible with version 2.0 of the API: Stack.PHP. I wrote a brief tutorial describing the usage of the library.

Things aren't so easy in JavaScript - the only library that I'm aware of supporting v2.0 of the API is Stack.JS, which I also wrote. However, it is far from complete and not very well tested. Your best bet is probably to use the jQuery.ajax() method of the jQuery library.


Edit: as requested, here is an example demonstrating how to fetch a user's reputation withs Stack.PHP:

<?php

require_once 'stackphp/api.php';

$stackoverflow = API::Site('stackoverflow');
$user = $stackoverflow->Users(1272330)->Exec()->Fetch();

echo $response['reputation'];

?>

The example above will output the reputation of user #1272330 on Stack Overflow.

4
2

I am trying to get the amount of reputation that a user has

how to handle the response (or get one) from a script

You can do this with a Bash command that you can then embed in a script. Here's how:

1. Look up your user ID

Assuming you want to get the reputation on Stack Overflow, go to your account and look up your user ID in the URL:

https://stackoverflow.com/users/123456/your-name

In this example, the ID is 123456.

2. Get user info

Make your first API call with curl:

curl --compressed https://api.stackexchange.com/2.2/users/123456/?site=stackoverflow

This will print a single line of JSON data about you on Stack Overflow, including your reputation.

The --compressed flag makes curl uncompress the response which is compressed using gzip.

We can format the data with jq to make it easier to read:

curl --compressed https://api.stackexchange.com/2.2/users/123456/?site=stackoverflow | jq

Given that you have jq installed, this will produce much nicer output:

{
  "items": [
    {
      "badge_counts": {
        "bronze": 122,
        "silver": 85,
        "gold": 11
      },
      "account_id": 999999,
      "is_employee": false,
      "last_modified_date": 1560547206,
      "last_access_date": 1562693872,
      "reputation_change_year": 2168,
      "reputation_change_quarter": 90,
      "reputation_change_month": 90,
      "reputation_change_week": 30,
      "reputation_change_day": 0,
      "reputation": 15343,
      "creation_date": 1306744511,
      "user_type": "registered",
      "user_id": 123456,
      "accept_rate": 85,
      "location": "Austria",
      "website_url": "https://www.you.com",
      "link": "https://stackoverflow.com/users/123456/your-name",
      "profile_image": "https://i.stack.imgur.com/SyKPw.jpg?s=128&g=1",
      "display_name": "Your Name"
    }
  ],
  "has_more": false,
  "quota_max": 300,
  "quota_remaining": 268
}

3. Filter the data

Alright, now that we have the data, how do we get at just the user's reputation?

Using jq's filters:

curl --compressed https://api.stackexchange.com/2.2/users/123456/?site=stackoverflow | jq '.items[0].reputation'

This will print the reputation of the user with ID 123456:

15343

Not the answer you're looking for? Browse other questions tagged .