简体   繁体   English

如何使用 PHP 和 CURL 从 API 端点 POST 到多个 ID?

[英]How can I POST to multiple IDs from an API endpoint using PHP and CURL?

I am trying to post a message via the Canvas API using PHP.我正在尝试使用 PHP 通过 Canvas API 发布消息。

I believe this is more of a PHP question than Canvas.我相信这比 Canvas 更像是一个 PHP 问题。

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).当我为“收件人[]”(“79”是一个特定的用户想法,API 向他们发送一条消息 - 就像一封电子邮件)包含一个用户 ID 时,以下代码有效。

See below for the API documentation and the issue trying to post to multiple IDs.有关 API 文档和尝试发布到多个 ID 的问题,请参见下文。

$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这是画布文档: https : //edu.pretpriemacedu.mk/doc/api/conversations.html#method.conversations.create

Specifically:具体来说:

recipients[] Required收件人[] 必填
string细绳
An array of recipient ids.一组收件人 ID。 These may be user ids or course/group ids prefixed with “course_” or “group_” respectively, eg recipients[]=1&recipients=2&recipients[]=course_3这些可能是分别以“course_”或“group_”为前缀的用户ID或课程/组ID,例如recipients[]=1&recipients=2&recipients[]=course_3

The API calls for a string to be sent for this "array" (brackets at the end?). API 调用要为此“数组”发送的字符串(末尾的括号?)。 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.我认为一个解决方案可能与使用 http_build_query (见https://www.php.net/http_build_query )发送复杂/嵌套数组有关,但我尝试了各种方法将更多内容打包到“收件人[]”中,他们都失败了。

Any PHP or general API wisdom def appreciated...任何 PHP 或通用 API 智慧都值得赞赏......

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] .或者,也许是以下内容: 'recipients' => [59,48,19,55] But recipients[] would be an odd parameter to pass, as it contains special-characters.但是recipients[]将是一个奇怪的参数,因为它包含特殊字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM