Muhammad Muazzam
Muhammad Muazzam

Reputation: 2800

Json Code Validation Error Curl Post Request

I am getting following JSON output using curl in PHP

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:

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);
}

Output:

"{"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":{}}"

and curl response is "

"{"Code":"Validation"}"

Developer Console:

Malformed JSON Ouput

Note: Values got from NetBeans Variables. When I check output from Json validator it gets invalid only because of double quotes in start and end of output that I think is not bad in php when we assign a json output into variable.

Test Cyclos API here. U: demo P: 1234

Upvotes: 0

Views: 1416

Answers (1)

Saqib Amin
Saqib Amin

Reputation: 1171

So it turned out to be an issue with the demo account they provide. The error validation has this description on their documentation site: Input error. Either a validation error or the maximum allowed items was exceeded I created a new account and it is working fine, below is the code that i am using:

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);

I have hardcoded the URL and also changed the username to my demo one. Thank You.

Upvotes: 3

Related Questions