可以在Internet上找到there许多资源,其中包含有关如何在WordPress中显示MailChimp订阅者计数的相同代码和说明。直到今天,我收到PHP警告时才使用该代码:
PHP警告:缺少Mailchimp :: call()的参数2,调用 第19行的/.../mc-subscriber-count/mc-subscriber-count.php和 在第192行的/.../mc-subscriber-count/Mailchimp.php中定义
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;
}
如何解决警告?
更新
我忘了提到我发现错过了第二个论点,但我不明白第二个论点是什么。
P.S。我不是PHP程序员,所以不要打败我。
答案 0 :(得分:0)
<强>更新强>
因为我收到的只是downvotes而不是帮助(但没关系)并且我的第一个解决方案不起作用(请参阅删除的文本),我再次搜索,最后我发现了一个更好的(我希望){ {3}}。我只是将推荐的代码包装在这样的短代码中:
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订阅者数量的位置即可。