我有一个使用 mysql 和 php 的应用程序,我通过 mysql 的事件更改数据状态,我想在数据状态更改时通知客户。
我知道 cron 作业可以工作但有延迟,有没有办法让 mysql 在数据更改时发出 http post 请求。
我尝试使用 mysql 过程中的 curl,但在执行时失败了
1305 - FUNCTION my_db.curl_init does not exist
完整的程序代码是:
DELIMITER $$
CREATE PROCEDURE send_notifications()
BEGIN
-- Set variables
SET @url = 'url/sendnotifications';
SET @data = '{"key":"data value"}';
-- Build the request
SET @ch = curl_init();
CALL curl_setopt(@ch, CURLOPT_URL, @url);
CALL curl_setopt(@ch, CURLOPT_POST, true);
CALL curl_setopt(@ch, CURLOPT_POSTFIELDS, @data);
CALL curl_setopt(@ch, CURLOPT_RETURNTRANSFER, true);
CALL curl_setopt(@ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer xxxx'
));
-- Execute the request
SET @response = curl_exec(@ch);
SET @error = curl_error(@ch);
SET @httpcode = curl_getinfo(@ch, CURLINFO_HTTP_CODE);
-- Close the request
CALL curl_close(@ch);
-- Do something with the response, such as logging it
IF @httpcode = 200 THEN
SELECT @response;
ELSE
SELECT @error, @httpcode;
END IF;
END $$
DELIMITER ;
我打算调用一个可以在后台异步执行的作业函数,而不会在 mysql 中增加负载。