我正在努力从响应头中提取X-Message-Id.谁能提供一个示例代码来获取这个X-Message-Id?

In my php:

try {
        $Response = $sendgrid->send($SgEmail);
        print_r($Response->headers());
    } 

The SendGrid Response

Array ( [0] => HTTP/1.1 202 Accepted [1] => Server: nginx [2] => Date: Fri, 22 Dec 2023 15:00:54 GMT [3] => Content-Length: 0 [4] => Connection: keep-alive [5] => X-Message-Id: BjsVqpBVTq63Z2fuITBUKw [6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io [7] => Access-Control-Allow-Methods: POST [8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl [9] => Access-Control-Max-Age: 600 [10] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html [11] => Strict-Transport-Security: max-age=600; includeSubDomains [12] => [13] => ) ('to','To_Name','Email@address.com')

推荐答案

您只需逐个查看所有头文件并判断以X-Message-Id:开头的头文件,或许可以使用以下内容:

$headers = $Response->headers();

$headerPrefix = 'X-Message-Id:';
$headerValue = null;
foreach ($headers as $header) {
  if (str_starts_with($header, $headerPrefix)) {
    $headerValue = trim(substr($header, strlen($headerPrefix)));
    break;
  }
}

// header value is now in $headerValue, or NULL if not found
// ...

Php相关问答推荐

WooCommerce拆分运输包裹上的商品数量增加运输成本

在AJX请求中使用简写后,FormData出现非法调用错误

如何在WordPress中为特定自定义帖子类型自定义URL struct

FatFreeFramework上的不同路由

在Google云存储对象上从PHP设置缓存控制TTL会更改错误的元数据

为什么在我的PHP联系人脚本中开机自检后出现未定义变量错误?

在PHP中,如何将介于0和1之间(包括0和1)的浮点概率转换为数组索引?

Azure应用程序无法访问共享文件夹

如何使用 PHP 将带有图像的文本上传到 Linkedin?

使用 secure 和 httponly 选项删除 php 中的 cookie 值得吗?

WooCommerce 相关产品(按 children 类别)作为排名数学主要类别的后备

MongoDB Laravel Jenssegers 包:插入数据库时​​未定义的类常量PRIMARY

无法在 Laravel 中实例化抽象类 ProductAbstract

在 DateTime 或 DateTimeImmutable 对象上调用modify() 时是否必须使用空格?

在 WooCommerce 管理变量产品中启用其他变体图像

如何在PHP中对多个脚本调用进行排队?

服务器遇到内部错误或配置错误,无法完成您的请求

如果有很多重定向,PHP curl 的行为不像命令行 curl

基于其他数组创建多维数组 struct

PHP 从 API 获取随机项目