0
我有以下用於允許用戶註冊我的MailChimp列表的PHP'Subscription'腳本。PHP錯誤 - 「試圖獲取非對象的屬性」
它按預期工作(將我的電子郵件地址添加到我的MailChimp列表中),但我也在提交時遇到錯誤。
這是我的錯誤:
Notice: Trying to get property of non-object in
E:\domains\*******\subscribe.php on line 29
線29是:if ($data->error){
這裏是我的代碼:
<?php
$apiKey = '*********************************';
$listId = '*******************';
$double_optin=true;
$send_welcome=false;
$email_type = 'html';
$email = $_POST['email'];
//replace us2 with your actual datacenter
$submit_url = "http://us5.api.mailchimp.com/1.3/?method=listSubscribe";
$data = array(
'email_address'=>$email,
'apikey'=>$apiKey,
'id' => $listId,
'double_optin' => $double_optin,
'send_welcome' => $send_welcome,
'email_type' => $email_type
);
$payload = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $submit_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($payload));
$result = curl_exec($ch);
curl_close ($ch);
$data = json_decode($result);
if ($data->error){
echo $data->error;
} else {
echo "Thanks. You will receive an email shortly to confirm your subscription.";
}
它是否有助於如果我還提供了HTML表單代碼和AJAX我正在使用?
與此:-)誰看到任何助手
$ data是一個數組......你想要的是檢查$ result === FALSE,然後如果它確實如此:'curl_error($ ch);' – We0
同時檢查json_decode()之前是否有錯誤() – We0
('1'); if($ result === FALSE){ \t die('1'); ('2');其他{ } else { \t die('2'); '當我運行這個時,我得到'2'。這是什麼意思? – michaelmcgurk