config = json_decode(file_get_contents($config_filename)); $this->ch = curl_init(); } function __destruct() { curl_close($this->ch); } // // Test communication with Porkbun API. // // https://porkbun.com/api/json/v3/documentation#Authentication // function ping() { // Create the correct endpoint based on the URL: $endpoint = $this->config->url . "ping"; // Set some cURL options: curl_setopt($this->ch, CURLOPT_URL, $endpoint); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->ch, CURLOPT_POST, 1); // Tell the server that the request body contains JSON data: $headers = array(); $headers[] = "Content-Type: application/json"; curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers); // Porkbun API wants to be passed data in JSON format: $json = array ( "apikey" => $this->config->apikey, "secretapikey" => $this->config->secretapikey ); curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($json)); // Execute cURL and return the result: $result = curl_exec($this->ch); if (curl_errno($this->ch)) { echo_to_cli("Error: " . curl_error($this->ch)); exit; } return $result; } // // Create a DNS record. // // https://porkbun.com/api/json/v3/documentation#DNS%20Create%20Record // function create(string $domain, string $name = "", string $type = "A", string $content = "1.1.1.1", int $ttl = 600) { // Create the correct endpoint based on the URL: $endpoint = $this->config->url . "dns/create/" . $domain; // Set some cURL options: curl_setopt($this->ch, CURLOPT_URL, $endpoint); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->ch, CURLOPT_POST, 1); // Tell the server that the request body contains JSON data: $headers = array(); $headers[] = "Content-Type: application/json"; curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers); // Porkbun API wants to be passed data in JSON format: $json = array ( "apikey" => $this->config->apikey, "secretapikey" => $this->config->secretapikey, "name" => "$name", "type" => "$type", "content" => "$content", "ttl" => "$ttl" ); curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($json)); // Execute cURL and return the result: $result = curl_exec($this->ch); if (curl_errno($this->ch)) { echo_to_cli("Error: " . curl_error($this->ch)); exit; } return $result; } // // Edit a DNS record. // // https://porkbun.com/api/json/v3/documentation#DNS%20Edit%20Record%20by%20Domain%20and%20ID // function edit(string $domain, string $id, string $content, string $name = "") { // Create the correct endpoint based on the URL: $endpoint = $this->config->url . "dns/edit/" . $domain; $endpoint .= "/" . $id; // Set some cURL options: curl_setopt($this->ch, CURLOPT_URL, $endpoint); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->ch, CURLOPT_POST, 1); // Tell the server that the request body contains JSON data: $headers = array(); $headers[] = "Content-Type: application/json"; curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers); // Porkbun API wants the API key and secret in a JSON structure: $json = array ( "apikey" => $this->config->apikey, "secretapikey" => $this->config->secretapikey, "name" => "$name", "type" => "A", "content" => "$content" ); curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($json)); // Execute cURL and return the result: $result = curl_exec($this->ch); if (curl_errno($this->ch)) { echo_to_cli("Error: " . curl_error($this->ch)); exit; } return $result; } // // Retrieve all editable DNS records associated with a domain or a single record for a particular record ID. // // https://porkbun.com/api/json/v3/documentation#DNS%20Retrieve%20Records%20by%20Domain%20or%20ID // function retrieve(string $domain, string $id = null) { // Create the correct endpoint based on the URL: $endpoint = $this->config->url . "dns/retrieve/" . $domain; if (!is_null($id)) $endpoint .= "/" . $id; // Set some cURL options: curl_setopt($this->ch, CURLOPT_URL, $endpoint); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->ch, CURLOPT_POST, 1); // Tell the server that the request body contains JSON data: $headers = array(); $headers[] = "Content-Type: application/json"; curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers); // Porkbun API wants the API key and secret in a JSON structure: $json = array ( "apikey" => $this->config->apikey, "secretapikey" => $this->config->secretapikey ); curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($json)); // Execute cURL and return the result: $result = curl_exec($this->ch); if (curl_errno($this->ch)) { echo_to_cli("Error: " . curl_error($this->ch)); exit; } return $result; } // // Retrieve all editable DNS records associated with a domain, subdomain and type.v // // https://porkbun.com/api/json/v3/documentation#DNS%20Retrieve%20Records%20by%20Domain,%20Subdomain%20and%20Type // function retrieveByNameType(string $domain, string $type, string $subdomain = null) { // Create the correct endpoint based on the URL: $endpoint = $this->config->url . "dns/retrieveByNameType/" . $domain . "/" . $type; if (!is_null($subdomain)) $endpoint .= "/" . $subdomain; // Set some cURL options: curl_setopt($this->ch, CURLOPT_URL, $endpoint); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->ch, CURLOPT_POST, 1); // Tell the server that the request body contains JSON data: $headers = array(); $headers[] = "Content-Type: application/json"; curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers); // Porkbun API wants the API key and secret in a JSON structure: $json = array ( "apikey" => $this->config->apikey, "secretapikey" => $this->config->secretapikey ); curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($json)); // Execute cURL and return the result: $result = curl_exec($this->ch); if (curl_errno($this->ch)) { echo_to_cli("Error: " . curl_error($this->ch)); exit; } return $result; } // These properties don't need to be declared as they would be dynamically created when assigned a value: private stdClass $config; private CurlHandle $ch; }; ?>