我试图创建稍后使用的mailchimp类,但是我遇到了__constructor函数的问题。没有构造函数,只需使用add($data, $list)
它的工作正常,但使用__constructor这只输出(int)0
,当它应该是(int)200
...
有2个列表可供选择,添加,获取和删除功能......
Curl工作时没有构造函数,当我将构造函数组合起来添加函数时,一切正常,问题是构造函数
班级档案:
class mailchimp
{
private $apiKey = '<apiKey>';
private $list1 = 'listOneId';
private $list2 = 'listTwoId';
function __construct($data, $list)
{
$listId = $list === 'list1' ? $this->list1 : $this->list2;
$memberId = md5(strtolower($data['email']));
$dataCenter = substr($this->apiKey,strpos($this->apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;
}
function add() {
$json = json_encode([
'email_address' => $data['email'],
'status' => $data['status'], // "subscribed","unsubscribed","cleaned","pending"
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $this->apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $httpCode;
}
function get($email)
{
执行的文件(添加和获取):
include_once 'mailchimp.class.php';
$data = array(
'email' => 'my@example.com',
'status' => 'subscribed'
);
$mc = new mailchimp($data, 'list2');
var_dump($mc->add());
或
$data = array(
'email' => 'my@example.com'
);
$mc = new mailchimp($data, 'list1');
var_dump($mc->get());
答案 0 :(得分:1)
你想要阅读scoping in PHP,构造函数中的function get_filesize($url) {
$headers = get_headers($url, 1);
if (isset($headers['Content-Length'])) return $headers['Content-Length'];
if (isset($headers['Content-length'])) return $headers['Content-length'];
$c = curl_init();
curl_setopt_array($c, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('User-Agent: Mozilla/5.0
(Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.3)
Gecko/20090824 Firefox/3.5.3'),
));
curl_exec($c);
return curl_getinfo($c, CURLINFO_SIZE_DOWNLOAD);
}
}
$filesize = get_filesize("http://www.dailymotion.com/rss/user/dialhainaut/");
if($filesize<=10485760){
echo 'Fine';
}else{
echo $filesize.'File is too big';
}
不会超出它的功能范围。您需要将其作为属性放入对象中,以便在类中的其他函数中使用它。
我的意思的一个例子:
$url