usyme/typeform-webhook

PHP的Typeform webhook包装器

dev-master 2020-06-19 03:15 UTC

This package is auto-updated.

Last update: 2024-09-19 12:56:45 UTC


README

Typeform服务提供的安全PHP包装器。

Build Status

安装

第一步是在您的typeform账户/工作区中创建一个新的webhook端点创建新的webhook端点

第二步,您需要生成一个随机字符串。例如,通过终端

$ ruby -rsecurerandom -e 'puts SecureRandom.hex(20)'

然后,更新工作区“连接”部分的webhook设置密钥。

下一步是使用Composer安装包装器

$ composer require usyme/typeform-webhook dev-master

接收Typeform响应

原生

require __DIR__ . '/../vendor/autoload.php';

use Usyme\Typeform\Webhook\Exception\AuthenticationFailedException;
use Usyme\Typeform\Webhook\Exception\BadPayloadFormatException;
use Usyme\Typeform\Webhook\Webhook;

$webhook = new Webhook('{personal_key}');

try {
    $response = $webhook->getResponse('{secret_key_from_header}', '{response_payload}');
} catch (AuthenticationFailedException $exception) {
    // Invalid received secret key
} catch (BadPayloadFormatException $exception) {
   // Bad received response payload format
}

Symfony

此库是为基于Symfony的项目构建的,因此我们准备了一些类助手来简化Typeform与symfony/http-foundation & symfony/form组件的集成。

class TypeformController
{
    /**
     * @param Request        $request
     * @param WebhookRequest $webhookRequest
     *
     * @return Response
     */
    public function webhook(Request $request, WebhookRequest $webhookRequest): Response
    {
        try {
            // Fetch the typeform response directly with $request object.
            $response = $webhookRequest->getResponse($request);
        } catch (AuthenticationFailedException $exception) {
            // Invalid received secret key
        } catch (BadPayloadFormatException $exception) {
            // Bad received response payload format
        }
        
        // ...
        
        // Or if you want to use the symfony's form component directly
        // you need to handle your request object, we're simulating a simple
        // post request using the handleHttpRequest() method.
        try {
            // In this case we're replacing our $request->request parameters
            // by the typeform data. So our $request object is now up-to-date :)
            $webhookRequest->handleHttpRequest($request);
        } catch (AuthenticationFailedException $exception) {
            // Invalid received secret key
        } catch (BadPayloadFormatException $exception) {
            // Bad received response payload format
        }
        
        // ...
    }
}

有疑问吗?

如果您有任何疑问,请提出问题

许可证

此库在MIT许可证下发布。有关详细信息,请参阅附带LICENSE文件。