turnstile php
Version 0.5.3
Recaptcha에서 영감을 얻었습니다
composer require usarise/turnstile
composer require symfony/http-client nyholm/psr7 usarise/turnstile
<?php
declare (strict_types= 1 );
require_once __DIR__ . ' /vendor/autoload.php ' ;
use Symfony Component HttpClient Psr18Client ;
use Turnstile Error Code ;
use Turnstile Turnstile ;
// Get real API keys at https://dash.cloudflare.com/?to=/:account/turnstile
$ siteKey = ' 1x00000000000000000000AA ' ; // Always passes (Dummy Testing)
$ secretKey = ' 1x0000000000000000000000000000000AA ' ; // Always passes (Dummy Testing)
if ( $ token = $ _POST [ ' cf-turnstile-response ' ] ?? null ) {
$ turnstile = new Turnstile (
client: new Psr18Client (),
secretKey: $ secretKey ,
);
$ response = $ turnstile -> verify (
$ token , // The response provided by the Turnstile client-side render on your site.
$ _SERVER [ ' REMOTE_ADDR ' ], // With usage CloudFlare: $_SERVER['HTTP_CF_CONNECTING_IP']
);
if ( $ response -> success ) {
echo ' Success! ' ;
} else {
$ errors = $ response -> errorCodes ;
var_dump ( $ errors );
var_dump (Code:: toDescription ( $ errors ));
}
exit ;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Turnstile example</title>
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
</head>
<body>
<form action="" method="POST">
<!-- The following line controls and configures the Turnstile widget. -->
<div class="cf-turnstile" data-sitekey=" <?php echo $ siteKey ; ?> " data-theme="light"></div>
<!-- end. -->
<button type="submit" value="Submit">Verify</button>
</form>
</body>
</html> var_dump (( string ) $ response ); var_dump ( $ response -> toArray ()); var_dump ( $ response -> toArray (strict: true ));Turnstile use Turnstile Client Client ;
use Turnstile Turnstile ;
$ turnstile = new Turnstile (
client: new Client (...),
secretKey: ' secret key ' ,
idempotencyKey: ' idempotency key ' ,
); php-http/discovery 와 같은 PSR-18 클라이언트
$ turnstile = new Turnstile (
client: new Psr18Client (),
secretKey: ' secret key ' ,
idempotencyKey: ' idempotency key ' ,
);Client use Turnstile Client Client ;
use Turnstile TurnstileInterface ;
$ client = new Client (
client: ..., // implementation PsrHttpClientClientInterface
requestFactory: ..., // implementation PsrHttpMessageRequestFactoryInterface (default: requestFactory = client)
streamFactory: ..., // implementation PsrHttpMessageStreamFactoryInterface (default: streamFactory = requestFactory)
siteVerifyUrl: TurnstileInterface:: SITE_VERIFY_URL , // https://challenges.cloudflare.com/turnstile/v0/siteverify (default)
); composer require guzzlehttp/guzzle
use GuzzleHttp Client as GuzzleHttpClient ;
use GuzzleHttp Psr7 HttpFactory ;
use Turnstile Client Client ;
$ client = new Client (
new GuzzleHttpClient (),
new HttpFactory (),
); composer require symfony/http-client nyholm/psr7
use Symfony Component HttpClient Psr18Client ;
use Turnstile Client Client ;
$ client = new Client (
new Psr18Client (),
); use Symfony Component HttpClient Psr18Client ;
$ client = new Psr18Client (); composer require symfony/http-client guzzlehttp/psr7
use GuzzleHttp Psr7 HttpFactory ;
use Symfony Component HttpClient Psr18Client ;
use Turnstile Client Client ;
$ client = new Client (
new Psr18Client (
responseFactory: new HttpFactory (),
),
); use GuzzleHttp Psr7 HttpFactory ;
use Symfony Component HttpClient Psr18Client ;
$ client = new Psr18Client (
responseFactory: new HttpFactory (),
); composer require symfony/http-client guzzlehttp/psr7 php-http/discovery
use Symfony Component HttpClient Psr18Client ;
use Turnstile Client Client ;
$ client = new Client (
new Psr18Client (),
); use Symfony Component HttpClient Psr18Client ;
$ client = new Psr18Client (); composer require nyholm/psr7 php-http/curl-client
use Http Client Curl Client as CurlClient ;
use Nyholm Psr7 Factory Psr17Factory ;
use Turnstile Client Client ;
$ psr17Factory = new Psr17Factory ();
$ client = new Client (
client: new CurlClient (
responseFactory: $ psr17Factory ,
streamFactory: $ psr17Factory ,
),
requestFactory: $ psr17Factory ,
); composer require php-http/discovery
use Http Discovery Psr18Client ;
use Turnstile Client Client ;
$ client = new Client (
new Psr18Client (),
); use Http Discovery Psr18Client ;
$ client = new Psr18Client ();위젯의 비밀 키. 비밀 키는 개찰구 아래 CloudFlare 대시 보드의 위젯 설정에서 찾을 수 있습니다.
https://dash.cloudflare.com/?to=/:account/turnstile의 API 키
1x0000000000000000000000000000000AA 항상 통과합니다
2x0000000000000000000000000000000AA 항상 실패합니다
3x0000000000000000000000000000000AA "이미 소비 된 토큰"오류를 생산합니다
use Turnstile Client Client ;
use Turnstile Turnstile ;
// Real API keys at https://dash.cloudflare.com/?to=/:account/turnstile
$ secretKey = ' 1x0000000000000000000000000000000AA ' ;
$ turnstile = new Turnstile (
client: $ client ,
secretKey: $ secretKey ,
);응용 프로그램이 실패한 요청을 다시 시도 해야하는 경우 Idempotency 기능을 사용해야합니다.
idempotencyKey 매개 변수로 UUID를 제공 한 다음 필요한 숫자와 동일한 토큰으로 $turnstile->verify(...) 사용하여 그렇게 할 수 있습니다.
composer require ramsey/uuid
use Ramsey Uuid Uuid ;
use Turnstile Client Client ;
use Turnstile Turnstile ;
$ turnstile = new Turnstile (
client: $ client ,
secretKey: $ secretKey , // The site’s secret key.
idempotencyKey: ( string ) Uuid:: uuid4 (), // The UUID to be associated with the response.
);
$ response = $ turnstile -> verify (
$ token , // The response that will be associated with the UUID (idempotencyKey)
);
if ( $ response -> success ) {
// ...
}
$ response = $ turnstile -> verify (
$ token , // The response associated with UUID (idempotencyKey)
);
if ( $ response -> success ) {
// ...
} $ response = $ turnstile -> verify (
token: $ _POST [ ' cf-turnstile-response ' ], // The response provided by the Turnstile client-side render on your site.
); remoteIp 매개 변수는 현재 방문자가 토큰을받은 사람임을 확인하여 학대를 방지하는 데 도움이됩니다.
이것은 현재 엄격하게 검증되지 않았습니다.
$ response = $ turnstile -> verify (
token: $ _POST [ ' cf-turnstile-response ' ], // The response provided by the Turnstile client-side render on your site.
remoteIp: $ _SERVER [ ' REMOTE_ADDR ' ], // The visitor’s IP address.
); $ response = $ turnstile -> verify (
token: $ _POST [ ' cf-turnstile-response ' ], // The response provided by the Turnstile client-side render on your site.
remoteIp: $ _SERVER [ ' HTTP_CF_CONNECTING_IP ' ], // The visitor’s IP address.
); $ response = $ turnstile -> verify (
...
challengeTimeout: 300 , // Number of allowed seconds after the challenge was solved.
expectedHostname: $ _SERVER [ ' SERVER_NAME ' ], // Expected hostname for which the challenge was served.
expectedAction: ' login ' , // Expected customer widget identifier passed to the widget on the client side.
expectedCdata: ' sessionid-123456789 ' , // Expected customer data passed to the widget on the client side.
); $ response -> success $ response -> errorCodes $ response -> challengeTs $ response -> hostname $ response -> action $ response -> cdata RAW JSON 데이터가있는 문자열
( string ) $ response DECODED JSON 데이터
$ response -> toArray () Response 클래스의 속성을 기반으로 처리 된 JSON 데이터 배열 : success , errorCodes , challengeTs , hostname , action , cdata
$ response -> toArray (strict: true )오류 코드를 적절한 언어로 설명으로 변환 (기본 영어)
use Turnstile Error { Code , Description };
var_dump (
Code:: toDescription (
codes: $ response -> errorCodes ,
descriptions: Description:: TEXTS , // Default
),
);