ActiveCampaign / activecampaign-api-php

MIT License
115 stars 76 forks source link

How do add a new contact using this wrapper? #35

Closed JoabMendes closed 8 years ago

JoabMendes commented 8 years ago

The example I found in the documentation uses raw curl to add new contacts. I did some reading on the classes and I believe there's a way to add contacts using this wrapper, but I couldn't find any example. The code I have is like this:

require_once 'includes/ActiveCampaign.class.php';

$API_URL = "myurl";
$API_KEY = "myapikey";

$ac = new ActiveCampaign($API_URL, $API_KEY);

$mycontactEmail = "email@example.com";
$mycontactName = "Name Example";

Since I have the object, could some help me telling how it would be to add a new contact? I will keep reading the code, but an example will make my job faster. :smiley:

mthommes commented 8 years ago

Hi, thanks for reaching out. We have an examples script that shows how to add contacts, along with other common requests (using this wrapper). Let us know of any questions! :+1:

JoabMendes commented 8 years ago

Hi @mthommes,

Now I am trying to get the ids of all my lists with this code:


//I am including the ActiveCampaign.class
//Also I defined the following constants in the config.php file

$ac = new ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY);

if (!(int)$ac->credentials_test()) {
    echo "<p>Access denied: Invalid credentials (URL and/or API key).</p>";
    exit();
}

//The test works already

$post_data = array(
    'api_action' => 'list_list',
    'api_key' => ACTIVECAMPAIGN_API_KEY,
    'api_output' => 'json',
    'ids' => 'all', 
);

var_dump($ac->api("list/list_", $post_data));

But I'm always get the error: Trying to get property of non-object#0 Do you have any ideia of what I might be doing wrong?

mthommes commented 8 years ago

Hi, your $post_data doesn't need to be included at all. In fact list_list is a GET method and doesn't require post data. Second, maybe try doing just this: "list/list" (instead of "list/list_" - remove the underscore).

Let us know if that doesn't work still! :smiley:

JoabMendes commented 8 years ago

Sadly, I still the the same error doing var_dump( $ac->api("list/list") );

JoabMendes commented 8 years ago

When I run this code to test the $ac object


$ac = new ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY);

if (!(int)$ac->credentials_test()) {
    echo "<p>Access denied: Invalid credentials (URL and/or API key).</p>";
    exit();
}

var_dump(get_class_methods($ac)); //output here

I get its methods

array(6) { [0]=> string(11) "__construct" [1]=> string(7) "version" [2]=> string(3) "api" [3]=> string(16) "credentials_test" [4]=> string(3) "dbg" [5]=> string(4) "curl" }

So I have no idea of what's happening since the including and the calls seem to be right.

mthommes commented 8 years ago

Try this:

$ac->api("list/list?ids=all");

Also set this (above the previous line):

$ac->debug = true;

JoabMendes commented 8 years ago

It works when I specified the parameter in $ac->api("list/list?ids=all");. I am getting all the lists. Also, the first error wasn't coming from the wrapper. The problem was from the definition of my framework that was wrong, sorry.

Thanks for your amazing support.

mthommes commented 8 years ago

Glad we could help! :+1: