I'm struggling to extract the X-Message-Id from the response header. Can anyone provide an example code that gets this 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 protected]')
You could just step through all the headers and check for the one that starts with X-Message-Id:
, perhaps using something like this:
$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
// ...