首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Json代码验证错误Curl Post请求

Json代码验证错误Curl Post请求
EN

Stack Overflow用户
提问于 2019-02-25 12:32:01
回答 1查看 1.2K关注 0票数 0

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

卷曲:

代码语言:javascript
复制
$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);

功能:

代码语言:javascript
复制
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);
}

输出:

代码语言:javascript
复制
"{"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中并不坏。

测试循环API 这里U__:演示P__:1234

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-07 08:15:15

所以这是他们提供的演示帐户的一个问题。错误验证在他们的文档站点上有这样的描述:输入错误。要么是验证错误,要么是最大允许项超出了,我创建了一个新帐户,它运行良好,下面是我正在使用的代码:

代码语言:javascript
复制
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,并将用户名更改为我的演示用户名。谢谢。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54866279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档