I have a program that adds and update sstp-server interface with an api call.
I’m using PEAR2_Net_RouterOS 1.0.0b5 and I can’t send properly some UTF-8 characters.
I’ve add header(“Content-Type: text/html; charset: UTF-8”); and tryed to add this:
//Here’s where we specify the charset pair.
$client->setCharset(
array(
RouterOS\Communicator::CHARSET_REMOTE => ‘windows-1256’,
RouterOS\Communicator::CHARSET_LOCAL => ‘UTF-8’
)
);
with differnt combinations.
but every thime I send characters like á í é ó the are not shown correctly on mikroitk sstp-server interface.
Is there any combination i could use to be available to store correctly on mikroitk that characters and also to red them back correctly as well, because i need to update them so I need to read, compare that they are ok and update if necessari.
In addition to the Content-Type HTTP header and PEAR2_Net_RouterOS’ charset settings, your actual PHP file (i.e. where the string is written) also needs to be UTF-8 encoded.
To get a good handle on what the correct remote charset is, try to type something from Winbox that does contain the weird characters you’ll need, and try to display them on the web page correctly. If that part is working correct, then your content type and charset pair are fine.
When writing to RouterOS, it’s the raw bytes that matter (and must match the local charset, i.e. UTF-8)… Hence the need for the PHP file to be UTF-8 encoded.
To ensure the file is UTF-8 encoded, open it up with Notepad, click “Save As”, and select “UTF-8” in the “Encoding” dropdown. More coding oriented editors (e.g. PhpStorm, Visual Studio Code) will usually have a similar option in the bottom right of the status bar (along with detected line endings), allowing you to switch the encoding, and the really good ones (again, PhpStorm, Visual Studio Code) default to UTF-8 for new files.
But the problem then comes with chinese characters not supported.
Without the last setCharset i did send the chinese characters with other like í and all of them looks bad on winbox.
But I could reover and compare with php with same characters coming from database.
With this new charset characters some chars like í are sent and read perfect but chinese chars are lost so when i read the sting sent is not the same I have on php.
What I’ve donne to solve the probleme is:
$pppname=preg_replace(“/\p{Han}+/u”, '', $pppname); / Convert chinese chars to * - to have semething writed /
$pppname=iconv(“UTF-8//IGNORE”, “WINDOWS-1252//IGNORE”, $pppname); / Delete other chars not supported */
$pppname=iconv(“WINDOWS-1252//IGNORE”, “UTF-8//IGNORE”, $pppname);
So now works ok but all the chinese characters are losted (some converted with * other losted).
Some strings examples:
X Dining 艾克斯義式餐酒館(牛排。甜點。義式料理。包場 )íáóú
等一下串燒-ちょっと待って íáóú
What is your locale for non-Unicode applications (such as Winbox)?
You can check by going to “Control Panel” > “Region” > “Administrative”, under the “Current language for non-Unicode programs”. Also if you click the “Change system locale”, do you have the “use UTF-8…” checkbox checked?
Displaying all characters in Winbox is sort of an impossible feat I think… I thought you effectively have to pick either Chinese or western European, but can’t do both. If you’ve somehow configured Windows to let you type both in Winbox, and have it displayed correctly, then surely, there would be a way to convert UTF-8 to that combination, but I would have to replicate the settings that your Winbox uses to see what’s actually going on.
Oh. Well, if Winbox doesn’t allow you to type in both, then PHP is also working as expected.
You have two options:
Write everything in Spanish. PEAR2_Net_RouterOS already uses iconv which should ignore (and therefore drop) all characters that Winbox would not allow you to type in.
Don’t use charset conversion at all, write everything from PHP in UTF-8, but forget about Winbox. Webfig I think should display UTF-8 fine, so you can look at the router settings from there.
If you’re trying to make an app that is agnostic to the target router, let users configure their Winbox charset, since it may not be the same as yours, and default to not using a conversion, but sending everything as UTF-8.
BTW, fun fact I learned just now first hand… MikroTik’s mobile app supports and defaults to UTF-8, while allowing you to switch encodings.
Assuming WebFig does also use UTF-8 (I haven’t checked), this means Winbox and terminal are the only environments where UTF-8 is not supported. Terminal doesn’t support anything beyond ASCII, so that’s expected I guess, but Winbox… Winbox is just old.
All my respect, great master! Please help me. I’m having character encoding problems. I have a Lazarus project, on win64. This project sends data to mikrotik hap ac2 via API. Everything works perfectly except the accented characters. The point is that I can create hotspot users with accented characters and spaces in winbox. My Lazarus project means that it copies the data entered in the edit fields into a memo component, and then a piece of code sends this data to Mikrotik. The accented characters are still there in the memo component, but only strange characters appear in the mikrotik. I think the settings of the project itself are good, because everything that can be set is utf-8 encoded. (I’m a poor Lazarus programmer, so I’m not sure) If you could take some time to look at my project, I’d really appreciate it!
User created in Mikrotik winbox:
What comes to mikrotik:
Kis János
Memo font writing system: Central European
Project character encoding:
Poject setting:
And the code: (adat: TMemo)
procedure TDemoAPI.BitBtn1Click(Sender: TObject);
var
pa: array of String;
i: Integer;
s: String;
nap: String;
erveny: String;
Utf8_Name: String;
begin
if Useredit.Text='' then panel1.visible:=true;
if Useredit.Text <> '' then
begin
SaveQRCodeToImage('', TPortableNetworkGraphic);
PasswEdit.Text:='';
result2 := RandomString(10);
PasswEdit.Text:=result2;
adat.Clear;
// UTF-8-ra konvertálás
Utf8_Name := UTF8Encode(Useredit.Text);
adat.Lines.Add('/ip/hotspot/user/add');
adat.lines.Add('=server=Vendeg');
adat.lines.Add('=profile=Vendeg_user_profil');
adat.lines.Add('=name=' + Utf8_Name);
adat.lines.Add('=password=' + PasswEdit.Text);
adat.lines.Add('=email=' + emailedit.Text);
adat.lines.Add('=limit-uptime=' + nap);
edText.Text:='';
edText.Text:= 'http://nails.wifi/login?username='+Useredit.Text+'&password='+PasswEdit.Text;
QRCode.Free;
QRCode := TBarcodeQR.Create(self);
QRCode.Text := edText.Text;
with QRCode do
begin
Height := 200;
Top := 100;
Width := 200;
Left := (page.width - QRCode.Width) div 2;
Parent := page;
end;
Result.Lines.Clear;
end;
Where is the problem? What could be the problem? (translating this page in chrome with google translator, I noticed that "What comes to mikrotik:
From the "Kis János section, Kis János appears well)
RouterOS does not support UTF encoding or any other.
The only one it supports is 7-bit ASCII characters.
So don’t use special characters, but only the classic 0-9 A-Z a-z
v space
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
So then the solution is to convert the contents of the memo component to 7-bit ASCII, or send the data that way? Because mikrotik handles accented characters, at least for hotspot users.
WinBox 4 beta should show the (UTF-8) textual content fromt he API correctly. Can you try with it?
However, the text contents that look correct in WinBox 3.x (I think it uses Windows-1252 or ISO-8859-1) will become garbage in WinBox 4 (it assumes UTF-8 encoding). You’ll probably need to spend some time to convert them to UTF-8 (for instance by manually editing them in WinBox 4).
Well, I was almost happy that winbox 4 recommended by CGGXANNX solves the problem. Yes, accented characters go through the parenthesis, now the space that was there before has disappeared.
Well, ok, something happened, starting Lazarus as administrator solved the problem. Now mikrotik hotspot user has an accented and spaced user. However, another error occurred in this line: edText.Text:= 'http://nails.wifi/login?username=‘+Useredit.Text+’&password='+PasswEdit.Text; The Useredit.Text variable does not receive the space character well, so the received “link” is not good, the QR code link is not generated well.