【问题标题】:How to used aweber api to add new subscriber usign php and aweber OAuth 2.0 Examples如何使用 aweber api 使用 php 和 aweber OAuth 2.0 示例添加新订阅者
【发布时间】:2019-07-16 03:43:55
【问题描述】:

我在添加订阅者和 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() 为 null 堆栈跟踪:#0 {main} 在 D:\xampp\htdocs 中抛出\Aweber\index.php 第 30 行

【问题讨论】:

标签: php aweber


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多