我正在尝试将用户添加到我在Mailchimp中创建的列表中,但我无法在任何地方找到任何代码示例.我已经尝试弄清楚如何使用API,但我非常关注"看一个例子并学习"这类人.
我已经尝试使用API的第2版但是似乎没有任何工作,尽管在网上的例子工作,Mailchimp在他们的网站上说下面关于他们的API的早期版本:
不推荐使用版本2.0及更早版本.只有最小的支持错误修复,安全补丁 - 将适用于这些版本.
更新1:我根据TooMuchPete 关于管理订户的链接的答案做了一些进一步的研究,并修改了我在这里找到的一些代码,但它不起作用,因为函数http_build_query()不处理嵌套数组.我不知道如何处理添加订阅者的'merge_fields'部分.我目前的代码如下:
$postdata = http_build_query(
array(
'apikey' => $apikey,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $name
)
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://us2.api.mailchimp.com/3.0/lists/<list_id>/members/', false, $context);
var_dump($result);
die('Mailchimp executed');
Run Code Online (Sandbox Code Playgroud)
更新2:我现在使用卷曲,我已经设法得到一些几乎工作的东西.数据发送到Mailchimp但我收到错误"您的请求不包含API密钥".我猜这里需要进行身份验证.我已经尝试将它添加到http标头,但尚未奏效.请参阅下面的代码:
$apikey = '<api_key>';
$auth = base64_encode( 'user:'.$apikey );
$data = …
Run Code Online (Sandbox Code Playgroud) 我在下面的代码中将用户添加到Mailchimp中的预先存在的列表中.
$apikey = '<api_key>';
$auth = base64_encode( 'user:'.$apikey );
$data = array(
'apikey' => $apikey,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $name
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://us2.api.mailchimp.com/3.0/lists/<list_id>/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
var_dump($result);
die('Mailchimp executed');
Run Code Online (Sandbox Code Playgroud)
此代码仅将用户添加到列表中,当我尝试将同一用户的详细信息添加两次时,它会在第二次尝试时引发以下错误:
test@user.com已经是列表成员.使用PATCH更新现有成员.
如何使用PATCH更新用户详细信息?我不知道在哪里指定它.
在进入代码之前,让我解释一下我的目标。我的网络应用程序显示待售车辆。如果用户尝试访问不存在的页面,我需要一个自定义 404 页面,该页面将显示添加到数据库中的 12 辆最新车辆。
我有以下...
App\Exceptions\CustomException.php
<?php
namespace App\Exceptions;
use Exception;
class CustomException extends Exception
{
public function __construct()
{
parent::__construct();
}
}
Run Code Online (Sandbox Code Playgroud)
App\Exceptions\CustomHandler.php
<?php
namespace App\Exceptions;
use Exception;
use App\Exceptions\Handler as ExceptionHandler;
use Illuminate\Contracts\Container\Container;
use App\Project\Frontend\Repo\Vehicle\EloquentVehicle;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Support\Facades\View;
class CustomHandler extends ExceptionHandler
{
protected $vehicle;
public function __construct(Container $container, EloquentVehicle $vehicle)
{
parent::__construct($container);
$this->vehicle = $vehicle;
}
/**
* Report or log an exception.
*
* This is a great spot to send exceptions to Sentry, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用PDO从我的数据库中选择信息.直到->prepare()->execute()
方法正常工作的一切.转储响应时,我得到一个布尔值true响应.
但是一旦我使用->fetchAll()
我就会收到以下错误:
致命错误:在布尔值中调用成员函数fetchAll()....
我不确定为什么会这样.我应该在结果中收到一行.代码如下:
$host = 'localhost';
$db = 'database';
$user = 'useername';
$pass = 'password';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$connection = new PDO($dsn, $user, $pass, $opt);
$connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$query = 'SELECT * FROM gen_dealers WHERE dealer = ?';
$result = $connection->prepare($query)->execute(['General Motors']);
echo $result; // returns 1
$result->fetchAll(PDO::FETCH_UNIQUE); // returns the Fatal error mentioned above
Run Code Online (Sandbox Code Playgroud)