0

I need help.

I'm trying connect a website by API, but it requests authentication. They show me example by C# but I want to change into PHP.

I use file_get_contents for getting data. How I have to send them in the HTML Header of my request with their name. How I make signature.

There is explaining of website. in sum can you convert C# to PHP please :)


API Authentication:

All API calls related to a user account require authentication.

You need to provide 3 parameters to authenticate a request:

"X-PCK": API key
"X-Stamp": Nonce
"X-Signature": Signature
API key

You can create the API key from the Account > API Access page in your exchange account.

Nonce:

Nonce is a regular integer number. It must be increasing with every request you make.

A common practice is to use unix time for that parameter.

Signature:

Signature is a HMAC-SHA256 encoded message. The HMAC-SHA256 code must be generated using a private key that contains a timestamp and your API key

Example (C#):

string message = yourAPIKey + unixTimeStamp;
using (HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String( yourPrivateKey ))){

   byte[] signatureBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(message));

   string X-Signature = Convert.ToBase64String(signatureBytes));
}

After creating the parameters, you have to send them in the HTML Header of your request with their name

Example (C#):

client.DefaultRequestHeaders.Add("X-PCK", yourAPIKey);
client.DefaultRequestHeaders.Add("X-Stamp", stamp.ToString());
client.DefaultRequestHeaders.Add("X-Signature", signature);
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
ahmedov
  • 11
  • 2

2 Answers2

0

I think that you must use curl instead of file_get_contents.

Exemple :

// API Url
$url = 'http://';
// Put the value of parameters
$yourAPIKey = "";
$stamp = "";
$signature = "";

$ch = curl_init($url);
curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER  => array('X-PCK: '. $yourAPIKey, 'X-Stamp: '. $stamp, 'X-Signature: '. $signature),
    CURLOPT_RETURNTRANSFER  =>true,
    CURLOPT_VERBOSE     => 1
));
$response = curl_exec($ch);
curl_close($ch);
// echo response output
echo $response;
Yassine
  • 26
  • 3
0

Use curl. Take an example like this: http://php.net/manual/de/function.curl-init.php

For the headers use something like that

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    '"X-PCK": API key',
    '"X-Stamp": Nonce',
    '"X-Signature": Signature'
));
csskevin
  • 383
  • 2
  • 14
  • how I create signature – ahmedov May 20 '17 at 21:59
  • Search through the PHP Manual and Google: For HMACSHA256 in C# you will need http://php.net/manual/de/function.hash-hmac.php. For Convert.FromBase64String is http://php.net/manual/de/function.base64-decode.php. Equal to ComputeHash(encoding.GetBytes(message)); in php: http://stackoverflow.com/questions/12804231/c-sharp-equivalent-to-hash-hmac-in-php. Convert.ToBase64String is http://php.net/manual/de/function.base64-encode.php – csskevin May 20 '17 at 22:23