mailjet / mailjet-apiv3-php-simple

[API v3] Simple PHP wrapper for the Mailjet API /!\ [DEPRECATED - SEE README] /!\
https://dev.mailjet.com
MIT License
55 stars 45 forks source link

Create and Add contact to list #70

Closed jonathan0211 closed 9 years ago

jonathan0211 commented 9 years ago

Hey guys I am creating a form to put in my website for users to sign in to my mailjet list. I am using php can you help me doing this. I am trying to use the functions in the readme but for example when I create the contact how can I get the ID information of the object returned to call AddContactToList($listID, $contactID)

arnaudbreton commented 9 years ago

Hi @jonathan0211,

Thanks for contacting us. Could you please show us the code you're using? Thanks!

jonathan0211 commented 9 years ago

<?php include("php-mailjet-v3-simple.class.php"); $email = $_POST["email"]; $contact = createContact ($email));

function createContact($Cemail) {
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "Email" => $Cemail
    );

    $result = $mj->contact($params);
    echo serialize ($result);
    if ($mj->_response_code == 201)
       echo "success - created contact";
    else
       echo "error - ".$mj->_response_code;

    return $result;
}
    function addContactToList($contactID, $listID) {
    $mj = new Mailjet();
    $params = array(
        "method" => "POST",
        "ContactID" => $contactID,
        "ListID" => $listID,
        "IsActive" => "True"
    );

    $result = $mj->listrecipient($params);

    if ($mj->_response_code == 201)
       echo "success - contact ".$contactID." added to the list ".$listID;
    else
       echo "error - ".$mj->_response_code;

    return $result;
}

?>

And then after creating the contact I would like to get the contact ID and pass to addContactToList($contactID, $listID) but I can't get the ID.

arnaudbreton commented 9 years ago

You can see all the result information by doing php var_dump(result). The ID is in $result->Data[0]->ID

jonathan0211 commented 9 years ago

Thanks a lot, It worked smoothly

arnaudbreton commented 9 years ago

Glad to hear! Could you please close this issue? Don't hesitate to open new issues for further questions. Best.

rplacom commented 9 years ago

Hi Arnaud,

I'm having problems with this API and maybe you can give a hand. Do you?

I used the same code for create the contact. The contact is created but I can't get the contact ID for put it on a contact list. Here my code:

$mj = new Mailjet($apiKey, $apiSecret);

$Cemail = 'test3@test.aaa';
$params = array(
    "method" => "POST",
    "Email" => $Cemail
);

$result = $mj->contact($params);

echo 'var dump:'.var_dump($result).'<br>';
echo 'ID:'.$result->Data[0]->ID.'<br>';
echo '<hr>';

if ($mj->_response_code == 201)
   echo "success - created contact ".$Cemail;
else
   echo "error - ".$mj->_response_code;

The result of this page is:

NULL var dump: ID: success - created contact test3@test.aaa

Do you know what I'm doing wrong? Seems that I have a NULL value before the var_dump, but I don't know why.

Thank you very much. Regards,

Ramon

fturk commented 9 years ago

this function permit get the id of email contact public function getIdContact_contact($s_contactEmail){ $params = array( "method" => "VIEW", "ID" => $s_contactEmail ); return $this->contact($params)->Data[0]->ID; }

arnaudbreton commented 9 years ago

Hi @rplacom,

In order to make it easier to track issues here, could you please open a new one since this one is already closed? Thanks!

Thanks for your help on this @fturk!

fturk commented 9 years ago

you're welcome @arnaudbreton !

WeshGuillaume commented 9 years ago

Hey @jonathan0211 :airplane:

We just released a new PHP API wrapper. It is now more PHP-ish (including namespace, PSR-0 compliance, and globally a better architecture). All it requires is PHP 5.4

In addition to our API Guides, we would be happy to help you get started with it.

Guillaume, from Mailjet!