0

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?

  • So, we are you converting the html to json other than just naming your variable json? – Travis Acton Aug 07 '17 at 20:32
  • Does the HTML response indicate any sort of error? – Patrick Q Aug 07 '17 at 20:38
  • @TravisActon the variable is named JSON is just for my remembrance. The issue is that the HTML is the whole Changelly home page. I need a JSON response – Moazzam Ali Aug 07 '17 at 20:52
  • @PatrickQ nope, it's literally the home page for Changelly, thoughts? – Moazzam Ali Aug 07 '17 at 20:53
  • What is the content of `$response->getBody()`? When I make the request from my Postman client, I get the expected result. See https://stackoverflow.com/a/30536709/1505169 – Patrick Q Aug 07 '17 at 21:06
  • I get this: " Bitcoin Ethereum Monero Ripple Litecoin Dash cryptocurrency exchange | Changelly – Moazzam Ali Aug 07 '17 at 21:36
  • @MoazzamAli did you got any solution for this question ?.. –  Aug 24 '17 at 07:23

1 Answers1

0

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 !!

Mahesh Yadav
  • 2,416
  • 20
  • 23