我正在尝试编写一个简单的插件,从API端点获取一些数据。我计划从一个短代码中读取api密钥,但还没到那个程度。
我编写了以下代码。我的问题是,我如何触发代码,以便调试它以查看发生了什么?
如果这是一个简单的问题,那么接下来的问题是如何从短代码中读取api密钥?
class DATA_PARSING
{
private static $instance;
/**
* Initializes the plugin and read some data
*
* @access private
*/
private function __construct()
{
add_action(\'data\', [$this, \'fetchData\']);
}
/**
* Creates an instance of this class
*
* @access public
* @return DATA_PARSING An instance of this class
*/
public function get_instance()
{
if (null == self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
private function fetchData($apiKey)
{
$url = \'https://api.website.com/data\';
$args = [
\'id\' => 1234,
\'fields\' => \'*\'
];
$method = \'GET\';
$headers = array(
\'Authorization\' => \'Bearer \' . $apiKey,
\'Accept\' => \'application/vnd.website.v1+json\',
\'content-type\' => \'application/json\',
);
$request = array(
\'headers\' => $headers,
\'method\' => $method,
);
if ($method == \'GET\' && !empty($args) && is_array($args)) {
$url = add_query_arg($args, $url);
} else {
$request[\'body\'] = json_encode($args);
}
$response = wp_remote_request($url, $request);
try {
$json = json_decode($response[\'body\']);
} catch (Exception $e) {
$json = null;
}
return $json;
}
}