【问题标题】:POST call to 'Changelly' API returns an HTML page instead of JSON, using PHP使用 PHP 对“Changelly”API 的 POST 调用返回 HTML 页面而不是 JSON
【发布时间】: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-&gt;getBody()的内容是什么?当我从我的邮递员客户提出请求时,我得到了预期的结果。见stackoverflow.com/a/30536709/1505169

标签: php api


【解决方案1】:

请按照“Changelly”本身分享的以下代码。

PHP 中 Changelly API 的 GitHub 代码:

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

我已经按照适用于我的 GitHub 代码尝试了一些方法。 希望它也对你有用。 谢谢!!

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 2018-08-27
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    相关资源
    最近更新 更多