createPost() inserts text to database with wrong charset

Started by morcibacsi, December 10, 2013, 03:31:49 PM

Previous topic - Next topic

morcibacsi

Hello everyone!

I've just started using SMF, I'm mirroring a forum's single thread to my SMF (2.06 version). What I do is download the thread (in html) via file_get_contents() in every hour and insert the entries to SMF via createpost(). I started the whole thing on my developer machine, and when the converter was doing what i thought I've transferred the forum to my web server which is hosted via a provider. Here comes the problem, which worked fine on my environment, does not work on the server. Somehow the message body is become gibberish. It seems that it is inserted with a wrong charset. This 'áÁéÉíÍóÃ"öÖÅ'ŐúÚüÜűŰ' is inserted in the database instead of this: 'áÁéÉíÍóÓöÖőŐúÚüÜűŰ ' 

I wrote a small sample, to narrow down the problem, but I got stuck. This works well on my machine but not on the server. Does anyone have an idea what could be the problem?


<?php
require('../Settings.php');
require(
'../SSI.php');
require_once(
'../Sources/QueryString.php');
require_once(
'../Sources/Subs.php');
require_once(
'../Sources/Errors.php');
require_once(
'../Sources/Load.php');
require_once(
'../Sources/Security.php');
require_once(
'../Sources/Subs-Admin.php');
require_once 
'../Sources/Subs-Post.php';
require_once(
'../Sources/Subs-Boards.php');


$msgOptions = array(
    
'body' => 'áÁéÉíÍóÓöÖőŐúÚüÜűŰ',
    
'subject' => 'Wrong charset áÁéÉíÍóÓöÖőŐúÚüÜűŰ',
    
'smileys_enabled' => true
);

$topicOptions = array(
    
'board' => 2,
    
'mark_as_read' => true,
    
'id' => 2,
    
'sticky_mode' => 1
);

$posterOptions = array(
    
'name' => 'TestUser',
    
'ip' => '127.0.0.1',
    
'email' => ''
);

createPost($msgOptions$topicOptions$posterOptions);
?>



EDIT:

Okay, if I try to write to the remote database from my dev machine the result is the same, so the difference is in the database server. On my dev machine it is MySQL 5.5.20 and on the server it is 5.1.72-2. However I still don't get it because if I post a message from the forum, everything is OK, but via the createPost() the message is garbled :(

Arantor

Firstly, if you include SSI.php you do not need any of the other files except Subs-Post.php to run createPost.

Secondly, assuming your database is UTF-8 and SMF is set to use UTF-8, ensure your file was saved as UTF-8 as well. Otherwise whatever your database is set to, make sure your editor is also set to save the file with the same encoding.

Note that the main SMF converter doesn't itself use createPost so that it can ensure it keeps message ids and topics from the original content, this may be something to bear in mind.

morcibacsi

Thanks for the reply, I figured it out. I put this line into my file and the problem gone.


$smcFunc['db_query']('', "SET NAMES utf8", 'security_override');

Arantor

SMF (including SSI) will normally do that itself. The fact it isn't (inside loadDatabase) suggests something is strange in your setup.

Advertisement: