S
S
ssdk2017-12-06 12:33:06
PHP
ssdk, 2017-12-06 12:33:06

Why can't I test api methods that require verification on localhost?

Hello!
I'm trying on a local server (WAMP) to test the operation of api methods that require verification (I tried to run it from a real site on a hosting - everything works there).
I am testing this exchange script https://www.livecoin.net/api/examples#php
The second example is on the link, where $url = " https://api.livecoin.net/exchange/client_orders ";
I am posting this code here

$url = "https://api.livecoin.net/exchange/client_orders";
$apiKey = "gJx7Wa7qXkPtmTAaK3ADCtr6m5rCYYMy";
$secretKey = "8eLps29wsXszNyEhOl9w8dxsOsM2lTzg";
 
$params = array(
    'currencyPair'=> 'BTC/USD'
);
 
ksort($params);
$fields = http_build_query($params, '', '&');
$signature = strtoupper(hash_hmac('sha256', $fields, $secretKey));
 
$headers = array(
    "Api-Key: $apiKey",
    "Sign: $signature"
);
 
$ch = curl_init($url."?".$fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 
if ($statusCode!=200) {
    //dump info:
    echo "Status code: $statusCode, response: $response";
    //throw new Exception('Can not execute the query!');
}
 
var_dump(json_decode($response));

In fact, if you run it from any site on the hosting, it will either issue a json array with different parameters, or, if $apiKey and $secretKey are not set correctly, then the same json array with an error
Status code: 400, response: {"success":false,"errorCode":1,"errorMessage":"For authorization must specify the api kei and signature"}

When I start from localhost, it gives out with both correct and incorrect $apiKey and $secretKey always only one answer
Status code: 0, response: null
Googled and found that this usually happens, that methods requiring verification will not work, that you need to create your own virtual hosting to work .
Created this - api.dev
Added to hosts
127.0.0.1 api.dev
127.0.0.1 www.api.dev
Changed
the httpd.conf and httpd-vhosts.conf files
In the httpd.conf line Include conf/extra/httpd-vhosts.conf should be uncommented.
Added the following code to httpd-vhosts.conf
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "c:/wamp64/www"
    ServerName localhost
    ErrorLog "logs/localhost-error.log"
    CustomLog "logs/localhost-access.log" common
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "c:/wamp64/www/api.dev"
    ServerName api.dev
    <directory "c:/wamp64/www/api.dev">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Deny from all
        Allow from 127.0.0.1
    </directory>
</VirtualHost>

Wirth Host created by api.dev opens and works, but the answer is still the same
Status code: 0, response: null
At the same time, all methods for public melons that do not require keys work well on the local https://www.livecoin.net/api/userdata
Tell me where else to dig, what can you still try?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
ssdk, 2017-12-06
@ssdk

The answer came from the support service - I thought they were ignoring it.
Now everything is working!
On the local host, it can be difficult to validate the certificate.
After the line
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
add the line
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question