PHP: Server side user creation

Hi,I ma trying the create the users from server side using PHP, Here i my code ,
<?php
class Chat_api
{
private const API_KEY = “5fb4ad187084******************”;
private const APP_ID = “19642***********”;
private const region = “us”;

            private const API_BASE_URL = 'https://api-us.cometchat.io/v2.0/';


            private $headers = [
                'apikey' => self::API_KEY,
                'appid' => self::APP_ID,
                'Content-type' => 'application/json'
            ];


            public function create_user($uid, $name, $profile_image)
            {
                $data = [
                    'uid' => $uid,
                    'name' => $name,
                    'avatar' => $profile_image
                ];

                $url = self::API_BASE_URL . 'users';
                // dd($data);
                try {
                    $response = Requests::post($url, $this->headers, $data);

                    var_dump($response->body);

                    if ($response->status_code == 200) {
                        return true;
                    }
                } catch (Exception $e) {
                    _log($e);
                    return false;
                }
            }
        }

I always get this error if make a request,

string(253) “{“error”:{“message”:“Failed to validate the data sent with the request.”,“devMessage”:“Failed to validate the data sent with the request.”,“details”:{“uid”:[“The uid field is required.”],“name”:[“The name field is required.”]},“code”:“ERR_BAD_REQUEST”}}”

Hi @ropali.m,

Please try json_encode($data)

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.