parsing a response

i am trying to get the Email field of 5 subscribers in a list using this code.

if($result->was_successful()) {
    foreach ($result->response as $value) {
        echo $result->response->EmailAddress;
        echo '<br/>';
        echo 'A';
    }

and i am getting eight A (as the number of subscribers) but it seems the code i use to get the email (echo $result->response->EmailAddress;) is not returning anything. Any ideas on how to loop through the results and get the values?

Another general question that i have is which files/folders do i need to keep from the PHP API Wrapper so that i have all the functionality but not the test/samples files?

Thank you in advance

tobyb tobyb, 5 years ago

Hey mailcloud,

I'm assuming that your getting active subscribers or at least something that returns a result of the same type. If not and your still having trouble let us know which route or sample your accessing and we can look at that specifically.

As shown in the documentation, the response returned when getting active subscribers is paged and contains both the subscribers on the current page and additional paging details. When parsing the subscribers returned you want to look at the Results property, which will be an array in PHP. Based on the code you've provided you can iterate through these results like this:

if($result->was_successful()) {
    foreach ($result->response->Results as $value) {
        echo $value->EmailAddress;
        echo '<br/>';
        echo 'A';
    }
}

Each of the samples displays the structure of the response, showing which properties are available to a developer. This structure mimics the returned JSON, so the documentation is always a good place to start when checking what data is available.

As for which files are required, everything within the samples and tests folders can be safely removed, however everything else is required.

jamesd jamesd, 5 years ago

It would be more helpful in resolving the issue you are experiencing if you provided more context - including which method in the PHP library you are calling to get $result?

You would probably do well to try this first too, which will show you exactly what you are getting in the response:

var_dump($result->response);
mailcloud, 5 years ago

thank you guys for the timely responses.

I have solved the issue of getting the information of an active subscriber. However i have two small issues.

1. How can i access the data stored on custom fields?
2. How can i access the name of fields and custom fields?

A little suggestion that i have, is to have a section on the forum with little snippets on how to accomplish basic stuff (e.g. get subscribers Information) so that people can look on those and use them as little building blocks in order to create something more complex.

aviationattorney0, 5 years ago

The response returned when getting active subscribers is paged and contains both the subscribers on the current page and additional paging details.

jamesd jamesd, 5 years ago

Answers:

1. Custom field data for a subscriber is included when getting subscribers:

{
  "EmailAddress": "subs+7t8787Y@example.com",
  "Name": "Person One",
  "Date": "2010-10-25 10:28:00",
  "State": "Active",
  "CustomFields": [
    {
      "Key": "website",
      "Value": "http://example.com"
    },
    {
      "Key": "age",
      "Value": "24"
    },
    {
      "Key": "subscription date",
      "Value": "2010-03-09"
    }
  ]
}

2. You can retrieve the custom fields defined for any list:

[
  {
    "FieldName": "website",
    "Key": "[website]",
    "DataType": "Text",
    "FieldOptions": []
  },
  {
    "FieldName": "age",
    "Key": "[age]",
    "DataType": "Number",
    "FieldOptions": []
  },
  {
    "FieldName": "subscription date",
    "Key": "[subscriptiondate]",
    "DataType": "Date",
    "FieldOptions": []
  }
  {
    "FieldName": "newsletterformat",
    "Key": "[newsletterformat]",
    "DataType": "MultiSelectOne",
    "FieldOptions": [
      "HTML",
      "Text"
    ]
]
mailcloud, 5 years ago

I have create a list with the following fields

email = 'asdasd@asd.com'
alias = 'bob the builder' [custom field]
dob = '2008/01/01' [custom field]

I want to get all the information for the active subscribers for this list so i run the getActiveSubscribers example in the PHP wrapper and i have managed to get the email (e.g echo the email of each person) but i cant access the custom fields. This is the code i use.

$wrap = new CS_REST_Lists($ListID, $MailCloud_API);
$result = $wrap->get_active_subscribers('2011-01-01', 1, 1000, 'email', 'asc');
if($result->was_successful()) {
    foreach ($result->response->Results as $value) {
                echo $value->EmailAddress;
        echo '<br/>';
        echo $value->alias;
        echo '<br/>';
        echo $value->dob;
        echo '<br/>';
    }

any ideas?

thank you in advance

mailcloud, 5 years ago

Another question that i have is if there is an API call to get the count of active subscribers

Thank you

jamesd jamesd, 5 years ago

If you run the complete sample code provided for getting active subscribers, you will be able to see the structure of the result returned by inspecting the output of the call to var_dump.

You can get the total number of active subscribers for the request you have made using the TotalNumberOfRecords property of the result (assuming the date argument you have passed does not limit the total number of records).

Or, the number of active subscribers on a list is available in the result when you get list stats.

mailcloud, 5 years ago

thank for the response Jamesd. That is what i finally did and worked perfectly..

Join 200,000 companies around the world that use Campaign Monitor to run email marketing campaigns that deliver results for their business.

Get started for free
1-888-533-8098