0

I am using trac 1.0.1 with the latest XmlRpcPlugin. When adding a ticket using python like this, non-ASCII characters (ä in this example) get inserted correctly:

import xmlrpclib
server = xmlrpclib.ServerProxy('https://user:pwd@localhost/trac/login/xmlrpc')
server.ticket.create("Täst", "Täst")

Now I want to add add a ticket using phpxmlrpc like this:

$client = new xmlrpc_client("https://localhost/trac/xmlrpc");
$client->setCredentials("usr", "pwd", CURLAUTH_BASIC);

$xmlrpc_msg = new xmlrpcmsg('ticket.create', array(new xmlrpcval("Täst"), new xmlrpcval("Täst"));
$xmlrpc_resp = $client->send($xmlrpc_msg);

I now get this error: not well-formed (invalid token): line 6, column 57 and the ticket is not being inserted.

When setting $client->request_charset_encoding to either UTF-8 or ISO-8859-1, both times the ticket is being inserted but all non-ascii characters are malformed.

How can I get this working?

Zulakis
  • 7,859
  • 10
  • 42
  • 67

2 Answers2

0

The main question is: is your example pertinent, ie. is your source string saved as part of your php sources, or is it gotten from somewhere else like a database?

If it is stored in php sources, then you should make sure that you control the character set that is used by your text editor when saving that file:

  • if you save the php source as utf8: nothing to do
  • if you save the php source as latin-1: you can call utf8_encode on your string

Once this is done, you have to tell the phpxmlrpc library that the charset used internally by your app is utf8. this is not done by setting $client->request_charset_encoding, but instead PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding

gggeek
  • 315
  • 1
  • 3
  • 8
-1

Note that PHP is known to have issues with Unicode characters. There is another SO question that discusses this in more detail.

Try using the utf8_encode() function to encode your strings before passing them to the xmlrpc functions.

Community
  • 1
  • 1
bta
  • 43,959
  • 6
  • 69
  • 99
  • I do not agree with the statement "PHP is known to have issues with Unicode characters": php is agnostic to charset encoding, treating strings as streams of bytes. Functions such as substr or strlen work with bytes, not with characters, and `mb_` functions exist to deal properly with unicode. As for adding `utf8_encode` calls in the poster's own code: it is technically possible, but the phpxmlrpc lib has extensive support for doing charset conversion on its own, which I think is a cleaner solution – gggeek Dec 22 '22 at 11:30
  • @gggeek My point was that PHP doesn't have native Unicode support like most other modern languages, and the programmer has to manually keep track of character encodings and handle it themselves. The linked answer (plus its linked article) explain in a lot more detail why this style of Unicode support is an issue. This answer is also almost a decade old, so current versions may include better support. – bta Dec 22 '22 at 16:40