/**
 * returns an array of detailType structured information for all detailTypes
 *     detailTypes = {"groups" => {"groupIDToIndex":{detailTypeGroupID:index},
 *                                       index:{propName:val,...},...},
 *                    "names":{dtyID:name,...},
 *                    "rectypeUsage" => { dtyID => [rtyID,...], ...},
 *                    'usageCount' => {dtyID:nonzero-count,...},
 *                    "typedefs":{"commonFieldNames":[defDetailTypes Column names],
 *                                "commonNamesToIndex":{"defDetailTypes columnName":index,...},
 *                                dtyID:{"commonFields":[val,....]},
 *                                ...},
 *                    'lookups' => {basetype=>displayName);
 *
 * @global    int $dbID databse ID
 * @param     boolean $useCachedData if true does a lookup for the detailtypes structure in cache
 * @return    object information describing all the detailtypes defined in the database
 * @uses      getDetailTypeColNames()
 * @uses      getColumnNameToIndex()
 * @uses      getRectypeStructureFieldColNames()
 * @uses      getDetailTypeGroups()
 * @uses      getDetailTypeUsageCount()
 * @uses      getDetailTypeDefUsage()
 * @uses      getDtLookups()
 * @uses      getCachedData()
 * @uses      setCachedData()
 */
function getAllDetailTypeStructures($useCachedData = false)
{
    global $dbID;
    $cacheKey = DATABASE . ":AllDetailTypeInfo";
    if ($useCachedData) {
        $dtStructs = getCachedData($cacheKey);
        if ($dtStructs) {
            return $dtStructs;
        }
    }
    $dtG = getDetailTypeGroups();
    /*****DEBUG****/
    //error_log(print_r($dtG,true));
    $dtStructs = array('groups' => $dtG, 'names' => array(), 'rectypeUsage' => getDetailTypeDefUsage(), 'usageCount' => getDetailTypeUsageCount(), 'typedefs' => array('commonFieldNames' => getDetailTypeColNames(), 'fieldNamesToIndex' => getColumnNameToIndex(getDetailTypeColNames())), 'lookups' => getDtLookups());
    $query = "select dtg_ID, dtg_Name, " . join(",", getDetailTypeColNames());
    $query = preg_replace("/dty_ConceptID/", "", $query);
    if ($dbID) {
        //if(trm_OriginatingDBID,concat(cast(trm_OriginatingDBID as char(5)),'-',cast(trm_IDInOriginatingDB as char(5))),'null') as trm_ConceptID
        $query .= " if(dty_OriginatingDBID, concat(cast(dty_OriginatingDBID as char(5)),'-',cast(dty_IDInOriginatingDB as char(5))), concat('{$dbID}-',cast(dty_ID as char(5)))) as dty_ConceptID";
    } else {
        $query .= " if(dty_OriginatingDBID, concat(cast(dty_OriginatingDBID as char(5)),'-',cast(dty_IDInOriginatingDB as char(5))), '') as dty_ConceptID";
    }
    $query .= " from defDetailTypes left join defDetailTypeGroups  on dtg_ID = dty_DetailTypeGroupID" . " order by dtg_Order, dtg_Name, dty_OrderInGroup, dty_Name";
    $res = mysql_query($query);
    $dtStructs['sortedNames'] = mysql__select_assoc('defDetailTypes', 'dty_Name', 'dty_ID', '1 order by dty_Name');
    while ($row = mysql_fetch_row($res)) {
        array_push($dtStructs['groups'][$dtG['groupIDToIndex'][$row[0]]]['allTypes'], $row[2]);
        if ($row[17]) {
            // dty_ShowInLists
            array_push($dtStructs['groups'][$dtG['groupIDToIndex'][$row[0]]]['showTypes'], $row[2]);
        }
        $dtStructs['typedefs'][$row[2]]['commonFields'] = array_slice($row, 2);
        $dtStructs['names'][$row[2]] = $row[3];
    }
    setCachedData($cacheKey, $dtStructs);
    /*****DEBUG****/
    //error_log(print_r($dtStructs['typedefs'],true));
    return $dtStructs;
}
<?php

require_once dirname(__DIR__) . '/utils/cache.class.php';
$user_slug = $_GET['user_slug'];
$cache_id = 'mitti-social-counter';
if ($user_slug == NULL) {
    $user_slug = 'mittistockholm';
}
$url = sprintf('http://instagram.com/%s', $user_slug);
$response = array('followed_by' => '');
if (isCached($user_slug)) {
    $response['followed_by'] = getCachedData($user_slug);
    echo json_encode($response);
    die;
}
$opts = array('http' => array('method' => "GET", 'header' => "Accept-language: en\r\n" . "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0\r\n"));
$context = stream_context_create($opts);
$data = file_get_contents($url, false, $context);
$pattern = "/followed_by\"\\:(\\s+)?\\{\"count\"\\:(.*?)\\}\\,/";
preg_match($pattern, $data, $matches);
if (!empty($matches[2])) {
    $response['followed_by'] = $matches[2];
    setCacheData($matches[2]);
} else {
    $response['followed_by'] = 'Error occurred during a request.';
}
echo json_encode($response);
function setCacheData($data)
{
    global $user_slug;
    $cache = get_cache();