Introduction

The PHP library is a client library written by NewHaze. You can make API calls using the library, and the details of each API call are found on each API method page.

Download

Go to the download page!

How it works

Declaring a connection

To connect to the API through the PHP library, you should start like this:

<?php
$consumer_key
='';
$consumer_secret='';
$newhaze = new NewHaze($consumer_key,$consumer_secret);
?>

This checks to see if the user is logged in, if they are, then the relevant session data is collected ready for API calls, otherwise they aren't. To find out whether the user is logged in or not, you can use $newhaze->isLoggedIn(); and $newhaze->getLoggedInUser();.

Making an API call

Making an API call is really easy! Once you've declared the connection, simply call:

$response=$newhaze->api->method_name($params);

The method name needs the dots turned into underscores. For example games.get becomes games_get: $response=$newhaze->api->games_get($params);

More information about each of these can be found on the API method page in this documentation. Consumer keys and session keys are automatically included where they're needed.

Making an OpenAPI call

Making a call to the OpenAPI is even easier. Once again, declare a connection and then call:

$response=$newhaze->openapi->call($url);

The $url parameter there can be changed to something like /games?limit=10 to load information about that game. Once again, access tokens are automatically added.

Logging a user in

<?php
require_once('newhaze_api.php'); //include the NewHaze API file (you might need to change this path!)
$consumer_key='#####'; //add your consumer key here
$consumer_secret='#####'; // and your consumer secret here!
$permissions=array('manage_favorites','manage_comments'); //List all the permissions you require for your site
$ending_url='http://www.example.com'; //take this to any page on your site where you have the NewHaze API running

$newhaze=new NewHaze($consumer_key,$consumer_secret);
$url=$newhaze->getLoginUrl($ending_url,$permissions);
header("Location: $url");
?>
See: getLoginUrl()

<?php
if($newhaze->isLoggedIn()) {
   
$user_id=$newhaze->getLoggedInUser();
   
$userinfo=$newhaze->api->users_getInfo($user_id);
   echo
"Hello {$userinfo['first_name']}! Nice picture!<br />
   <img src=\"{$userinfo['pic_square']}\" /><br />
   Want to know what we know about you?"
;
   
print_r($userinfo);
}else{
   
//display login button
}
?>
See: isLoggedIn(), getLoggedInUser(), users.getInfo

<?php
require_once('newhaze_api.php'); //change to your path again!
$consumer_key='#####'; //add your consumer key here
$consumer_secret='#####'; // and your consumer secret here!
$ending_url='http://www.example.com'; //to your homepage, maybe?

$newhaze=new NewHaze($consumer_key,$consumer_secret);
$newhaze->logUserOut();
header("Location: ".$ending_url);
?>
See: logUserOut()

Built in functions

Some additional functions are built into the PHP library. These help when building a NewHaze website. You don't need to use these, but it may make your life considerably easier if you do!

authorize_url

<?php
$newhaze
->authorize_url($token);
?>

If you supply this function with an authorization token, as returned from auth.requestToken, then it returns the authorization URL to use.

auth_setCurrentUser

<?php
$newhaze
->auth_setCurrentUser($access_token,$access_token_secret);
?>

This can be used to log in the current user. The access token and access token secret are returned from auth.requestAccessToken API call. Returns true if the user was logged in successfully. The PHP library calls this when you initialize the NewHaze class, so doesn't need to be called again.

getLoggedInUser

<?php
$newhaze
->getLoggedInUser();
?>

This gets the ID of the current logged in user. You can use this to make other API calls.

isLoggedIn

<?php
$newhaze
->isLoggedIn();
?>

Returns true if a user is logged in, else returns false.

getLoginUrl

<?php
$permissions
=array('manage_favorites','manage_friends');
$newhaze->getLoginUrl($callback_url,$permissions);
?>

This function returns the URL to redirect the user to if you want to log them in.

logUserOut

<?php
$newhaze
->logUserOut();
?>

This function destroys all the session cookies and wipes the access keys from within the library. This logs the user out of your site completely, provided you use the predefined functions stated here and don't set your own user cookies.


© 2012 NewHaze   Icons by Pixel Mixer   Help