php - 如何使用 aweber api 添加新订阅者 ussign php 和 aweber OAuth 2.0 示例
问题描述
我在添加订阅者和 OAuth 2.0 示例中使用 aweber api 使用签名 php
require_once('aweber_api/aweber_api.php');
$body = [
'ad_tracking' => 'ebook',
'custom_fields' => [
'apple' => 'fuji',
'pear' => 'bosc'
],
'email' => 'anand@gmail.com',
'ip_address' => '192.168.1.1',
'last_followup_message_number_sent' => 0,
'misc_notes' => 'string',
'name' => 'Anand',
'strict_custom_fields' => 'true',
'tags' => [
'slow',
'fast',
'lightspeed'
]
];
$headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'User-Agent' => 'AWeber-PHP-code-sample/1.0'
];
$listId='myid';
$accountId='myid';
$url = "https://api.aweber.com/1.0/accounts/{$accountId}/lists/{$listId}/subscribers";
$response = $client->post($url, ['json' => $body, 'headers' => $headers]);
echo $response->getHeader('Location')[0];
错误代码 :
注意:未定义变量:第 30 行 D:\xampp\htdocs\Aweber\index.php 中的客户端
致命错误:未捕获错误:在 D:\xampp\htdocs\Aweber\index.php:30 中调用成员函数 post() 堆栈跟踪:#0 {main} 在 D:\xampp\htdocs\Aweber\ 中抛出第 30 行的 index.php
解决方案
AWeber 示例使用了名为 Guzzle 的 PHP 第三方 HTTP 客户端。您需要先设置客户端,然后才能使用它。您可以在 AWeber 的 GitHub 上的代码示例中看到一个示例:
注意创建客户端的调用:
// Create a Guzzle client
$client = new GuzzleHttp\Client();
Guzzle 的文档可以在这里找到:
http://docs.guzzlephp.org/en/stable/
看起来您还缺少授权标头。当您进行 API 调用时,除非您在标头中包含访问令牌,否则它将失败,所以不要忘记那部分!您可以将其添加到现有标题中,如下所示:
$headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'User-Agent' => 'AWeber-PHP-code-sample/1.0',
'Authorization' => 'Bearer ' . $accessToken
];
其中 $accessToken 是您在某处使用令牌初始化的变量。
推荐阅读
- javascript - 使用猫鼬填充过滤数据
- jquery - Django 表单 ChoiceField 允许通过 jquery 进行动态选择
- javascript - 将 HTML 内容读入 Javascript 字符串
- laravel - 无法上传视频文件
- python - 获取所需字母任一侧的字符串
- angular - 如何下载我的服务器(springboot)上生成的角度pdf文件?
- java - 试图在 android studio 中制作一个聊天应用程序,但我无法附加文本
- flutter - 有没有办法可以在一定的时间间隔内连续更改一些文本?
- python - 通过指定纬度和经度提取推文
- mysql - 查找列 device_id 的空率