Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- # ========================================================================#
- #
- # Author: Ruslan Mingaev & Dmitry Gordienko
- # Version: 2.0
- # Date: 17-Oct-16
- # Purpose: Change ip server for domain on CloudFlare.
- # Requires : Requires PHP5.
- # Usage Example:
- # include("classes/cloudflare.class.php");
- # $cl = new CloudFlare('email','global_key');
- # $cl->set_new_ip('domain.com','server_ip');
- #
- #
- # ========================================================================#
- class CloudFlare {
- private $api_url = 'https://api.cloudflare.com/client/v4/';
- private $email;
- private $global_key;
- private $cookie;
- private $user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:49.0) Gecko/20100101 Firefox/49.0';
- public $Response;
- public function __construct($email, $global_key) {
- $this->email = $email;
- $this->global_key = $global_key;
- }
- public function set_new_ip($domain_name, $new_ip) {
- if($domain_name != null) $data['name'] = $domain_name;
- $res = $this->api_call('zones', $data);
- if($res['success']) {
- $zone_id = $res['result'][0]['id'];
- $record_id = null;
- $records = $this->api_call('zones/'.$zone_id.'/dns_records',array());
- foreach ($records['result'] as $dns) {
- if($dns['type'] == 'A') {
- $record_id = $dns['id'];
- break;
- }
- }
- $post['data'] = json_encode(array(
- 'id' => $record_id,
- 'type' => 'A',
- 'name' => '@',
- 'content' => $new_ip,
- 'zone_id' => $zone_id,
- 'zone_name' => '@',
- 'proxiable' => true,
- 'proxied' => false,
- 'locked' => false,
- 'ttl' => '120'
- ));
- $res = $this->api_call('zones/'.$zone_id.'/dns_records/'.$record_id, $data, $post);
- return $res['success'];
- }
- }
- private function get_headers(){
- return array(
- "X-Auth-Email: ".$this->email,
- "X-Auth-Key: ".$this->global_key,
- "Content-Type: application/json"
- );
- }
- private function api_call($method, $get = null, $post = null) {
- if($post != null) {
- $request = curl_init($this->api_url.$method);
- curl_setopt($request, CURLOPT_POSTFIELDS, $post['data']);
- curl_setopt($request, CURLOPT_CUSTOMREQUEST, 'PUT');
- }else{
- $request = curl_init($this->api_url.$method.'?'.http_build_query($get));
- curl_setopt($request, CURLOPT_CUSTOMREQUEST, 'GET');
- }
- curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($request, CURLOPT_COOKIESESSION, true);
- curl_setopt($request, CURLOPT_VERBOSE, 0);
- curl_setopt($request, CURLOPT_USERAGENT, $this->user_agent);
- curl_setopt($request, CURLOPT_COOKIEFILE, $this->cookie);
- curl_setopt($request, CURLOPT_COOKIEJAR, $this->cookie);
- curl_setopt($request, CURLOPT_HTTPHEADER, $this->get_headers());
- $this->Response = curl_exec($request);
- curl_close($request);
- return json_decode($this->Response, true);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement