How to used aweber api to add new subscriber usign php and aweber OAuth 2.0 Examples

150

i am used aweber api in add subscriber and OAuth 2.0 Examples usign php

require_once('aweber_api/aweber_api.php');
$body = [
  'ad_tracking' => 'ebook',
  'custom_fields' => [
    'apple' => 'fuji',
    'pear' => 'bosc'
  ],
  'email' => '[email protected]',
  '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];

Error Code :

Notice: Undefined variable: client in D:\xampp\htdocs\Aweber\index.php on line 30

Fatal error: Uncaught Error: Call to a member function post() on null in D:\xampp\htdocs\Aweber\index.php:30 Stack trace: #0 {main} thrown in D:\xampp\htdocs\Aweber\index.php on line 30

405

Answer

Solution:

The AWeber examples make use of a third party HTTP client for PHP called Guzzle. You need to set up the client first before you'll be able to use it. You can see an example of this in the code examples on AWeber's GitHub here:

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

Note the call to create the client:

// Create a Guzzle client
$client = new GuzzleHttp\Client();

Documentation for Guzzle can be found here:

http://docs.guzzlephp.org/en/stable/

It also looks like you're missing the authorization header. When you make the API call it will fail unless you include your access token in the header, so don't forget that part! You can add it to your existing headers like so:

$headers = [
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'User-Agent' => 'AWeber-PHP-code-sample/1.0',
    'Authorization' => 'Bearer ' . $accessToken
];

Where $accessToken is a variable you initialize with your token somewhere.

People are also looking for solutions to the problem: php - Should I use truthy approach rather than is_null, isset, or empty?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.