Php wp异步任务don';t消防运行动作法
我必须使用techcrunch在我的wordpress插件的后台运行同步任务 为了测试,在主文件的底部,我有:Php wp异步任务don';t消防运行动作法,php,asynchronous,wordpress,background-process,Php,Asynchronous,Wordpress,Background Process,我必须使用techcrunch在我的wordpress插件的后台运行同步任务 为了测试,在主文件的底部,我有: //top of the php file require_once(dirname(__FILE__) . '/lib/WP_Async_Task.php'); require_once(dirname(__FILE__) . '/class/my_api_status.class.php'); define('API_URL', '...'); /* ... */ // At
//top of the php file
require_once(dirname(__FILE__) . '/lib/WP_Async_Task.php');
require_once(dirname(__FILE__) . '/class/my_api_status.class.php');
define('API_URL', '...');
/* ... */
// At the bottom of the file
function my_api_status($api_url)
{
sleep(5);
$r = wp_safe_remote_get($api_url);
if (!is_wp_error($r)) {
$body = json_decode(wp_remote_retrieve_body($r));
if (isset($body->success)) {
return;
}
}
}
add_action('wp_async_api_status', 'my_api_status');
function my_init_api_status()
{
new ApiStatusTask();
do_action('api_status', constant('API_URL'));
}
add_action('plugins_loaded', 'my_init_api_status');
和api状态任务类
class ApiStatusTask extends WP_Async_Task {
protected $action = 'api_status';
/**
* Prepare data for the asynchronous request
* @throws Exception If for any reason the request should not happen
* @param array $data An array of data sent to the hook
* @return array
*/
protected function prepare_data( $data ) {
return array(
'api_url' => $data[0]
);
}
/**
* Run the async task action
*/
protected function run_action() {
if(isset($_POST['api_url'])){
do_action("wp_async_$this->action", $_POST['api_url']);
}
}
}
launch
正确调用了函数prepare\u data
,之后也正确调用了launch\u on\u shutdown
,最后使用admin post.php
在launch\u on\u shutdown
结束时调用了wp\u remote\u post
。
但是从未调用函数run\u action
。。。因此,主文件中的my\u api\u status
它可能会出什么问题?我将很快在这里给出一个完整的插件示例。但现在,我发现了我的问题:
// In the `launch_on_shutdown` method of `WP_Async_Task` class
public function launch_on_shutdown() {
GcLogger::getLogger()->debug('WP_Async_Task::launch_on_shutdown');
if ( ! empty( $this->_body_data ) ) {
$cookies = array();
foreach ( $_COOKIE as $name => $value ) {
$cookies[] = "$name=" . urlencode( is_array( $value ) ? serialize( $value ) : $value );
}
$request_args = array(
'timeout' => 0.01,
'blocking' => false,
'sslverify' => false, //apply_filters( 'https_local_ssl_verify', true ),
'body' => $this->_body_data,
'headers' => array(
'cookie' => implode( '; ', $cookies ),
),
);
$url = admin_url( 'admin-post.php' );
GcLogger::getLogger()->debug('WP_Async_Task::launch_on_shutdown wp_remote_post');
wp_remote_post( $url, $request_args );
}
}
sslverify选项在我的本地环境中失败。如果我们不生产的话,我就得把它涂成假的
设置此选项后,将正确触发运行操作。你好,托马斯,谢谢您发布此消息!我尝试在Wordpress v4.7.5上运行类似的设置。我在theme functions.php文件中设置了自定义类,该类扩展了WP_Async_任务类。但是,即使在
WP\u Async\u Task
类的launch\u on\u shutdown
方法中设置sslverify,这也不起作用。您知道Techcrunch插件和此方法在Wordpress v4.7.5中是否有效吗。我们一直在努力让这个插件使用一个钩子,甚至是wp\u async\u save\u post钩子。