<?php
namespace Newland\CityTheme\ViewHelpers\Api;

use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use Neos\Flow\Annotations as Flow;

class GetWiifChatTokenViewHelper extends AbstractViewHelper {
    public function render()
    {
        // External API endpoint
        $url = "https://api.wiif.net/hsw-chatbot/token";

        // Your confidential credentials
        $data = [
            "grant_type" => "password",
            "username"   => "lis-api",
            "password"   => "K0jC1JKGO7ON7vSMwBKHasWfWEF!FE",
            "scope"      => ""
        ];

        // Convert to x-www-form-urlencoded
        $postFields = http_build_query($data);

        // Initialize curl
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            "Content-Type: application/x-www-form-urlencoded"
        ]);

        // Execute and capture response
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $error = curl_error($ch);
        curl_close($ch);

        return $response;
    }
}
