Getresponse API 2 (Adding Custom fields and contacts using PHP)

限于喜欢 提交于 2019-12-30 05:27:04

问题


Im new to coding and web development as it is and diving into the deep end with API's is a thing i wish i never had done! However being said i have progressed further than expected. I am now having problems when trying to add custom fields to the add contact feature. Im trying to get the code to add the hidden form input fields when the user hits my thankyou page. I dont want to use Getresponses own Form builder for my main page so it was better to use the API. I have the code running perfectly when it comes to just adding the contact however when i add the set_contact_customs the code does not execute and fails with the following error: (Request have return error: Array) So i understand its to do with the set_contact_customs array however im clueless as to what it is i have done wrong.. Any advice and help is greatly appreciated as i am still learning the basics so picking up on what you experts say is a great learning curve. Thanks.

--- Below is the working version without the set_contact_customs ----

<?php
// Add contact to selected campaign id
try{
$result_contact = $client->add_contact(
$api_key,
array (
'campaign' => 'My-Camp-ID',
'name' => $fullname,
'email' => $emailaddress
)
);

echo "<p style='color: blue; font-size:24px;'>No Errors, Contact and Custom Fields have been added...</p>";
}

catch (Exception $e) {

echo $e->getMessage();
}

?>

--- Here is the code that causes the problems (with set_contact_customs) ----

    <?php
// Add contact to selected campaign id
try{
$result_contact = $client->add_contact(
$api_key,
array (
'campaign' => 'My-Camp-ID',
'name' => $fullname,
'email' => $emailaddress
)
);
$result_contact = $client->set_contact_customs(
        $api_key,
            array(
                'Survey Type' => $surveytype,
                'Survey Cost' => $surveycost
                )
);
echo "<p style='color: blue; font-size:24px;'> Contact Added </p>";
}

catch (Exception $e) {

echo $e->getMessage();
}

?>

回答1:


  1. API 2 doesn't really exist: in GetResponse they say version "1.5.0 - this is last JSON/RPC version of our API", especially if you were speaking 10 months ago. Now they are preparing to beta-test v3. So I will assume you were speaking about 1.5 and answer about it (I'm not familiar with v3, maybe there it's different).

  2. You must send contact id with set_contact_customs, and you didn't.

  3. When it says, "request error: array", it doesn't relate to your array (even though the problem is in your array, because you don't send in it contact id), they are sending an array as a response with error messages.

  4. I'd love to tell you, where to get the contact id in order to send it, but I'm looking for it myself now. :)

UPDATE:

Ok, I combined it from pieces all over the internet, and now here's the working format.

  1. You don't need to add_contact and then update it, you can do it in one go, adding the 'customs' parameter to the add_contact call (GR say, that we shouldn't expect for the contact to be added immediately, so you might not even get whom to update, if you call that function right away).

    The fields for add_contact are described here.

  2. The 'customs' parameter should look differently. Instead of:

    array(
        'Survey Type' => $surveytype,
        'Survey Cost' => $surveycost
        )
    

    it should be:

    array(
        array( 'name' => 'Survey Type', 'content' => $surveytype ),
        array( 'name' => 'Survey Cost', 'content' => $surveycost )
        )
    

    By the way, from what I tested, - blessedly, you don't need to define in GR UI those custom fields first, whatever you send, will be added or updated (in their limits for the custom field names and values).

    I got error, when tried to send one custom field with empty content, when calling add_contact. When I sent it with set_contact_customs, I didn't get any error; I wanted to see, if it would delete the field or field value - it didn't do a thing.

  3. If you still wish to update the existing contact, here's how to send the contact id with the update call:

    $result = $client->set_contact_customs(
       $api_key, array(
          'contact' => $contact_id,
          'customs' => $custom_fields_array
        )
    );
    
  4. To first find contact id, you should call get_contacts. And since it's been said (I haven't tested it), that in different campaigns contacts with the same email address have different contact id, you should pass both the campaign, and the email with it.

    As you can see, campaign can be sent in 'campaigns' parameter (then campaign id, that you got for add_contact, should be used), or in 'get_campaigns' (then the campaign name or even prefix can be used).

    Here's the call with campaign id, for your code:

    $result = $client->get_contacts(
        $api_key, array(
           'campaigns' => array( 'My-Camp-ID' ),
           'email' => array( 'EQUALS' => $emailaddress )
        )
    );
    
  5. To retrieve contact id from get_contacts, do the same as recommended for retrieving campaign id:

    $contact_id = array_pop( array_keys( $result ) );
    if ( empty( $contact_id ) ) {
        //still not ok
    }
    else {
        //you can call set_contact_customs
    }
    
  6. In order for that error message to be more descriptive, instead of just 'Request have return error: Array', open your jsonRPCClient.php, which you most surely include in your file with these GR function calls, and look for the following line:

    !is_null($response['error']) => 'Request have return error: ' . $response['error'],
    

    and replace it with the following, at least:

    !is_null($response['error']) => 'Request have returned error: ' . var_export($response['error'], true),
    

    Now your code will use the beloved var_export function and if you make a mistake, you will see in your error log something like:

    Request have returned error: array (
      'message' => 'Invalid params',
      'code' => -32602,
    )
    

I dedicate this thorough answer to all those, who helped me endlessly here on StackOverflow, just giving their answers to someone else's questions, sometimes years ago. Thank you! Hopefully my answer will save someone time, efforts, and mood, too. :)



来源:https://stackoverflow.com/questions/31486052/getresponse-api-2-adding-custom-fields-and-contacts-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!