AnsweredAssumed Answered

Canvas API PHP cURL PUT Not Updating Record, How do I Update Changes in PHP cURL?

Question asked by Larry Robertson on May 23, 2019
Latest reply on May 26, 2019 by Larry Robertson

To start I have already looked at a similar post at quiz submission update api - missing key ":quiz_submissions" but it did not help.

 

I am trying to update course data of an existing record using cURL - PUT in PHP.  The PHP code (shown after input data definitions below) returns a web page from the result of curl_exec (print_r($result);), here is a screenshot:

 

Web Page Returned from curl_exec($curl) in PHP after PUT request passing updated date in JSON

 

ARRAY OF UPDATE DATA RECEIVED VIA AJAX FROM JS TO PHP ($newCanvasArray)

 

Array
(
[id] => 34400
[name] => Know Your Rights_Stratford
[account_id] => 3
[uuid] => KsGMzFna4CSo4VXQOTlJxkpitPCViVzqmUtB4APT
[start_at] => 2019-02-04T16:07:00Z
[grading_standard_id] =>
[is_public] =>
[created_at] => 2019-02-15T12:38:03Z
[course_code] => Know Your Rights_STRAT
[default_view] => wiki
[root_account_id] => 1
[enrollment_term_id] => 1
[license] => private
[end_at] => 2019-05-30T00:00:00Z
[public_syllabus] =>
[public_syllabus_to_auth] =>
[storage_quota_mb] => 10000
[is_public_to_auth_users] =>
[hide_final_grades] =>
[apply_assignment_group_weights] =>
[calendar] => stdClass Object
(
[ics] => https://test.instructure.com/feeds/calendars/course_KsGMzFna4CSo4VXQOTlJxkpitPCViVzqmUtB4APT.ics
)

[time_zone] => America/New_York
[blueprint] =>
[sis_course_id] =>
[sis_import_id] => 4702
[integration_id] =>
[enrollments] => Array
(
)

[workflow_state] => available
[restrict_enrollments_to_course_dates] =>
)

 

THE ABOVE UPDATE DATA AFTER JSONENCODE ($data_json)

THIS IS THE ACTUAL DATA THAT IS TO BE PUT

{"id":"34400","name":"Know Your Rights_Stratford","account_id":"3","uuid":"KsGMzFna4CSo4VXQOTlJxkpitPCViVzqmUtB4APT","start_at":"2019-02-04T16:07:00Z","grading_standard_id":null,"is_public":false,"created_at":"2019-02-15T12:38:03Z","course_code":"Know Your Rights_STRAT","default_view":"wiki","root_account_id":1,"enrollment_term_id":1,"license":"private","end_at":"2019-05-30T00:00:00Z","public_syllabus":false,"public_syllabus_to_auth":false,"storage_quota_mb":10000,"is_public_to_auth_users":false,"hide_final_grades":false,"apply_assignment_group_weights":false,"calendar":{"ics":"https:\/\/test.instructure.com\/feeds\/calendars\/course_KsGMzFna4CSo4VXQOTlJxkpitPCViVzqmUtB4APT.ics"},"time_zone":"America\/New_York","blueprint":false,"sis_course_id":null,"sis_import_id":4702,"integration_id":null,"enrollments":[],"workflow_state":"available","restrict_enrollments_to_course_dates":false}

 

PHP CODE

/////////////////////////////////////////
// Post Update to Canvas
/////////////////////////////////////////

$newCanvasArray= $_REQUEST["mydata"]; 

$url = 'https://test.instructure.com/api/v1/accounts/2/courses/2'; //URL is just for example

$data_json = json_encode($newCanvasArray);
print_r($data_json);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
   'Authorization: Bearer ' . '<token>',
   'Content-Type: application/json',
   'Content-Length: ' . strlen($data_json)
));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
if(!$response){
   die("Connection Failure");
}
curl_close($curl);

echo $response;
file_put_contents("putresponse.html",$response); //File to store above screenshot

 

ERRORS

  1. No PHP errors are generated not even in the error_log file on the web server.
  2. No data was changed when the course is viewed in the canvas web page.

 

Can anyone shed some light?  Let me know if you need more information from me.

Thanks

Outcomes