【发布时间】:2018-01-15 05:50:54
【问题描述】:
我正在尝试在我的 PHP 网站上实现“Changelly”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?
【问题讨论】:
-
那么,除了将变量命名为 json 之外,我们还在将 html 转换为 json 吗?
-
HTML 响应是否指示任何类型的错误?
-
@TravisActon 变量名为 JSON 只是为了纪念。问题是 HTML 是整个 Changelly 主页。我需要 JSON 响应
-
@PatrickQ 不,这实际上是 Changelly 的主页,你想吗?
-
$response->getBody()的内容是什么?当我从我的邮递员客户提出请求时,我得到了预期的结果。见stackoverflow.com/a/30536709/1505169