繁体   English   中英

Json代码验证错误卷发请求

[英]Json Code Validation Error Curl Post Request

我在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。 U :演示P :1234

因此,事实证明,他们提供的模拟帐户是一个问题。 错误验证在其文档站点上具有以下描述: 输入错误。 验证错误或超出了允许的最大项目数,我创建了一个新帐户,并且工作正常,以下是我使用的代码:

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进行了硬编码,并且还将用户名更改为我的演示之一。 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM