Если указать значение $key = 'Здесь ключ API'; и $secret = 'Здесь секретный ключ'; непосредственно в файле, то все работает.
Но я решил эти данные хранить в Mysql в зашифрованном виде. Все нормально шифруется, расшифровывается и выводится echo $strapi_key; echo $strapi_secret;
Но в таком вот виде в ответ получаю ошибку "api key not specified" или "invalid"
Заранее благодарю за помощь.
CODE (htmlphp):
скопировать код в буфер обмена
скопировать код в буфер обмена
- $query = $db->query("SELECT * FROM test WHERE id='1'");
- $row = $query->fetch_assoc();
- function btce_query($method, array $req = array()) {
- $codeapi_key = $row['api_key'];
- $strapi_key = __decode(hexToStr($codeapi_key),'$k');
- $codeapi_secret = $row['api_secret'];
- $strapi_secret = __decode(hexToStr($codeapi_secret),'$k');
- $key = $strapi_key;
- $secret = $strapi_secret;
- $req['method'] = $method;
- $mt = explode(' ', microtime());
- $req['nonce'] = $mt[1];
- // generate the POST data string
- $post_data = http_build_query($req, '', '&');
- $sign = hash_hmac('sha512', $post_data, $secret);
- // generate the extra headers
- $headers = array(
- 'Sign: '.$sign,
- 'Key: '.$key,
- );
- // our curl handle (initialize if required)
- static $ch = null;
- if (is_null($ch)) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; WEX PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
- }
- curl_setopt($ch, CURLOPT_URL, 'https://site.com/api/');
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
- // run the query
- $res = curl_exec($ch);
- if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
- $dec = json_decode($res, true);
- if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
- return $dec;
- }