【问题标题】:Trying to add contacts via ActiveCampain API (Laravel)尝试通过 ActiveCampain API (Laravel) 添加联系人
【发布时间】:2021-04-29 17:43:33
【问题描述】:

我正在尝试将 ActiveCampaing 的 RESTFUL API 集成到我的 Laravel 环境中,但我没有那么幸运,我正在使用 GuzzleHttp 来发出请求,这是 error image 和我的代码:

 $client = new \GuzzleHttp\Client([‘base_uri’ => ‘https://myaccount.api-us1.com/api/3/’]);

$response = $client->request('POST', 'contacts', [
    'headers' => [
        'Api-Token' => 'xxx',
        'api_action' => 'contact_add',
    ],
    'json' => [
        'email' => 'test2021@test.com',
        'first_name' => 'Julian',
        'last_name' => 'Carax',
    ]
]);

echo $response->getStatusCode(); // 200
echo $response->getBody(); 

希望你能帮助我! :D

【问题讨论】:

  • 使用try catch块并捕获ClientException,然后$e->->getResponse()->getBody()->getContents();要获得完整的错误消息,请检查 api 文档中需要哪些字段并检查您没有发送哪些字段,然后更改您的代码并重新运行它将起作用的请求

标签: php laravel request guzzle activecampaign


【解决方案1】:

您没有以正确的格式发送数据, 来自文档https://developers.activecampaign.com/reference#contact

{
    "contact": {
        "email": "johndoe@example.com",
        "firstName": "John",
        "lastName": "Doe",
        "phone": "7223224241",
        "fieldValues":[
          {
            "field":"1",
            "value":"The Value for First Field"
          },
          {
            "field":"6",
            "value":"2008-01-20"
          }
        ]
    }
}

所以创建一个包含关键联系人的数组。

$contact["contact"] = [
        "email" => "johndoe@example.com",
        "firstName" => "John",
        "lastName" => "Doe",
        "phone" => "7223224241",
        "fieldValues" => [
            [
                "field"=>"1",
                "value"=>"The Value for First Field"
            ],
            [
                "field"=>"6",
                "value"=>"2008-01-20"
            ]
        ]
    ];

使用 try catch 块,这样你就可以捕捉到你的错误

try{
    $client = new \GuzzleHttp\Client(["base_uri" => "https://myaccount.api-us1.com/api/3/"]);

    $response = $client->request('POST', 'contacts', [
        'headers' => [
            'Api-Token' => 'xxx',
            'api_action' => 'contact_add',
        ],
        'json' => $contact
    ]);
    
    if($response->getStatusCode() == "200" || $response->getStatusCode() == "201"){
        $arrResponse = json_decode($response->getBody(),true);
    }
} catch(\GuzzleHttp\Exception\ClientException $e){
    $error['error'] = $e->getMessage();
    if ($e->hasResponse()){
        $error['response'] = $e->getResponse()->getBody()->getContents();
    }
    // logging the request
    \Illuminate\Support\Facades\Log::error("Guzzle Exception :: ", $error);
    // take other actions
} catch(Exception $e){
    return response()->json(
            ['message' => $e->getMessage()],
            method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 500);
}

【讨论】:

    【解决方案2】:

    您可以在API docs 上查看emailfirst_namelast_name 字段是否位于contact 节点下。

    所以创建一个contact 数组,将这些字段放入其中就可以了。

    名字和姓氏的字段写在firstNamelastName 行-驼峰式,而不是像你那样的蛇形。

    官方php客户端

    您可能应该使用官方的ActiveCampaign php api client - 这会让您的生活更轻松。

    【讨论】:

      猜你喜欢
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多