首页 > 解决方案 > 如何使用 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

标签: phpaweber

解决方案


AWeber 示例使用了名为 Guzzle 的 PHP 第三方 HTTP 客户端。您需要先设置客户端,然后才能使用它。您可以在 AWeber 的 GitHub 上的代码示例中看到一个示例:

https://github.com/aweber/public-api-examples/blob/ea87b1f504cb97d9081a9ea9c8737ae9fd8838e3/php/manage-subscriber

注意创建客户端的调用:

// 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 是您在某处使用令牌初始化的变量。


推荐阅读