Skip to content

Instantly share code, notes, and snippets.

@henninghorn
Created March 24, 2012 13:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henninghorn/2182941 to your computer and use it in GitHub Desktop.
Save henninghorn/2182941 to your computer and use it in GitHub Desktop.
Prints out instruments in C20
<?php
class Nordnet {
public $base_url;
public function __construct($username, $password, $service = 'NEXTAPI', $base_url = 'api.test.nordnet.se/next', $api_version = '1')
{
// Step 1: First Base64-encode the username, password and timestamp (UNIX timestamp in milliseconds) and combine them with the character ‘:’.
$login_string = base64_encode($username) . ':' . base64_encode($password) . ':' . base64_encode(microtime());
// Step 2: Use the public key for the application and encrypt the string.
// Loads the public key - available at: https://api.test.nordnet.se/projects/api/files | Note (use the .pem file)
$public_key = openssl_get_publickey(file_get_contents('publickey.pem'));
// This part encrypts our login string with the public key and return the encrypted string to the $login variable
$rsa_encrypt = openssl_public_encrypt($login_string, $login, $public_key);
// Step 3: Base64 encode the encrypted string.
$auth_string = base64_encode($login);
// Now we wrap up our fields in array
$fields = array(
'auth' => $auth_string,
'service' => $service
);
// We then build a URL-encoded query string
$post_data = http_build_query($fields);
// Finally we post our login to https://BASE_URL/API_VERSION/login
$login_result = $this->_rest_call('POST', 'https://'.$base_url.'/'.$api_version.'/login', $post_data);
$session_key = $login_result->session_key;
$this->base_url = "https://{$session_key}:{$session_key}@{$base_url}/{$api_version}";
}
private function _rest_call($method, $call, $data = array())
{
if ( ! $this->base_url)
{
$url = $call;
}
else
{
$url = $this->base_url . $call;
if ($method == 'GET')
{
$url .= '?' . http_build_query($data);
}
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
switch ($method) {
case 'POST':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
break;
case 'GET':
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
break;
}
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
public function get_session_key()
{
return $this->system->session_key;
}
public function get_lists()
{
return $this->_rest_call('GET', '/lists');
}
public function get_list_items($list_id)
{
return $this->_rest_call('GET', '/lists/'.$list_id);
}
public function get_instruments($list)
{
return $this->_rest_call('GET', '/instruments', $list);
}
public function get_lists_intruments($list_id)
{
$items = $this->get_list_items($list_id);
$list = '';
foreach ($items as $item)
{
$list .= $item->marketID . ',' . $item->identifier . ';';
}
$list = substr($list, 0, -1);
$instruments = $this->get_instruments(array('list' => $list));
return $instruments;
}
}
$START = microtime(true);
$nordnet = new Nordnet('USERNAME', 'PASSWORD');
// 29 is the id for OMX C20 in Nordnet's systems
$c20 = $nordnet->get_lists_intruments(29);
print_r($c20);
echo 'RUNTIME: ' . number_format(microtime(true) - $START, 4, ',', '.') . ' sek.';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment