1
我想創建mailchimp類以便稍後使用,但是我有__constructor函數的問題。如果沒有構造,只要使用add($data, $list)
其工作正常,但與__constructor這只是輸出(int)0
,當它應該是(int)200
...MailChimp類使用__construct
有2張表選擇,添加,獲取和刪除功能...
捲曲運行而不必構造,當我結合構造添加功能,一切都運行得很好,問題是用構造
類文件:
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' => '[email protected]',
'status' => 'subscribed'
);
$mc = new mailchimp($data, 'list2');
var_dump($mc->add());
或者
$data = array(
'email' => '[email protected]'
);
$mc = new mailchimp($data, 'list1');
var_dump($mc->get());
這我不清楚你能澄清類失敗,並且樣品用? –
'$ mc = new mailchimp($ data,'list2');如果我執行這個沒有構造函數和移動構造函數內部函數添加自己,它的工作...像我用'add($ data, $ list2)'沒有構造函數,它的工作原理。 – Diamonte
[PHP cURL HTTP代碼返回0]的可能重複(http://stackoverflow.com/questions/10227879/php-curl-http-code-return-0) –