Webhooks - 身份验证类型 Api 密钥 - 秘密名称和秘密值 - PHP 中
P粉545910687
P粉545910687 2023-08-30 18:46:16
[PHP讨论组]
<p>我正在尝试使用 api 密钥(秘密名称和秘密值)对 webhook 进行身份验证。所以我制作了两个文件:</p> <p>webhook.php:</p> <pre class="brush:php;toolbar:false;">&lt;?php include('webhook-api-key.php'); // Retrieve the request body from the webhook POST request if ($http_status_code === 200){ $request_body = file_get_contents('php://input'); // Convert the request body from JSON to a PHP object $request_data = json_decode($request_body); // Extract the contact properties from the request data $contact_properties = $request_data-&gt;properties; // Extract the email property value $email = $contact_properties-&gt;email-&gt;value; // Extract the first name property value $first_name = $contact_properties-&gt;firstname-&gt;value; // Extract the last name property value $last_name = $contact_properties-&gt;lastname-&gt;value; // Do something with the contact data, such as adding it to a database or sending an email notification // For example: $contact_data = array( 'email' =&gt; $email, 'first_name' =&gt; $first_name, 'last_name' =&gt; $last_name ); // Add the contact data to a database or send an email notification, etc. // Send a HTTP response to HubSpot indicating that the webhook was successfully received and processed http_response_code(200); } ?&gt;</pre> <p>和 webhook-api-key.php:</p> <pre class="brush:php;toolbar:false;">&lt;?php $endpoint_url = 'https:/.../hubspot/webhook.php'; // Set up the API key secret name and secret value $api_key_secret_name = 'word'; $api_key_secret_value = 'anther_word'; // Set up the HTTP POST request headers $headers = array( 'Content-Type: application/json', 'Authorization: Bearer '.$api_key_secret_value ); // Set up the HTTP POST request body $body = array( 'api_key' =&gt; $api_key_secret_value ); // Send the HTTP POST request to the webhook endpoint URL $ch = curl_init($endpoint_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); $response = curl_exec($ch); // Check for errors if(curl_errno($ch)) { $error_message = curl_error($ch); echo 'Error: '.$error_message; } // Get the HTTP response status code $http_status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Close the HTTP POST request curl_close($ch); // Handle the webhook response if ($http_status_code === 200) { echo 'Webhook successfully authenticated.'; } else { echo 'Webhook authentication failed with HTTP status code: ' . $http_status_code; } ?&gt;</pre> <p>在 Hubspot 配置中,网址为“https:/.../hubspot/webhook.php”。</p> <p>这样可以吗?我这么问是因为当我尝试测试它时它杀死了我的服务器,并且我在互联网上找不到使用这种身份验证的示例。</p> <p>谢谢!</p>
P粉545910687
P粉545910687

全部回复(1)
P粉413307845

所以其实很简单。互联网上没有示例,文档也很差,它更多地解释了 Hubspot 签名而不是 API 密钥。 我最终明白了它是如何工作的,这是工作代码:

$expectedSecretName = 'word'; // Replace with your expected secret name
$expectedSecretValue = 'another_word'; // Replace with your expected secret value

$requestBody = file_get_contents('php://input');
$data = json_decode($requestBody);

    if($_SERVER['HTTP_WORD'] == $expectedSecretValue){
//do something with values
$email = $data->email;
$firstname= $data->firstname;
$lastname= $data->lastname;
}
else{
//not from Hubspot
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号