简体   繁体   中英

POST call to 'Changelly' API returns an HTML page instead of JSON, using PHP

I am trying to implement the 'Changelly' API to my PHP website. I tried to make a POST request to the API to get a JSON file in response. I am using GUZZLE to make HTTP requests.

This is the API guide: https://changelly.com/developers#protocol

This is my code:

<?php

$uri = 'http://api.changelly.com/';

//set api-key and secret
$key = '7d5dd1b8d9c748559cc7b7f31f6adc37'; 
$secret = 'ca7ccb683f1d6baf4c448136f0cdfa47152814dbe339aacbccbb5568fa600fbe';

//API fields and Params
$message = array();
$message['jsonrpc'] = '2.0';
$message['method'] = 'getMinAmount';
$message['params'] = array('from' => 'LTC', 'to' => 'BTC');
$message['id'] = 'test';

//serialize the message body
$data = json_encode($message);

//sign the data with the key's secret with HMAC-SHA512
$sign = hash_hmac('SHA512', $data, $secret);
echo "<br><br>".$sign."<br><br>";

//load the API call variables

//load the composer libraries
require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;

//intialise a guzzle client
$client = new GuzzleHttp\Client();

//create a header
$head = array('headers' => array('api-key' => $key, 'sign' => $sign, 'Content-Type' => 'application/json', 'Accept' => 'application/json'));

//execute API call
$response = $client -> post($uri, $head, $data);

$json = $response->getBody()->getContents();

//dump the result
var_dump($response);
echo '<br><br><br>';
var_dump($json);

?>

'postman' shows the same thing, am I missing something obvious here?

Please follow the below code shared by "Changelly" itself.

GitHub Code for Changelly APIs in PHP:

https://github.com/changelly/changelly-examples/blob/master/php/example.php

I have tried some of the methods by following the GitHub code works for me. Hope it will work for you also. Thanks !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM