Search code examples
phpapacheauthenticationdataexplorer

How to display stackexchange data on my site?


Introduction
I am developing my presentation site and I want to include my Stack Overflow profile info/posts/data (eg top tag, score and so on.)

I found data.stackexchange.com to retrieve the desired data but I can't understand how can I show this data in my site.
In github.com I found this prerequisites: https://github.com/StackExchange/StackExchange.DataExplorer#prerequisites which basically says that I must be a .NET programmer to be able to display this data but I am a PHP programmer, I work with Apache MySQL and PHP.

I know that there are lots of PHP MsSQL functions I can use but how can I connect to the Stack Exchange database (I think as a guest/limited user) with which username-password?

Even if this is not too much on-topic here, where can I find more info on how I can display Stack Overflow data on my site?


Solution

  • Even if CONFUS3D' answer is a good solution, any alteration to the User Interface may cause errors in your site.

    I suggest you to use the Stack Exchange API set with which you can retrieve the most of data you probably need.

    Any API query will return a JSON object. I use this PHP class to retrieve this object:

    class ApiReader {
        public function getResponse($url) {
            $cH = curl_init();
            curl_setopt($cH, CURLOPT_URL, $url);
            curl_setopt($cH, CURLOPT_HEADER, 0);
            curl_setopt($cH, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($cH, CURLOPT_TIMEOUT, 30);
            curl_setopt($cH, CURLOPT_USERAGENT, cURL);
            curl_setopt($cH, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($cH, CURLOPT_ENCODING, "gzip");
    
            $result = curl_exec($cH);
    
            if(curl_errno($cH)) {
                $retur = FALSE;
                }
            else {
                $status = curl_getinfo($cH, CURLINFO_HTTP_CODE);
                if($status == 200) {
                    $retur = $result;
                    }
                 else {
                     $retur = FALSE;
                     }
                 }
             curl_close($cH);
             return $retur;
             }
         }
    

    I use this little trick to test the site even if I am off-line.

    In your host, save all the JSON objects you need to use, then declare two vars $UInfo_API containing the API query and $UInfo_Syn which gets the content of the saved JSON object

    $UInfo_API = "api.stackexchange.com/2.2/users/5039442?site=stackoverflow";
    $UInfo_Syn = file_get_contents("yourjsonobject.json");
    

    Then save the result in a variable checking if the getResponse() method has failed or not. After that, you have the data on tap.

    $sear = new ApiReader();
    $uInfo = $sear->getResponse($UInfo_API);
    $uInfo = ($uInfo !== FALSE)? json_decode($uInfo, TRUE): json_decode($UInfo_Syn, TRUE);
    $rep = $uInfo["items"][0]["reputation"];