PHP / Curl 传入 Webhook 到 MS Teams 并非始终有效
PHP / Curl Incoming Webhook to MS Teams doesn't work all of the time
我在网络应用程序上使用传入的网络挂钩 post 向团队发送一些数据。在查找了一些实现方法后,我在 CURL 中实现了这个功能,但应该注意的是我没有 CURL 方面的经验。问题是,它大约有一半时间有效,然后另一半时间出现在错误日志中:
[01-Feb-2021 09:00:59 Europe/London] Error: Could not resolve host: outlook.office.com
希望有人能看一看是否有明显的我做错的地方。这是我的 PHP 文件中使用的代码(出于隐私考虑删除了 webhook ID)
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_URL, 'webhook-id-here');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"text\": \"$teamsMessage\"}");
$headers = array();
$headers[] = 'Content-Type:application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
error_log("Error: " . curl_error($ch), 0);
}
curl_close($ch);
看起来像是 DNS 问题。 无法解析主机:outlook.office.com。
也许有防火墙参与阻止?删除 CURLOPT_SSL_VERIFYPEER
和 CURLOPT_IPRESOLVE
.
试试这个简化的解决方案,它很适合我。我正在使用 JSON header 而不是表单编码。
// Paste the URL here
$url = 'https://outlook.office.com/webhook/XXX';
// Use the text encoded as MARKDOWN.
// Any markdown character needs to be escaped!
$body = ['text' => 'Hello World'];
$curlHandle = curl_init($url);
curl_setopt_array($curlHandle, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode($body)
]);
$response = curl_exec($curlHandle);
curl_close($curlHandle);
if ($response !== '1') throw new Exception('No Response from MS incoming webhook API');
我在网络应用程序上使用传入的网络挂钩 post 向团队发送一些数据。在查找了一些实现方法后,我在 CURL 中实现了这个功能,但应该注意的是我没有 CURL 方面的经验。问题是,它大约有一半时间有效,然后另一半时间出现在错误日志中:
[01-Feb-2021 09:00:59 Europe/London] Error: Could not resolve host: outlook.office.com
希望有人能看一看是否有明显的我做错的地方。这是我的 PHP 文件中使用的代码(出于隐私考虑删除了 webhook ID)
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_URL, 'webhook-id-here');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"text\": \"$teamsMessage\"}");
$headers = array();
$headers[] = 'Content-Type:application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
error_log("Error: " . curl_error($ch), 0);
}
curl_close($ch);
看起来像是 DNS 问题。 无法解析主机:outlook.office.com。
也许有防火墙参与阻止?删除 CURLOPT_SSL_VERIFYPEER
和 CURLOPT_IPRESOLVE
.
试试这个简化的解决方案,它很适合我。我正在使用 JSON header 而不是表单编码。
// Paste the URL here
$url = 'https://outlook.office.com/webhook/XXX';
// Use the text encoded as MARKDOWN.
// Any markdown character needs to be escaped!
$body = ['text' => 'Hello World'];
$curlHandle = curl_init($url);
curl_setopt_array($curlHandle, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode($body)
]);
$response = curl_exec($curlHandle);
curl_close($curlHandle);
if ($response !== '1') throw new Exception('No Response from MS incoming webhook API');