1

I am trying to post a message via the Canvas API using PHP.

I believe this is more of a PHP question than Canvas.

The following code WORKS when I include a single userID for a "recipients[]' ('79' is a specific user idea and the API sends them a message - like an email).

See below for the API documentation and the issue trying to post to multiple IDs.

$post = [
  'recipients[]' => 79,
  'group_conversation' => true,
  'bulk_message' => true,
  'subject'=>$msg_subject,
  'body'=>$msg_body,
];


$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => $token_url,
  CURLOPT_HTTPHEADER => $header,
  CURLOPT_SSL_VERIFYPEER => false,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $post,
  CURLOPT_RETURNTRANSFER => true
));

$lti_msgresponse = curl_exec($curl);
curl_close($curl);
echo($lti_msgresponse);

Here is the Canvas documentation: https://edu.pretpriemacedu.mk/doc/api/conversations.html#method.conversations.create

Specifically:

recipients[] Required
string
An array of recipient ids. These may be user ids or course/group ids prefixed with “course_” or “group_” respectively, e.g. recipients[]=1&recipients=2&recipients[]=course_3

The API calls for a string to be sent for this "array" (brackets at the end?). You can't pass multiple "recipients" fields because only the last one will record (duh).

I think a solution might have to do with using http_build_query (see https://www.php.net/http_build_query) to send complex/nested arrays, but I have experimenting with various ways to pack more into "recipients[]" and they all fail.

Any PHP or general API wisdom def appreciated...

11teenth
  • 1,853
  • 1
  • 15
  • 28

1 Answers1

1

The post params ( specifically "recipients" ) should look like the following

$post = [
    'recipients'         => '59,48,19,55',
    'group_conversation' => true,
    'bulk_message'       => true,
    'subject'            => $msg_subject,
    'body'               => $msg_body,
];

or, perhaps the following: 'recipients' => [59,48,19,55]. But recipients[] would be an odd parameter to pass, as it contains special-characters.

admcfajn
  • 2,013
  • 3
  • 24
  • 32
  • That would be very logical! And thank you for answering... However, you can't pass an array inside the array - nested arrays not supported. Which is why I started looking at http_build_query - maybe smashing down the 'recipients' array... Thoughts? – 11teenth Dec 25 '19 at 21:38
  • I don't think it's a question of nested-arrays... It seems to only accept a string, but this bit here is completely out to lunch `recipients[]=1&recipients=2&recipients[]=course_3` ... I'd contact the staff at the api & ask them what's expected. A phone-call or an email / support-thread might be a lot cheaper than spending time coding in this case. – admcfajn Dec 25 '19 at 22:02
  • Hah! Well, it's helpful to know I'm not completely nuts and this is some crap documentation. Cheers. – 11teenth Dec 25 '19 at 22:03
  • 100% yeah! It's pretty hard to solve a problem you don't have access too. The [login page](https://edu.pretpriemacedu.mk/login/canvas) referred me to [instructure.com](https://www.instructure.com/). Cheer. – admcfajn Dec 25 '19 at 22:05
  • So - you were right. Took me some experimentation and wandering...the largest bad thing about the crappy documentation is the false lead to use recipients[]. This business: "recipients[]=1&recipients=2" The only line you need is 'recipients' => '256,268,276,249,26' Thanks Canvas Engineer! Burned most of a day pounding away at this...but learned a lot of additional stuff in the process...sigh. If you want to tweak your solution to suggest taking OUT the 'recipients[]' bit completely I'll accept as the solution. Thanks again for the help and moral support on Christmas! – 11teenth Dec 26 '19 at 03:20
  • Happy to help, glad you got it sorted! I've cleaned up my answer, Cheers – admcfajn Dec 26 '19 at 20:57