showDebug = true; $this->debug("init OSRest - lan: $lan - st: $st"); $this->st = $st; $this->lan = $lan; if ($baseUrl) { $this->baseUrl = $baseUrl . '/opensocial/social/rest'; } else { $this->baseUrl = 'http://' . $this->lan . '.api.netlog.com/opensocial/social/rest'; //when testing on beta site add beta. in front of url } $this->viewer = $this->getViewer(); $this->debug("Viewer: ".$this->viewer['nickname']); $this->uid = '@me'; //$this->viewer['id']; } private function debug($string) { if ($this->showDebug) { if (is_array($string)) { $string = '
' . print_r($string, true) . '
'; } echo '
'.$string . '
'; } } public function sendGETRequest($destURL, $method = "GET") { $this->debug('sending '.$method.' request to '.$destURL.'...'); $handler = curl_init(); curl_setopt($handler, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($handler, CURLOPT_URL, $destURL); curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($handler); curl_close($handler); $this->debug("Result:" . $result); return $result; } private function sendPOSTRequest($destURL, $paramstring = false) { $this->debug('sending post request to '.$destURL.'...
Params received: ' . print_r(urldecode($paramstring),true)); $handler = curl_init(); curl_setopt($handler, CURLOPT_URL, $destURL); curl_setopt($handler, CURLOPT_POST, 1); curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1); curl_setopt($handler, CURLOPT_POST,4); curl_setopt($handler, CURLINFO_HEADER_OUT, true); //needed to be able to do the geT_info() with that param if ($paramstring) { curl_setopt($handler, CURLOPT_POSTFIELDS, $paramstring); } $result = curl_exec($handler); //more info on the request sent: $info = curl_getinfo($handler, CURLINFO_HEADER_OUT); $this->debug('header sent:'); $this->debug('
' . $info . '
'); curl_close($handler); $this->debug('result:'); $this->debug('
' . $result . '
'); return $result; } public function getQuickViewer() { $this->debug( 'getting viewer info...'); $specificPart = '/people/@me/@self'; $params = array( 'format' => 'json', 'st' => $this->st, 'fields' => '@all' ); $completeURL = $this->baseUrl . $specificPart . '?'. http_build_query($params); $rawResponse = $this->sendGETRequest($completeURL); $response = json_decode(stripslashes($rawResponse), true); $this->debug('
'.print_r($rawResponse,true).'
'); $this->debug('Response as array:
'.print_r($response,true).'
'); return $response['entry']; } public function getViewer() { $specificPart = '/people/@me/@self'; $fields = array('nickname, location, thumbnailUrl, languages_spoken'); $params = array( 'format' => 'json', 'st' => $this->st, 'fields' => '@all', ); $this->debug( 'getting viewer info including these fields:' . print_r($fields,true) ); $completeURL = $this->baseUrl . $specificPart . '?'. http_build_query($params); $rawResponse = $this->sendGETRequest($completeURL); $response = json_decode($rawResponse, true); //stripslashes() call GIVES PROBLEMS WITH CHARACTERS LIKE @ IN USER STATUS!!! (not doing stripslashes($rawResponse) makes it work again but will cause other quirky behavior perhaps) $this->debug('
'.print_r($response['entry'],true).'
'); return $response['entry']; } public function sendNotification($body='',$id) { $this->debug('posting notification...'); if (!is_array($id)) { $id = Array($id); } //previously $specificPart = '/messages/@me/@self?st=' . $this->st . '&format=json'; which worked fine except multiline fix $specificPart = '/messages/@me/outbox?groupId=@self&st=' . $this->st . '&format=json'; //@ -> %40? $message = array( 'body' => $body, 'title' => 'will be ignored', //required parameter but will be ignored (quirky, but conforms 'recipients' => $id, 'type' => 'NOTIFICATION', //always needs type=notification param ); $completeURL = $this->baseUrl . $specificPart; $paramString = json_encode($message); $rawResponse = $this->sendPOSTRequest($completeURL, $paramString); return $rawResponse; } public function getFriendsOfUser($userId, $count = 75, $startIndex = 0) { $this->debug("getting friends of user # $userId ... (count: $count, index: $startIndex)"); $specificPart = '/people/'. $userId.'/@friends'; $params = array( 'format' => 'json', 'st' => $this->st, 'count' =>$count, 'startIndex' => $startIndex, 'fields' => 'nickname,thumbnailUrl' //,photos' ); $completeURL = $this->baseUrl . $specificPart . '?'. http_build_query($params); $rawResponse = $this->sendGETRequest($completeURL); $response = json_decode(stripslashes($rawResponse), true); $this->debug('
'.print_r($response, true).'
'); return $response['entry']; } public function getViewerFriends($count = 10, $startIndex=0) { $this->debug('getting viewer friends...'); return $this->getFriendsOfUser($this->uid, $count, $startIndex); } //Netlog returns at most 75 friends public function getAllViewerFriends() { $this->debug("getting all viewer friends..."); $count = 1; $index = 0; $countPerFetch = 75; $allFriends = $this->getViewerFriends($countPerFetch, $index); $lastCount = count($allFriends); $currentIteration = 0; $maxIterations = 10; while ($lastCount > 0) { if ($currentIteration >= $maxIterations) { $this->debug("Reached max of $maxIterations iterations! Stopping loop..."); break; } $currentBatch = $this->getViewerFriends($countPerFetch, $index); $allFriends = array_merge($allFriends, $currentBatch); $index += $countPerFetch; $lastCount = count($currentBatch); $currentIteration ++; $this->debug("Last count: $lastCount ..."); } return $allFriends; } public function postViewerActivity($title, $body = '') { $this->debug('posting activity...'); $specificPart = '/activities/'. $this->uid.'/@self/@app?st=' . $this->st . '&format=json'; $activity = array( 'title' => $title, 'body' => $body, //note: this can contain HTML like , and
tags ); $params = $activity; $completeURL = $this->baseUrl . $specificPart; $paramString = json_encode($params); $paramString = fix_unicode($paramString); $rawResponse = $this->sendPOSTRequest($completeURL, $paramString); $this->debug('response:' . $rawResponse); $response = json_decode(stripslashes($rawResponse), true); return $response['entry']; } public function getViewerFriendsActivities() { $this->debug('getting activities of viewer friends...'); $specificPart = '/activities/'. $this->uid.'/@self'; $params = array( 'format' => 'json', 'st' => $this->st ); $completeURL = $this->baseUrl . $specificPart . '?'. http_build_query($params); $rawResponse = $this->sendGETRequest($completeURL); $this->debug('response:' . $rawResponse); $response = json_decode(stripslashes($rawResponse), true); return $response['entry']; } public function getViewerPhotos($count = 20, $startindex = 5) { $this->debug('getting viewer photos...'); $specificPart = '/mediaitems/'. $this->uid.'/@self'; $params = array( 'format' => 'json', 'st' => $this->st, 'albumId' => 0, //gets all of them 'count' => $count, 'startIndex' => $startindex ); $completeURL = $this->baseUrl . $specificPart . '?'. http_build_query($params); $rawResponse = $this->sendGETRequest($completeURL); $response = json_decode(stripslashes($rawResponse), true); $this->debug($response); return $response['entry']; } // argument would be array('photoid' => array('userid1', 'userid2' ...), 'photoid2' => array(... )) public function tagPeopleInPhoto($photoIdsWithUserIds) { $this->debug('tagging people in photo ... NOT IMPLEMENTED YET'); return false; } //does both insert and update (right?) public function setViewerAppData($key, $value) { $this->debug("setting persistent data - key : $key - value: $value ... "); $specificPart = '/appdata/'. $this->uid.'/@self/@app'; $appData = array( $key => $value ); $params = array( 'userId' => $this->uid, 'format' => 'json', 'st' => $this->st ); $paramString = json_encode($appData); $paramString = fix_unicode($paramString); $completeURL = $this->baseUrl . $specificPart . '?'. http_build_query($params); $rawResponse = $this->sendPOSTRequest($completeURL, $paramString); $this->debug('appdata set response:
'.print_r($rawResponse,true).'
'); return $rawResponse; } public function removeViewerAppData($key) { $this->debug("removing persistent data - key : $key ... "); $specificPart = '/appdata/'. $this->uid.'/@self/@app'; $params = array( 'userId' => $this->uid, 'format' => 'json', 'st' => $this->st, 'fields' => $key ); $postParams = json_encode($appData); $completeURL = $this->baseUrl . $specificPart . '?'. http_build_query($params); $rawResponse = $this->sendGETRequest($completeURL, "DELETE"); $this->debug('appdata remove response:
'.print_r($rawResponse,true).'
'); return $rawResponse; } //just one key or all possible now, if you want more keys specify them as fields=key1,key2 public function getViewerAppData($key = '*') { $this->debug("getting persistent data - key : $key ... "); $specificPart = '/appdata/'. $this->uid.'/@self/@app'; $appData = array( 'key' => $key ); $params = array( 'userId' => $this->uid, 'format' => 'json', 'st' => $this->st, 'fields' => $key ); $completeURL = $this->baseUrl . $specificPart . '?'. http_build_query($params); $rawResponse = $this->sendGETRequest($completeURL); $response = json_decode(stripslashes($rawResponse), true); $this->debug('Response:
'.print_r($response,true).'
'); return $response; } } ?>