58 lines
1.9 KiB
PHP
58 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace traits;
|
|
|
|
trait wordpress_api_object_t
|
|
{
|
|
protected string $API_URL; // The URL of the WordPress API
|
|
protected string $API_KEY; // The key of the WordPress API
|
|
|
|
public function __construct()
|
|
{
|
|
$this->wordpress_api_object_t_construct();
|
|
}
|
|
|
|
public function wordpress_api_object_t_construct(): void
|
|
{
|
|
global $EMAIL_WASH_CERTIFICATE_TOKEN, $WORDPRESS_API_URL;
|
|
// Make sure the WordPress API configuration is set
|
|
if (!isset($EMAIL_WASH_CERTIFICATE_TOKEN) || !isset($WORDPRESS_API_URL)) {
|
|
throw new \Exception('WordPress API configuration is not set');
|
|
}
|
|
// Apply the WordPress API configuration
|
|
$this->API_KEY = $EMAIL_WASH_CERTIFICATE_TOKEN;
|
|
$this->API_URL = $WORDPRESS_API_URL;
|
|
}
|
|
|
|
public function debug(): void
|
|
{
|
|
echo 'API URL: ' . $this->API_URL . '<br>';
|
|
echo 'API KEY: ' . $this->API_KEY . '<br>';
|
|
}
|
|
|
|
public function request($invoke = 'example', $data = []): array
|
|
{
|
|
// Add the auth_key to the data
|
|
$data['auth_key'] = $this->API_KEY;
|
|
$data['invoke'] = $invoke;
|
|
$data['action'] = 'twc_api_endpoints';
|
|
// Send a post request to the API
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $this->API_URL);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
$response = curl_exec($ch);
|
|
curl_close($ch);
|
|
// Check if the response is an error
|
|
if ($response === false) {
|
|
return ['error' => 'Curl error: ' . curl_error($ch)];
|
|
}
|
|
// If the response is not JSON, return it as is
|
|
if ($response[0] !== '{') {
|
|
return ['response' => $response];
|
|
}
|
|
return json_decode($response, true);
|
|
}
|
|
} |