我正在使用PHP中的curl来跟踪JSON输出
CURL:
$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 here。 U :演示 P :1234
答案 0 :(得分:3)
因此,事实证明,他们提供的模拟帐户存在问题。 错误验证在其文档站点上有以下描述:输入错误。验证错误或超出了允许的最大数量 我创建了一个新帐户,它运行正常,下面是我正在使用的代码:
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进行了硬编码,并且还将用户名更改为我的演示之一。 谢谢。