Hurl.it Settings I am using.

I have some code for a WordPress plugin that logs into a website and gets data from the API which requires the user to be logged in. I can log in using hurl.it

The hurl.it settings I am using:

Error: Maximum (20) redirects followed

Info:
POST /login HTTP/1.1 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7
Host: example.com
Referer: example.com/login
Cookie: JSESSIONID=EEE3E265E723F10GH2EC6600C6F9E1AA; cookiename=dgl
Accept: */*
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded

<?php
//Main WP-DGL Class
class WP_DGL {
    /**
     * Adds the required hooks.
     */
    public function __construct() {
        add_action( 'admin_init', array( $this, 'settings_init' ) );
        add_action( 'init', array( $this, 'wp_dgl_shortcodes_init' ) );

        //$this->load_textdomain();
    }

    /**
     * Inits the settings page
     */
    public function settings_init() {
        add_settings_section(
            'wp_dgl_dgl_settings',
            esc_html__( 'WP DGL Settings', 'wp-dgl' ),
            array( $this, 'settings_callback' ),
            'general'
        );

        add_settings_field(
            'wp_dgl_dgl_username',
            esc_html__( 'Account Username', 'wp-dgl' ),
            array( $this, 'print_dgl_username_field' ),
            'general',
            'wp_dgl_dgl_settings'
        );

        add_settings_field(
            'wp_dgl_dgl_password',
            esc_html__( 'Account Password', 'wp-dgl' ),
            array( $this, 'print_dgl_password_field' ),
            'general',
            'wp_dgl_dgl_settings'
        );

        register_setting( 'general', 'wp_dgl_dgl_username' );
        register_setting( 'general', 'wp_dgl_dgl_password' );
    }

    /**
     * Prints the description in the settings page.
     */
    public function settings_callback() {
        esc_html_e( 'Setup Up DGL website access.', 'wp-dgl' );
    }

    /**
     * Prints the Bot Username settings field.
     */
    public function print_dgl_username_field() {
        $value = get_option( 'wp_dgl_dgl_username' );

        echo '<input type="text" name="wp_dgl_dgl_username" value="' . esc_attr( $value ) . '" style="width:300px;margin-right:10px;" />';
        echo '<span class="description">' . esc_html__( 'The username of the account.', 'wp-dgl' ) . '</span>';
    }

    /**
     * Prints the Avatar URL settings field.
     */
    public function print_dgl_password_field() {
        $value = get_option( 'wp_dgl_dgl_password' );

        echo '<input type="text" name="wp_dgl_dgl_password" value="' . esc_attr( $value ) . '" style="width:300px;margin-right:10px;" />';
        echo '<span class="description">' . esc_html__( 'The password of the account.', 'wp-dgl' ) . '</span>';
    }

    /**
     * Loads the plugin localization files.

    public function load_textdomain() {
        $locale = apply_filters( 'plugin_locale', get_locale(), 'wp-dgl' );
        load_textdomain( 'wp-dgl', WP_LANG_DIR . '/wp-discord-post/discord-post-' . $locale . '.mo' );
        load_plugin_textdomain( 'wp-dgl', false, plugin_basename( __DIR__ ) . '/languages' );
    }*/

    public function get_cups() {
        //username and password of account
        $username = get_option( 'wp_dgl_dgl_username' );
        $password = get_option( 'wp_dgl_dgh_password' );

        if (is_null($username) or is_null($password))
        {
            return "Please check the settings!";
        }

        //set the directory for the cookie using defined document root var
        $dir = plugin_dir_path( __FILE__ );
        //build a unique path with every request to store 
        //the info per user with custom func. 
        $path = $dir;

        //login form action url
        $url="https://example.com/login.php"; 
        $postinfo = "password=".$password."&username=".$username;

        $cookie_file_path = $path."/cookies.txt";
        $request_headers = [
                    'Accept: */*',
                    'Accept-Encoding: gzip, deflate',
                    'Content-Type: application/x-www-form-urlencoded',
        ];

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_NOBODY, false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
        //set the cookie the site has for certain features, this is optional
        curl_setopt($ch, CURLOPT_COOKIE, "cookiename=dgl");
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
        curl_setopt($ch, CURLOPT_ENCODING, "");
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
        curl_exec($ch);

        if(curl_error($ch)) //Catch errors and return them
        {
            $info = curl_getinfo($ch);
            return 'Error: ' . curl_error($ch) . "<br>Info: " . $info['request_header'];
        }
        elseif ($cups_raw == null) //If the text is null return failed.
        {
            return "Failed to recieve anything";
        }

        //page with the content I want to grab
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_URL, "https://example.com/api/json");
        //do stuff with the info with DomDocument() etc
        $cups_raw = curl_exec($ch);

        if(curl_error($ch))
        {
            $info = curl_getinfo($ch);
            return 'Error: ' . curl_error($ch) . "<br>Info: " . $info['request_header'];
        }
        elseif ($cups_raw == null)
        {

            curl_close($ch);
            return "Failed to recieve anything";
        }

        curl_close($ch);

        return $cups_raw;
    }

    /**
    Creates The Shortcodes to be used
    */
    public function wp_dgl_shortcodes_init()
    {
        function dgl_cups_shortcode($atts = [], $content = null)
        {
            return WP_DGL::get_cups();
        }
        add_shortcode('dgl_cups', 'dgl_cups_shortcode');

        function dgl_clan_shortcode($atts = [], $content = null)
        {
            return "<strong><em>NOT SETUP YET</em></strong>";
        }
        add_shortcode('dgl_clan', 'dgl_clan_shortcode');

        function dgl_team_shortcode($atts = [], $content = null)
        {
            return "<strong><em>NOT SETUP YET</em></strong>";
        }
        add_shortcode('dgl_team', 'dgl_team_shortcode');

        function dgl_user_shortcode($atts = [], $content = null)
        {
            return "<strong><em>NOT SETUP YET</em></strong>";
        }
        add_shortcode('dgl_user', 'dgl_user_shortcode');
    }
}

new WP_DGL();

I have tried many things but I still can’t figure it out.
I am open to suggestion to fix the code as well as improvement to the code so that I can learn more since it is the first time messing around with curl.

Read more here: PHP curl Maximum (20) redirects followed


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: