【问题标题】:PHP Warning: Missing argument 2 for Mailchimp::call()PHP 警告:Mailchimp::call() 缺少参数 2
【发布时间】:2018-03-14 09:39:31
【问题描述】:

可以在 Internet 上找到 there 的许多资源,其中包含有关如何在 WordPress 中显示 MailChimp 订阅者数量的相同代码和说明。直到今天收到 PHP 警告时,我才使用该代码没有问题:

PHP 警告:Mailchimp::call() 缺少参数 2,已调用 /.../mc-subscriber-count/mc-subscriber-count.php 在第 19 行和 在 /.../mc-subscriber-count/Mailchimp.php 第 192 行定义

mc-subscriber-count.php 完整代码:

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' );

Mailchimp.php 代码(仅第 192 行中的函数 - 完整代码 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;
}

如何解决该警告?

更新

我忘了提到我发现缺少第二个参数,但我不明白第二个参数是什么。

附:我不是 PHP 程序员,所以不要打败我。

【问题讨论】:

  • 你能指定第 19 行是什么吗?
  • 您发布了Mailchimp::call() 的代码,它需要两个参数(没有一个是可选的)。你只用一个参数来调用它。有什么问题?
  • @Tomm 第 19 行在第一块代码中标有注释。
  • 很抱歉我没有看到
  • 在您使用的 SDK 的文档中查找。

标签: php wordpress mailchimp mailchimp-api-v3.0


【解决方案1】:
在收到 cmets 后,我反复分析了上述两个函数,并决定将 `$params` 变量作为第二个参数添加到第 19 行,所以它现在看起来像这样: $mc = $MailChimp->call('lists/list', $params); 我不知道这是否是正确的方法,但我现在会等待错误,如果有的话。

更新

因为我在这里只收到了反对票而不是帮助(但这没关系)并且我的第一个解决方案不起作用(请参阅已删除的文本),我再次搜索并最终找到了一个更好的解决方案(我希望)@ 987654321@。我只是将推荐的代码包装在这样的短代码中:

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' );

只需将 [mc-subscribers] 短代码放在要显示 MailChimp 订阅者数量的位置即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多