0

我有一个 wordpress 页面,我需要在 cron 中设置这个页面。我该怎么做?我的wordpress页面链接是:

http://example.com/wp-admin/admin.php?page=popshop-import&category=32194&cate_id=1279

当我在浏览器中运行此 url 时,此页面工作正常。如何将其设置为 wordpress cron?

4

1 回答 1

1

好吧,我对 popshop api 没有任何经验,但是使用 wordpress crons 非常简单。您只需要创建一个函数来执行您想要的任何操作,然后将其挂接到 wp_schedule_event 函数中。

让我们假设 POPSHOP API 是一个简单的 REST API。这就是你将如何完成 cron 工作的方式。

if ( ! wp_next_scheduled( 'popshop_api' ) ) {
  wp_schedule_event( time(), 'hourly', 'popshop_api' );
}

add_action( 'popshop_api', 'running_popshop_api' );

function my_task_function() {
  $service_url = 'http://example.com/wp-admin/admin.php?page=popshop-import&category=32194&cate_id=1279';
  $curl = curl_init($service_url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  $curl_response = curl_exec($curl);
  if ($curl_response === false) {
      $info = curl_getinfo($curl);
      curl_close($curl);
      die('error occured during curl exec. Additioanl info: ' .   var_export($info));
  }
  curl_close($curl);
  $decoded = json_decode($curl_response);
  if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
      die('error occured: ' . $decoded->response->errormessage);
  }
  echo 'response ok!';
  var_export($decoded->response);

}

所以代码使用 curl 来查找响应,如果响应不为假,它会将数据保存到 $curl_response 变量,然后你可以 var_dump/var_export 它并用它做任何你想做的事情。

于 2015-05-05T07:05:29.913 回答