-3

Can be found there many resources on Internet with the same code and instructions about how to show your MailChimp subscriber count in WordPress. I used that code without problems until today, when received an PHP Warning:

PHP Warning: Missing argument 2 for Mailchimp::call(), called in /.../mc-subscriber-count/mc-subscriber-count.php on line 19 and defined in /.../mc-subscriber-count/Mailchimp.php on line 192

The mc-subscriber-count.php full code:

function wpb_mc_sub_count() {
    include "Mailchimp.php";
    $lastRunLog = __DIR__ . '/logs/lastrun.log';
    $subfile = __DIR__ . '/logs/subcount.log';
    $lastRun = file_get_contents( $lastRunLog );

    if ( time() - $lastRun >= 86400 ) {
        $MailChimp = new MailChimp( 'Your_MailChimp_API_Key' );
        $mc = $MailChimp->call( 'lists/list' ); // THE LINE 19
        /*****************************************************/
        $subscriber_count .= $mc[data][0][stats][member_count];
        file_put_contents( $lastRunLog, time() );
        file_put_contents( $subfile, $subscriber_count );
    } else {
        $subscriber_count .= file_get_contents( $subfile );
    }

    return number_format( $subscriber_count );

}

add_shortcode( 'mc-subscribers', 'wpb_mc_sub_count' );
add_filter( 'widget_text', 'do_shortcode' );

The Mailchimp.php code (only the function from the line 192 - full code here):

public function call($url, $params) {
    $params['apikey'] = $this->apikey;

    $params = json_encode($params);
    $ch     = $this->ch;

    curl_setopt($ch, CURLOPT_URL, $this->root . $url . '.json');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_VERBOSE, $this->debug);

    $start = microtime(true);
    $this->log('Call to ' . $this->root . $url . '.json: ' . $params);
    if($this->debug) {
        $curl_buffer = fopen('php://memory', 'w+');
        curl_setopt($ch, CURLOPT_STDERR, $curl_buffer);
    }

    $response_body = curl_exec($ch);

    $info = curl_getinfo($ch);
    $time = microtime(true) - $start;
    if($this->debug) {
        rewind($curl_buffer);
        $this->log(stream_get_contents($curl_buffer));
        fclose($curl_buffer);
    }
    $this->log('Completed in ' . number_format($time * 1000, 2) . 'ms');
    $this->log('Got response: ' . $response_body);

    if(curl_error($ch)) {
        throw new Mailchimp_HttpError("API call to $url failed: " . curl_error($ch));
    }
    $result = json_decode($response_body, true);

    if(floor($info['http_code'] / 100) >= 4) {
        throw $this->castError($result);
    }

    return $result;
}

How to solve that warning?

UPDATE

I forgot to mention that I see that there is missing a second argument, but I don't understand what this second argument can be.

P.S. I am not a PHP coder, so don't beat me.

Yurié
  • 2,148
  • 3
  • 23
  • 40
  • could you specify what line 19 is? – Tomm Oct 03 '17 at 08:37
  • 2
    You posted the code of `Mailchimp::call()` and it requires two arguments (none is optional). You call it with only one argument. What is the problem? – axiac Oct 03 '17 at 08:37
  • @Tomm line #19 is marked with a comment in the first block of code. – axiac Oct 03 '17 at 08:38
  • I am sorry i did not see that – Tomm Oct 03 '17 at 08:38
  • You are only calling 1 argument like axiac also said you need to specify 2 arguments – Tomm Oct 03 '17 at 08:40
  • Thank you very much to all for very constructive suggestions! Probably it is my fault that I haven't mention that I know about the missing argument, but nobody said what this second argument can be. – Yurié Oct 03 '17 at 17:50
  • 1
    Look it up in the documentation for the SDK you're using. – ceejayoz Oct 03 '17 at 17:56

1 Answers1

0
After the received comments I analysed repeatedly the above two functions and decided to add as a second parameter to the line 19 the `$params` variable, so it looks now like this: $mc = $MailChimp->call( 'lists/list', $params ); I don't know if this is the correct way, but I will wait now for errors, if will be some there.

UPDATE

Because I received here only downvotes instead of help (but it doesn't matter) and my first solution doesn't worked (see the deleted text), I googled again and finally I found a better one (I hope) to display the MailChimp subscribers count in Wordpress. I just wrapped the recommended code in a shortcode like this:

function wpb_mc_sub_count() {
    $api['key']= 'INSERT-YOUR-API-KEY-HERE';
    $url='https://INSERT-YOUR-US-DOMAIN-KEY.api.mailchimp.com/3.0//lists/INSERT-YOUR-LISTID-HERE/?apikey=INSERT-YOUR-API-KEY-HERE';
    $lastRunLog = '/logs/lastrun.log';
    $subfile = '/logs/subcount.log';
    $lastRun = file_get_contents($lastRunLog);

    if (time() - $lastRun >= 3600) {
        // it's been more than one hour so we will connect to the API
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL,$url);
        $result=curl_exec($ch);
        curl_close($ch);
        $json = json_decode($result, true);
        $total= $json['stats']['member_count'];
        // update lastrun.log with current time
        file_put_contents($lastRunLog, time());
        file_put_contents($subfile, $total);
    } else {
        $total = file_get_contents($subfile);
    }

    //echo $total;
    return $total;

}

add_shortcode( 'mc-subscribers', 'wpb_mc_sub_count' );
//add_filter( 'widget_text', 'do_shortcode' );

Just put the [mc-subscribers] shortcode where you want to display your MailChimp subscribers count.

Yurié
  • 2,148
  • 3
  • 23
  • 40