SendGrid 响应 PHP

问题描述 投票:0回答:1

我正在努力从响应标头中提取 X-Message-Id。谁能提供获取此 X-Message-Id 的示例代码吗?

在我的PHP中:

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

SendGrid 响应

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 protected]')
php sendgrid
1个回答
0
投票

您可以单步浏览所有标题并检查以

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
// ...
© www.soinside.com 2019 - 2024. All rights reserved.