Change ip trong Cloudflare bằng PHP

Discussion in 'Chia Sẻ Tool & Bot' started by hutieumi, Aug 18, 2017.

  1. hutieumi

    hutieumi Khách Qua Đường

    Code này dùng để thay đổi Ip cho nhiều website 1 lúc trong cloudflare.
    Code:
    <?php
    
    set_time_limit(999999);
    
    $domainurl = array(
    "xxxxx.net",
    "bbbbbb.net"
    );
    
    $token = 'aaaaaa'; //https://www.cloudflare.com/my-account
    $email = '[email protected]';
    $new_ip = '149.202.219.xx;  //Gets server's IP Address
    $old_ip = '149.202.223.yy'; //Gets DNS's IP Address
    
    foreach ($domainurl as $domain) {
    $hostname = $domain; //Gets current hostname
    
    
    
    if ( $new_ip !== $old_ip ) {// iIf the IP addresses are the same, we don't need to make any changes
        $api_url = 'https://www.cloudflare.com/api_json.html';
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $api_url );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
        $body = array(
            'a'    => 'rec_load_all',
            'tkn'    => $token,
            'email' => $email,
            'z'     => $domain,
        );
        curl_setopt( $ch, CURLOPT_POST, true );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
        if ( $output = curl_exec( $ch ) ) {
            $results = json_decode( $output );
            if ( 'success' === $results->result ) {
                foreach( $results->response->recs->objs as $dns_entry ) {
                    if ( $dns_entry->name === $hostname ) {
                        $record_id = $dns_entry->rec_id;
                        $new_entry = false;
                        break;
                    } else {
                        $new_entry = true;
                    }
                }
                if ( $new_entry ) {
                    $body = array(
                        'a'     => 'rec_new',
                        'tkn'    => $token,
                        'email' => $email,
                        'z'    => $domain,
                        'type'    => 'A',
                        'name'     => $hostname,
                        'content' => $new_ip,
                        'ttl'     => '1',
                    );
                    curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
                                    if ( $output = curl_exec( $ch ) ) {
                        $results = json_decode( $output );
                                if ( 'success' === $results->result ) {
                            echo "IP Address added for hostname.\n";
                        } else {
                            printf( "Error adding ip address for hostname: %s\n", $results->msg );
                        }
                                    } else {
                                            echo "Error adding IP Address for hostname.\n";
                                    }
                } else {
                    $body = array(
                        'a'     => 'rec_edit',
                        'tkn'     => $token,
                        'email' => $email,
                        'z'     => $domain,
                        'id'     => $record_id,
                        'type'     => 'A',
                        'name'     => $hostname,
                        'content' => $new_ip,
                        'ttl'     => '1',
                    );
                        curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
                    if ( $output = curl_exec( $ch ) ) {
                        $results = json_decode( $output );
                                if ( 'success' === $results->result ) {
                            echo "IP Address updated for hostname.\n";
                        } else {
                            printf( "Error adding ip address for hostname: %s\n", $results->msg );
                        }
                    } else {
                        echo "Error changing IP Address for hostname.\n";
                    }
                }
            } else {
                printf( "Error adding ip address for hostname: %s\n", $results->msg );
            }
        }
        curl_close( $ch );
    } else {
        echo "No changes required\n";
    }
    }
     
    Huynh Dang, EDM and money like this.
  2. Nai

    Nai MiddleMan Staff Member

    Hiz, không hiểu luôn á
     
  3. Boo Đẹp Trai

    Boo Đẹp Trai Tân Thủ Thôn

    Thay vì thế lúc setup trỏ thẳng về 1 CNAME rồi đổi IP CNAME đó không phải nhanh hơn sao :v
     
  4. hutieumi

    hutieumi Khách Qua Đường

    dùng để thay đổi ip trong cloudflare khi chuyển server thôi bác
     
  5. fixbug

    fixbug Moderator Staff Member

    e có viết script = bash shell, bro nào cần ko :D