发送电话给' Changelly' API使用PHP返回HTML页面而不是JSON

时间:2017-08-07 20:22:02

标签: php api

我正在努力实施“变革”'我的PHP网站的API。我尝试向API发出POST请求以获取响应的JSON文件。我正在使用GUZZLE发出HTTP请求。

这是API指南:https://changelly.com/developers#protocol

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

请按照以下代码共享&#34; Changelly&#34;本身。

PHP中的Changelly API的GitHub代码:

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

我已经尝试了一些遵循GitHub代码的方法。 希望它也适合你。 谢谢!!