Announcement

Collapse
No announcement yet.

create a registration form, same role as the API user.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • create a registration form, same role as the API user.

    Hi, I need to create a registration form, after completing it the user will have the same role as the API user.

    Can it be done?
    Please do not have an example code?


    The registration form would include:

    username
    password

    mycode:

    PHP Code:
      <?php  class EspoApiClient {     private $url;      private $userName null;      private $password null;      protected $urlPath '/api/v1/';      protected $userAgent 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36';      private $lastCh;      private $lastResponse;      private $apiKey null;      private $secretKey null;      public function __construct($url null$userName null$password null)     {         if (isset($url)) {             $this->url $url;         }         if (isset($userName)) {             $this->userName $userName;         }         if (isset($password)) {             $this->password $password;         }     }      public function setUrl($url)     {         $this->url $url;     }      public function setUserName($userName)     {         $this->userName $userName;     }      public function setPassword($password)     {         $this->password $password;     }      public function setApiKey($apiKey)     {         $this->apiKey $apiKey;     }      public function setSecretKey($secretKey)     {         $this->secretKey $secretKey;     }      /**      * Send request to EspoCRM      *      * @param string $method      * @param string $action      * @param array|null $data      *      * @return array | \Exception      */     public function request($method$action, array $data null)     {         $method strtoupper($method);          $this->checkParams();          $this->lastResponse null;         $this->lastCh null;          $url $this->normalizeUrl($action);          $ch curl_init($url);          $headerList = [];          curl_setopt($chCURLOPT_RETURNTRANSFERtrue);         if ($this->userName && $this->password) {             curl_setopt($chCURLOPT_USERPWD$this->userName.':'.$this->password);             curl_setopt($chCURLOPT_HTTPAUTHCURLAUTH_BASIC);         } else if ($this->apiKey && $this->secretKey) {             $string $method ' /' $action;             $authPart base64_encode($this->apiKey ':' hash_hmac('sha256'$string$this->secretKeytrue));             $authHeader 'X-Hmac-Authorization: ' .  $authPart;             $headerList[] = $authHeader;         } else if ($this->apiKey) {             $authHeader 'X-Api-Key: ' .  $this->apiKey;             $headerList[] = $authHeader;         }          curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);         curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);         curl_setopt($chCURLOPT_USERAGENT$this->userAgent);         curl_setopt($chCURLOPT_HEADERtrue);          if ($method != 'GET') {             curl_setopt($chCURLOPT_CUSTOMREQUEST$method);         }          if (isset($data)) {             if ($method == 'GET') {                 curl_setopt($chCURLOPT_URL$url'?' http_build_query($data));             } else {                 $payload json_encode($data);                 curl_setopt($chCURLOPT_POSTFIELDS$payload);                 $headerList[] = 'Content-Type: application/json';                 $headerList[] = 'Content-Length: ' strlen($payload);             }         }          if (!empty($headerList)) {             curl_setopt($chCURLOPT_HTTPHEADER$headerList);         }          $this->lastResponse curl_exec($ch);         $this->lastCh $ch;          $parsedResponse $this->parseResponce($this->lastResponse);         $responseCode $this->getResponseHttpCode();          if ($responseCode == 200 && !empty($parsedResponse['body'])) {             curl_close($ch);             return json_decode($parsedResponse['body'], true);         }          $header $this->normalizeHeader($parsedResponse['header']);         $errorMessage = !empty($header['X-Status-Reason']) ? $header['X-Status-Reason'] : 'EspoClient: Unknown Error';          curl_close($ch);         throw new \Exception($errorMessage$responseCode);     }      public function getResponseContentType()     {         return $this->getInfo(CURLINFO_CONTENT_TYPE);     }      public function getResponseTotalTime()     {         return $this->getInfo(CURLINFO_TOTAL_TIME);     }      public function getResponseHttpCode()     {         return $this->getInfo(CURLINFO_HTTP_CODE);     }      protected function normalizeUrl($action)     {         return $this->url $this->urlPath $action;     }      protected function checkParams()     {         $paramList = [             'url'         ];          foreach ($paramList as $name) {             if (empty($this->$name)) {                 throw new \Exception('EspoClient: Parameter "'.$name.'" is not defined.');             }         }          return true;     }      protected function getInfo($option)     {         if (isset($this->lastCh)) {             return curl_getinfo($this->lastCh$option);         }     }      protected function parseResponce($response)     {         $headerSize $this->getInfo(CURLINFO_HEADER_SIZE);          return [             'header' => trimsubstr($response0$headerSize) ),             'body' => substr($response$headerSize),         ];     }      protected function normalizeHeader($header)     {         preg_match_all('/(.*): (.*)\r\n/'$header$matches);          $headerArray = array();         foreach ($matches[1] as $index => $name) {             if (isset($matches[2][$index])) {                 $headerArray[$name] = trim($matches[2][$index]);             }         }          return $headerArray;     } }  <form method="POST">   username:<br>   <input type="text" name="username" value="Mickey">   <br>   Password:<br>   <input type="password" name="password" value="Mouse"><br>   Retry password:<br>   <input type="password" name="password" value="Mouse">   <br><br>   <input type="submit" value="Submit"> </form>
Working...
X