phpfui / ConstantContact

MIT License
16 stars 7 forks source link

Get all contacts that have pagination #15

Closed handoyo closed 12 months ago

handoyo commented 12 months ago

Hi,

first of all thanks a lot for the library.

I'm trying to get all contacts by using this code. I have 3 contacts for testing purpose. `$contactsResponse = $contactsEndPoint->get('all',null,$list_id,null,null,null,null,null,null,null,null,null,null,true,1);

    do {
        $contactsResponse = $contactsEndPoint->next();
        print_r(contactsResponse);
    } while ($contactsResponse);`

I'm getting 504 document error and i'm only get the first and the second contact but no on the third contact.

What is the correct way to use the next()?

Thanks a lot.

phpfui commented 12 months ago

I believe you get an array from the get request that is paginated. So if you have more than one page, you would need to call next(). But in your case, you just have 3 things, so not paginated.

So try printing the $contactsResponse before calling next. That should contain an array that you can foreach through to get what you want.

Let me know if that does not work.

handoyo commented 12 months ago

In the get i set the limit to 1, hoping to get the other 2.

if i set the limit to 500, i get all of the contacts without issue and there are no next.

phpfui commented 12 months ago

This is the logic I am using for getting all contacts. I wrote all this a while ago, so not sure how everything works!

// Get all current Constant Contact users
$contactsClient = new \PHPFUI\ConstantContact\V3\Contacts($client);
$list = $contactsClient->get(status: 'all', limit: 500);
do
    {
    foreach ($list['contacts'] as $contact)
        {
        print_r($contact);
        }
    $list = $contactsClient->next();
    }
while ($list);

You seem to have a lot of parameters on the get() call. Maybe use named parameters? You could easily be off by one.

handoyo commented 12 months ago

Thanks a lot for the example, i will give it a test.

Do you have examples for adding contacts, subscribe, unsubscribe and re-subscibe as well?

phpfui commented 12 months ago

Actually I do have some examples! Check out my Bicycle Club Website repo: https://github.com/phpfui/BicycleClubWebsite2023

Looks like the good stuff is here: https://github.com/phpfui/BicycleClubWebsite2023/blob/master/App/Cron/Job/ConstantContactSync.php

Let me know if you get it working.

handoyo commented 12 months ago

Thanks, I will look into it and let you know the results.

handoyo commented 12 months ago

This is the logic I am using for getting all contacts. I wrote all this a while ago, so not sure how everything works!

// Get all current Constant Contact users
$contactsClient = new \PHPFUI\ConstantContact\V3\Contacts($client);
$list = $contactsClient->get(status: 'all', limit: 500);
do
  {
  foreach ($list['contacts'] as $contact)
      {
      print_r($contact);
      }
  $list = $contactsClient->next();
  }
while ($list);

You seem to have a lot of parameters on the get() call. Maybe use named parameters? You could easily be off by one.

i have tested using this code and set the limit to 1. i only get 1 contact and it's keep looping and then i get 504 Gateway error..

Thanks.

phpfui commented 12 months ago

Not sure at this point. You will probably need to talk to CC support to see if they have any idea of what is happening.

If you look into the client class code, I think you should be able to see the actual URLs and data sent to CC. They don't support any libraries just to make things super difficult.

handoyo commented 12 months ago

I give another test by calling the $contactsClient->next(); twice and i get all of the other 2 contacts.

so i use this approach:

$contactsResponse = $contactsEndPoint->get(status: 'all',include_count: true,lists: $list_id, limit: 1);
        $counts = $contactsResponse['contacts_count'];
        $pages =  1;
        if ($counts > 1) {
            $pages = intdiv($counts,1);
        }

        if ($pages > 1) {
            for ($i = 2; $i <= $pages; $i++) {
                print_r($contactsEndPoint->next());
            }
        }

It is working for me. maybe you have a better way?

phpfui commented 12 months ago

Looks like it would work. I would use a bigger limit and iterate through the returned array of contacts. That would minimize the number of API calls you are making. I believe CC has a rate limited API, so you can't call them too often. I think I added a rate limiter into the client, so it should not be a problem. Still not sure why you are getting a gateway error.

phpfui commented 12 months ago

Yes, the issue is this line:

$contactBody->email_address = $data['email'];

should be:

$contactBody->email_address = $email;

You want to assign the AddressPost object to the email_address member of $contactBody, not the email string.

I should have detected this in the assignment since I know the type. I will see if I can correct and issue a better error message. This will avoid this issue in the future.

Thanks for the heads up.

Bruce

On Thu, Sep 28, 2023 at 6:38 AM yonghan79 @.***> wrote:

I have update my code to use 500 as the limit.

Another thing, i copied your code on create contact like this:

    $contactBody = new \PHPFUI\ConstantContact\Definition\ContactPostRequest();
    $contactBody->first_name = $data['firstname'];
    $contactBody->last_name = $data['lastname'];
    $contactBody->create_source = 'Account';
    $contactBody->list_memberships = [new \PHPFUI\ConstantContact\UUID($list_id)];
    $email = new \PHPFUI\ConstantContact\Definition\EmailAddressPost();
    $email->address = $data['email'];
    $email->permission_to_send = 'explicit';
    $contactBody->email_address = $data['email'];
    $contactsEndPoint->post($contactBody);

I get this error:

Unknown Error: AbanteCart core v.1.3.4 PHPFUI\ConstantContact\Definition\Base::__construct(): Argument #1 ($initialValues) must be of type array, string given, called in /home/php81abdev7/public_html/ctct/extensions/constantcontact_integration/core/lib/ctct/vendor/phpfui/constantcontact/src/ConstantContact/Definition/Base.php on line 177 in /home/php81abdev7/public_html/ctct/extensions/constantcontact_integration/core/lib/ctct/vendor/phpfui/constantcontact/src/ConstantContact/Definition/Base.php on line 57

Did i miss anything?

— Reply to this email directly, view it on GitHub https://github.com/phpfui/ConstantContact/issues/15#issuecomment-1738898266, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABYW6S4DLYP5KHY2VF7BBILX4VHT5ANCNFSM6AAAAAA5H7OUCA . You are receiving this because you commented.Message ID: @.***>

phpfui commented 12 months ago

There is now a new version that fixes the assignment of the wrong type to properties that should be an object.

Closing this as answered. Open a new issue if you find another issue.