Updating Subscribers Using API.

Hi All,

I need to update lots of email addresses that are our on one of our subscribers lists. I was hoping the import function would achieve this but instead of updating an email address linked to a name it creates a new record with the new email address..... bummer.

So, I set about learning a bit more about PHP. I have developed a script below that will update the records correctly:

<?php

$updatelist["test1@test.com"] = "test2@test.com";
$updatelist["test3@test.com"] = "test4@test.com";
$updatelist["test5@test.com"] = "test6@test.com";

foreach( $updatelist as $oldemail => $newemail)
    {
        $wrap = new CS_REST_Subscribers('---ListID----', '---ApiKey---');
        $result = $wrap->update($oldemail, array('EmailAddress' => $newemail,));
        echo "$oldemail, trying to replace with: $newemail... <br />";
        if($result->was_successful())
            {
                echo "Subscribed with code ".$result->http_status_code."\n<br />";
            }
        else
            {
                echo 'Failed with code '.$result->http_status_code."\n<br /><pre>";
                var_dump($result->response);
                echo '</pre>';
            }
    }

?>

Okay so that's great but I need to change around 200 - 300 email addresses that we have update elsewhere.... I am able to produce a CSV file as follows:

oldemail1@email.com,newemail1@email.com
oldemail2@email.com,newemail2@email.com
oldemail3@email.com,newemail3@email.com
...
oldemailXX@email.com,newemailXX@email.com


So I then wrote a script to put these in to import these in to an array:

<?php

$file = fopen('addresses.csv', 'r');
$header = array("oldemail","newemail");
$data = array();


while (($line = fgetcsv($file)) !== false)
{
    $data = array_combine($header,$line);
    $updatelist[] = $data;
}

fclose($file);

echo '<pre>';
print_r($updatelist);
echo '</pre>';

?>

Which produces the following nested array:

Array
(
    [0] => Array
        (
            [oldemail] => oldemail1@test.com
            [newemail] => newemail1@test.com
        )

    [1] => Array
        (
            [oldemail] => oldemail2@test.com
            [newemail] => newemail2@test.com
        )

    [2] => Array
        (
            [oldemail] => oldemail3@test.com
            [newemail] => newemail3@test.com
        )

    [3] => Array
        (
            [oldemail] => oldemail4@test.com
            [newemail] => newemail4@test.com
        )

)

I then tried it in the following full script:

<?php
require_once '../../csrest_subscribers.php';
$file = fopen('addresses.csv', 'r');
$header = array("oldemail","newemail");
$data = array();

while (($line = fgetcsv($file)) !== false)
{
    $data = array_combine($header,$line);
    $updatelist[] = $data;
}

fclose($file);


foreach($updatelist as $array_key=>$array_data)
{
    foreach( $array_data as $oldemail => $newemail)
    {
        $wrap = new CS_REST_Subscribers('---ListID---', '---ApiKey---');
        $result = $wrap->update($oldemail, array('EmailAddress' => $newemail,));
        echo "$oldemail, trying to replace with: $newemail... <br />";
        if($result->was_successful())
            {
                echo "Subscribed with code ".$result->http_status_code."\n<br />";
            }
        else
            {
                echo 'Failed with code '.$result->http_status_code."\n<br /><pre>";
                var_dump($result->response);
                echo '</pre>';
            }
    }
}

?>

But of course this doesn't work as the 'imported' array is not properly formed - I don't know how to either 1. import a two column CSV to Key and Values in an array or 2. use the imported array as it is and change the way the valuables are handled...

Any pointers would be muchly appreciated!!

Thanks,

Chris

Ken Ken, 3 years ago

Hi Chris,

When you run your script, and look at the output that comes from this line:

echo "$oldemail, trying to replace with: $newemail... <br />";

it will be something like:

oldemail, trying to replace with: oldemail1@test.com...
newemail, trying to replace with: newemail1@test.com...
oldemail, trying to replace with: oldemail2@test.com...
newemail, trying to replace with: newemail2@test.com...
oldemail, trying to replace with: oldemail3@test.com...
newemail, trying to replace with: newemail3@test.com...
oldemail, trying to replace with: oldemail4@test.com...
newemail, trying to replace with: newemail4@test.com... 

ie. The $oldemail is either 'oldemail' or 'newemail'. Basically you don't want this array structure:

Array
(
    [0] => Array
        (
            [oldemail] => oldemail1@test.com
            [newemail] => newemail1@test.com
        )

    [1] => Array
        (
            [oldemail] => oldemail2@test.com
            [newemail] => newemail2@test.com
        )

    [2] => Array
        (
            [oldemail] => oldemail3@test.com
            [newemail] => newemail3@test.com
        )

    [3] => Array
        (
            [oldemail] => oldemail4@test.com
            [newemail] => newemail4@test.com
        )

)

but one like this:

Array
(
    [oldemail1@test.com] => newemail1@test.com
    [oldemail2@test.com] => newemail2@test.com
    [oldemail3@test.com] => newemail3@test.com
    [oldemail4@test.com] => newemail4@test.com
)

Here's what your whole script should be:

<?php
require_once 'campaignmonitor-createsend-php-e9bf187/csrest_subscribers.php';
$file = fopen('addresses.csv', 'r');
$header = array("oldemail","newemail");
$data = array();

while (($line = fgetcsv($file)) !== false)
{
    $updatelist[$line[0]] = $line[1];
}

fclose($file);

echo '<pre>';
print_r($updatelist);
echo '</pre>';



foreach( $updatelist as $oldemail => $newemail)
{
    $wrap = new CS_REST_Subscribers('---ListID---', '---ApiKey---');
    $result = $wrap->update($oldemail, array('EmailAddress' => $newemail,));
    echo "$oldemail, trying to replace with: $newemail... <br />";
    if($result->was_successful())
        {
            echo "Subscribed with code ".$result->http_status_code."\n<br />";
        }
    else
        {
            echo 'Failed with code '.$result->http_status_code."\n<br /><pre>";
            var_dump($result->response);
            echo '</pre>';
        }
}


?>

I hope this helps. Feel free to get back to me if you have any other problems or questions.

Ken


http://www.campaignmonitor.com
Ken Ken, 3 years ago

Oh also, your API Key is your private key to make API calls to your Campaign Monitor account. It's best not to post it on a public site :).


http://www.campaignmonitor.com
Chris Sargent, 3 years ago

Hi Ken,

Thanks for that - I did actually just solve it in a slightly different way this morning, taking out the inner for loop and using an array key identifier as the variable rather than the key itself as follows:

$result = $wrap->update($array_data[oldemail], array('EmailAddress' => $array_data[newemail],));

Your way would seem a bit more efficient as the array is simpler - what you described is exactly what I wanted to achieve but I couldn't find any info anywhere about importing a CSV in to Key,Value pairs.

Thanks very much for the help!!

I totally meant to take off my details too but was in a bit of a rush when copying all the code over, thanks for removing them!

Chris

Join 150,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