【发布时间】:2013-08-02 10:37:57
【问题描述】:
我有以下 PHP“订阅”脚本,我正在使用它来允许用户注册我的 MailChimp 列表。
它按预期工作(将我的电子邮件地址添加到我的 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); -
还要在 json_decode() 之前检查是否有错误
-
if ($result === FALSE) { die('1'); } else { die('2'); }当我运行它时,我得到'2'。这是什么意思? -
表示调用成功,现在是
var_dump(json_decode($result));而不是die('2');,你会看到结果 -
$result出错时为假,否则有来自服务器的结果...