【发布时间】: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