【问题标题】:Json Code Validation Error Curl Post RequestJson 代码验证错误 Curl Post 请求
【发布时间】:2019-02-25 12:32:01
【问题描述】:

我在 PHP 中使用 curl 获得以下 JSON 输出

卷曲:

$request = curl_init("{$config['root']}/api/tickets");
    curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($request, CURLOPT_POST, true);
    curl_setopt($request, CURLOPT_POSTFIELDS, json_encode($body));
    curl_setopt($request, CURLOPT_TIMEOUT, 30);
    add_headers($request);    
    $response = curl_exec($request);

功能:

function add_headers($request) {
    global $config;
    $headers = array('Content-Type: application/json');
    if (empty($config['accessClient'])) {
        curl_setopt($request, CURLOPT_USERPWD, "{$config['user']}:{$config['password']}");
    } else {
        array_push($headers, "Access-Client-Token: {$config['accessClient']}");
    }
    curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
}

输出:

"{"amount":"100","description":"A ticket of 100.","payer":null,"successUrl":"http:\/\/localhost\/wordpress5\/ticket-confirmed.php","successWebhook":"http:\/\/localhost\/wordpress5\/ticket-confirmed-webhook.php","cancelUrl":"http:\/\/localhost\/wordpress5\/shop","orderId":"OID-1","expiresAfter":{"amount":1,"field":"hours"},"customValues":{}}"

卷曲响应是“

"{"Code":"Validation"}"

开发者控制台:

格式错误的 JSON 输出

注意:值来自 NetBeans 变量。 当我检查 Json 验证器的输出时,它变得无效只是因为输出开始和结束中的双引号,当我们将 json 输出分配给变量时,我认为这在 php 中还不错。

测试 Cyclos API hereU:演示P:1234

【问题讨论】:

  • 它是第 3 部分 API?
  • @Vel 是的,它的环
  • @Vel 你能帮我吗
  • 您好,我没有 cyclos 帐户。所以我无法检查
  • 无论是你的 curl 代码还是你的 add_headers 函数都不应该产生任何输出,所以显然有些事情你没有告诉我们。 (例如,也许您在 $response = curl_exec($request); 之后有一个 echo $response;,但您没有向我们展示?)

标签: php json wordpress curl netbeans


【解决方案1】:

结果证明他们提供的模拟账户存在问题。 错误验证在他们的文档站点上有这样的描述:输入错误。验证错误或超出了允许的最大项目数 我创建了一个新帐户,它工作正常,下面是我正在使用的代码:

function add_headers($request) {
    global $config;
    $headers = array('Content-Type: application/json');
    if (true || empty($config['accessClient'])) {
        curl_setopt($request, CURLOPT_USERPWD, "geeky:1234");
    } else {
        array_push($headers, "Access-Client-Token: {$config['accessClient']}");
    }
    curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
}

$body = '{"amount":"100","description":"A ticket of 100.","payer":null,"successUrl":"http:\/\/localhost\/wordpress5\/ticket-confirmed.php","successWebhook":"http:\/\/localhost\/wordpress5\/ticket-confirmed-webhook.php","cancelUrl":"http:\/\/localhost\/wordpress5\/shop","orderId":"OID-1","expiresAfter":{"amount":1,"field":"hours"},"customValues":{}}';

$request = curl_init("https://demo.cyclos.org/api/tickets");
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_POSTFIELDS, $body);
curl_setopt($request, CURLOPT_TIMEOUT, 30);
add_headers($request);    
$response = curl_exec($request);
$response = json_decode($response);
var_dump($response);

我已对 URL 进行了硬编码,并将用户名更改为我的演示用户名。 谢谢。

【讨论】:

  • 你在上面的Api链接测试过吗
  • 我已经测试了上面的代码并返回一个长的 JSON 对象。您可以将上面的代码复制粘贴到一个空的 PHP 文件中并运行它来查看结果。
  • 它工作,你能告诉我需要用户配置或用户帐户有什么问题,因为我必须用我自己的 Cycls 服务器来做
  • 他们提供的默认demo用户有限制,因此用户不能创建很多创建请求。这就是您看到错误的原因。如果您有自定义安装,那么这将不是问题。如果您想使用他们的演示站点,请注册一个新用户,您仍然不会遇到任何问题。
  • @SaqibAmin,再次+1。对不起,我的手机昨天表现很奇怪。无论如何,了解问题的实际问题是件好事。干杯!
猜你喜欢
  • 2015-09-29
  • 2012-04-22
  • 1970-01-01
  • 1970-01-01
  • 2020-03-18
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多