phpcurltodoist

Todoist API - convert output from a string to array


Good evening, I have an account on Todoist. I would use the Todoist API to get all the projects. I wrote the following code:

$url = "https://todoist.com/API/v6/sync";
$post_data = array(
    'token' => "12345678901234567890abcdefabcdef01234567",
    'seq_no' => "0",
    'resource_types' => '["projects"]'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$output = curl_exec($ch);

print_r($output);

curl_close($ch);

The output is a string like this:

{"TempIdMapping":{},"seq_no_global":6201059540,"seq_no":6201059540,"UserId":7179424,"Projects":[{"user_id":7179424,"name":"Project1","color":1,"is_deleted":0,"collapsed":0,"id":165361294,"archived_date":null,"item_order":1,"indent":1,"archived_timestamp":0,"shared":false,"is_archived":0},{"indent":1,"name":"Inbox","user_id":7179424,"color":7,"is_deleted":0,"collapsed":0,"inbox_project":true,"archived_date":null,"item_order":0,"is_archived":0,"archived_timestamp":0,"shared":false,"id":165339673}]}

Is there a way to convert this output into an array? Example:

TempIdMapping => {},

seq_no_global => 6201059540,

seq_no => 6201059540

and so on...


Solution

  • Your output is JSON formated, so you can use json_decode like so

    json_decode($output, true);
    

    The second parameter convert the result to an associative array instead of an stdObject