Loginviapost without a form?

The loginviapost app is great so long as you login via a form, which I guess is what it’s designed to do.

In my scenario I already have a login screen which logs my users into our portal and also to the mail server though want this to log them into NC also. I’m also using the user_sql app to I can authenticate my users from my one database which also works.

I’m trying to the following but it never works, if i put a regular form it logs in perfectly though I want to login users behind the scenes so their NC drive is available via one of the links in our portal.
The example form is purely to execute the standard nextcloud login and bypasses external origin etc.

so in essence: the form below when posted works perfectly and logs the user in

<form method="post" action="https://domain.co.uk/index.php/apps/loginviapost/login">
	<input type="text" name="username" />
	<input type="text" name="password"  />
	<input type="submit" value="Submit" />

whereas executing via code as below does nothing and if I subsequently go the files page I just get the log in page.

$data = array(
	'username' => 'username',
	'password' => 'password'

$postvars = http_build_query($data) . "\n";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://domain.co.uk/index.php/apps/loginviapost/login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);

Can anyone please provide a code sample which will work and allow me via regular PHP code (given a username and password) to login to nextcloud without going through the UI login process?

FYI, this is logged as a feature request on github with a $50 bounty for someone who can help … please?