Basic Auth php przykład

        $parameters = json_encode($parameters);
        
        $headers = [
            'Content-Type: application/json',
            'Accept: application/json',
        ];

        try {
            // curl setup, disabling certificate check
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_USERPWD, $this->login . ":" . $this->pass);

            $responseJson = curl_exec($ch);
            $response = json_decode($responseJson, true);

            $this->lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
        } catch (\Exception $ex) {
            throw new \Exception('Curl call failed! HttpCode: ' . $this->lastHttpCode . ' (Ex: ' . $ex->getMessage() . ')');
        }

        return $response;
Komentarze wyłączone