2013-07-08 78 views
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我正在使用?

與此:-)誰看到任何助手

+1

$ data是一個數組......你想要的是檢查$ result === FALSE,然後如果它確實如此:'curl_error($ ch);' – We0

+0

同時檢查json_decode()之前是否有錯誤() – We0

+0

('1'); if($ result === FALSE){ \t die('1'); ('2');其他{ } else { \t die('2'); '當我運行這個時,我得到'2'。這是什麼意思? – michaelmcgurk

回答

0

對不起已故的答覆,只是讓任何人都非常感謝用戶使用這是知道:

你,因爲變量你想收到此錯誤使用不是一個「對象」 看到http://php.net/manual/en/language.oop5.php

$數據在這種情況下是一個數組。對於任何使用類和對象的人,一定要開始你的課程,或者全球一個已經開始的課程。

例如。

$object = new class(); 

OR

global $object; 

如果$對象已被宣佈爲任何包含的文件。

相關問題