使用 wordpress rest 每六小时生成一次唯一代码 api
Generate unique code every six hour with wordpress rest api
我想用 wordpress rest api 生成 12 位唯一代码,每六小时刷新一次。我没有找到符合我要求的任何代码或方法。
示例URL:....域名。com/wp-json/astra-sites/v1/get-last-unique-code
Json 上面的代码 link:
{"last_unique_code":"4qN6Ixy0&2!H"}
此代码每六小时刷新一次。请给我代码或详细信息以生成每六小时刷新一次的相同代码。
我知道如何自定义“register_rest_route”来创建自定义 api 路径,但我没有找到任何方法来使用 php 和 wordress 自动刷新代码。
我正在使用下面的代码
class My_Rest_APIs {
public function __construct() {
add_action( 'rest_api_init', array( $this, 'create_api_posts_meta_field' ) );
}
public function create_api_posts_meta_field() {
register_rest_route('my-custom-pages/v1', 'get-last-unique-code', [
'methods' => 'GET',
'callback' => array( $this, 'last_unique_code' )
]);
}
public function last_unique_code() {
$pass = substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstvwxyz@#$%&"), 0, 12);
$checksum = array('last_unique_code' => $pass);
return $checksum;
}
}
以上代码生成唯一代码,但我不知道如何将其存储在每六小时刷新一次的数据库中。我尝试了 update_option() 方法,但它不起作用。
谢谢。
在您的更新代码段下方执行。
当您 运行 SITE_URL/wp-json/my-custom-pages/v1/get-last-unique-code
时,您会发现校验和的值每六个小时更新一次
class My_Rest_APIs {
public function __construct() {
add_action( 'rest_api_init', array( $this, 'create_api_posts_meta_field' ) );
}
public function create_api_posts_meta_field() {
register_rest_route('my-custom-pages/v1', 'get-last-unique-code', [
'methods' => 'GET',
'callback' => array( $this, 'last_unique_code' )
]);
}
public function last_unique_code() {
// Get any existing copy of our transient data
if ( false === ( $pass = get_transient( 'pass_key' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$pass = substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstvwxyz@#$%&"), 0, 12);
set_transient( 'pass_key', $pass, 6 * HOUR_IN_SECONDS );
}
$checksum = array('last_export_checksums' => $pass);
return $checksum;
}
}
new My_Rest_APIs();
我想用 wordpress rest api 生成 12 位唯一代码,每六小时刷新一次。我没有找到符合我要求的任何代码或方法。
示例URL:....域名。com/wp-json/astra-sites/v1/get-last-unique-code
Json 上面的代码 link:
{"last_unique_code":"4qN6Ixy0&2!H"}
此代码每六小时刷新一次。请给我代码或详细信息以生成每六小时刷新一次的相同代码。
我知道如何自定义“register_rest_route”来创建自定义 api 路径,但我没有找到任何方法来使用 php 和 wordress 自动刷新代码。
我正在使用下面的代码
class My_Rest_APIs {
public function __construct() {
add_action( 'rest_api_init', array( $this, 'create_api_posts_meta_field' ) );
}
public function create_api_posts_meta_field() {
register_rest_route('my-custom-pages/v1', 'get-last-unique-code', [
'methods' => 'GET',
'callback' => array( $this, 'last_unique_code' )
]);
}
public function last_unique_code() {
$pass = substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstvwxyz@#$%&"), 0, 12);
$checksum = array('last_unique_code' => $pass);
return $checksum;
}
}
以上代码生成唯一代码,但我不知道如何将其存储在每六小时刷新一次的数据库中。我尝试了 update_option() 方法,但它不起作用。
谢谢。
在您的更新代码段下方执行。
当您 运行 SITE_URL/wp-json/my-custom-pages/v1/get-last-unique-code
时,您会发现校验和的值每六个小时更新一次
class My_Rest_APIs {
public function __construct() {
add_action( 'rest_api_init', array( $this, 'create_api_posts_meta_field' ) );
}
public function create_api_posts_meta_field() {
register_rest_route('my-custom-pages/v1', 'get-last-unique-code', [
'methods' => 'GET',
'callback' => array( $this, 'last_unique_code' )
]);
}
public function last_unique_code() {
// Get any existing copy of our transient data
if ( false === ( $pass = get_transient( 'pass_key' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$pass = substr(str_shuffle("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstvwxyz@#$%&"), 0, 12);
set_transient( 'pass_key', $pass, 6 * HOUR_IN_SECONDS );
}
$checksum = array('last_export_checksums' => $pass);
return $checksum;
}
}
new My_Rest_APIs();