I am trying to use my platform's API by using PHP and cURL, but I have a problem when I try to PATCH some custom attributes. As the title says, I am getting an API error with code: 422 and the message: Validation failed., because the attributes field has Invalid data?

I get this error when I am using this piece of code:

$updateServiceIP = UcrmApi::ucrmRequest("clients/services/$serviceId", 'PATCH', [
    'attributes' => [
        'value' => $IPs[$ipAddress],
        'customAttributeId' => 45
    ]
]);

var_dump($updateServiceIP); // this outputs NULL

but when I use this piece of code, the value is updated successfully:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, '{{URL}}/crm/api/v1.0/clients/services/' . $serviceId);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");

curl_setopt($ch, CURLOPT_POSTFIELDS, "{
    \"attributes\": [
        {
            \"value\": \"lorem ipsum\",
            \"customAttributeId\": 45
        }
    ]
}");

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "X-Auth-App-Key: API_KEY"
));

$response = curl_exec($ch);
curl_close($ch);

var_dump($response);

I know that I can use the second piece of code, but I do not want to, because I have a special function that calls the API and can PATCH, POST, GET from my platform.

public static function ucrmRequest($url, $method = 'GET', $post = [])
{
    $method = strtoupper($method);

    $ch = curl_init();

    curl_setopt(
        $ch,
        CURLOPT_URL,
        sprintf(
            '%s/%s',
            self::UCRM_URL,
            $url
        )
    );
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);

    curl_setopt(
        $ch,
        CURLOPT_HTTPHEADER,
        [
            'Content-Type: application/json',
            sprintf('X-Auth-App-Key: %s', self::UCRM_KEY),
        ]
    );

    if ($method === 'POST') {
        curl_setopt($ch, CURLOPT_POST, true);
    } elseif ($method !== 'GET') {
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    }

    if (! empty($post)) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
    }

    $response = curl_exec($ch);

    if (curl_errno($ch) !== 0) {
        echo sprintf('Curl error: %s', curl_error($ch)) . PHP_EOL;
    }

    if (curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400) {
        echo sprintf('API error: %s', $response) . PHP_EOL;
        $response = false;
    }

    curl_close($ch);

    return $response !== false ? json_decode($response, true) : null;
}

What can be the problem and how can I solve it? By the way, I've read some of the similar questions, but I didn't found a solution for my problem...

Shouldn't you be using quotes around your IP address, and around you entire JSON message?

commented: I've tried that way, but I got the same error, sometimes it doesn't even send the API call. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.