Getresponse API 2(使用 PHP 添加自定义字段和联系人) [英] Getresponse API 2 (Adding Custom fields and contacts using PHP)

查看:19
本文介绍了Getresponse API 2(使用 PHP 添加自定义字段和联系人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编码和 Web 开发的新手,深入研究 API 是我希望我从未做过的事情!然而,据说我的进步比预期的要大.我现在在尝试将自定义字段添加到添加联系人功能时遇到问题.当用户点击我的谢谢页面时,我试图获取代码以添加隐藏的表单输入字段.我不想为我的主页使用 Getresponses 自己的表单构建器,所以最好使用 API.我的代码在添加联系人时运行良好,但是当我添加 set_contact_customs 时,代码不会执行并失败并出现以下错误:(请求有返回错误:数组)所以我理解它与 set_contact_customs 数组有关但是,我对我做错了什么一无所知..非常感谢任何建议和帮助,因为我仍在学习基础知识,因此了解您专家所说的内容是一个很好的学习曲线.谢谢.

--- 下面是没有 set_contact_customs 的工作版本----

add_contact($api_key,大批 ('运动' =>'我的营地ID','名称' =>$全名,'电子邮件' =>$电子邮件地址));echo "<p style='color: blue; font-size:24px;'>未添加任何错误、联系人和自定义字段...</p>";}捕获(异常 $e){echo $e->getMessage();}?>

--- 这是导致问题的代码(使用 set_contact_customs)----

 add_contact($api_key,大批 ('运动' =>'我的营地ID','名称' =>$全名,'电子邮件' =>$电子邮件地址));$result_contact = $client->set_contact_customs($api_key,大批('调查类型' =>$调查类型,调查成本"=​​>$调查成本));echo "<p style='color: blue; font-size:24px;'> 添加联系人 </p>";}捕获(异常 $e){echo $e->getMessage();}?>

解决方案

  1. API 2 实际上并不存在:在 GetResponse 他们说 版本"1.5.0 - 这是我们 API 的最后一个 JSON/RPC 版本",特别是如果您在 10 个月前发言.现在他们正准备对 v3 进行 beta 测试.所以我会假设你说的是 1.5 并回答它(我不熟悉 v3,也许它有所不同).

  2. 您必须使用 set_contact_customs 发送联系人 ID,而你没有.

  3. 当它说请求错误:数组"时,它与您的数组无关(即使问题出在您的数组中,因为您没有发送联系人 ID),他们正在发送一个数组作为带有错误消息的响应.

  4. 我很想告诉您,从哪里获取联系人 ID 以便发送它,但我现在正在自己寻找.:)

更新:

好的,我从互联网上的各个部分组合而成,现在是工作格式.

  1. 你不需要add_contact然后更新它,你可以一次性完成,将'customs'参数添加到add_contact调用中(GR说,我们不应该期望联系人立即添加,因此如果您立即调用该函数,您甚至可能不知道要更新谁).

    此处描述了 add_contact 的字段.>

  2. 'customs' 参数看起来应该有所不同.而不是:

    数组('调查类型' =>$调查类型,调查成本"=​​>$调查成本)

    应该是:

    数组(array( 'name' => 'Survey Type', 'content' => $surveytype ),数组('名称' => '调查成本','内容' => $surveycost ))

    顺便说一下,根据我的测试,幸运的是,您无需在 GR UI 中首先定义这些自定义字段,无论您发送什么,都将添加或更新(在自定义字段名称和值的限制中)).

    我在调用 add_contact 时尝试发送一个内容为空的自定义字段时出错.当我用 set_contact_customs 发送时,我没有收到任何错误;我想看看它是否会删除字段或字段值 - 它没有做任何事情.

  3. 如果您仍希望更新现有联系人,以下是通过更新调用发送联系人 ID 的方法:

    $result = $client->set_contact_customs($api_key, 数组('联系' =>$contact_id,'海关' =>$custom_fields_array));

  4. 要首先查找联系人 ID,您应该调用 get_contacts.既然有人说过(我没有测试过),在不同的活动中,具有相同电子邮件地址的联系人具有不同的联系人 ID,您应该同时传递活动和带有它的电子邮件.

    正如您可以看到的,活动可以发送'campaigns' 参数(然后是您为 add_contact 获得的活动 ID,应该使用),或在 'get_campaigns' 中(然后可以使用活动名称甚至前缀).

    这是带有广告系列 ID 的调用,用于您的代码:

    $result = $client->get_contacts($api_key, 数组('广告系列' =>数组('我的营地ID'),'电子邮件' =>数组('EQUALS' => $emailaddress )));

  5. 要从 get_contacts 中检索联系人 ID,请按照检索广告系列 ID 的建议操作:

    $contact_id = array_pop( array_keys( $result ) );如果(空($contact_id)){//还是不行}别的 {//你可以调用set_contact_customs}

  6. 为了使该错误消息更具描述性,而不仅仅是请求有返回错误:数组",请打开您的 jsonRPCClient.php,您肯定会将其包含在具有这些 GR 函数调用的文件中,并且查找以下行:

    !is_null($response['error']) =>'请求有返回错误:'.$响应['错误'],

    并至少将其替换为以下内容:

    !is_null($response['error']) =>'请求已返回错误:'.var_export($response['error'], true),

    现在你的代码将使用心爱的 var_export 函数,如果你犯了错误,你会在错误日志中看到类似的内容:

    请求已返回错误:数组('消息' =>'无效参数','代码' =>-32602,)

我将这个彻底的答案献给所有那些在 StackOverflow 上无休止地帮助我的人,他们只是回答别人的问题,有时是几年前.谢谢!希望我的回答也能节省一些人的时间、精力和心情.:)

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. 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. :)

这篇关于Getresponse API 2(使用 PHP 添加自定义字段和联系人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆