0
votes

I'm invoking SWI-prolog from PHP, sending a question and getting result. I'm using exec() function for that:

$goal = "all_facts('S').";
$cmd = "/software/swipl/bin/swipl -f Domain.pl -g " . $goal . " -t halt";

The predicate all_facts() returns facts about counties, in this example - square.

There are a lot of facts about countries in Russian in the prolog file as it needed for my site. SWI-prolog returns an answer with Russian symbols and every thing is OK.

The problem rises when I'm trying to use Russian symbols in request, for example:

 $goal = "all_facts('Столица').";

Then I'm running this request exactly in SWI-prolog, I get the correct answer. But then I'm asking it from PHP, I don't get the answer at all.

So, I guess, bugs appear while sending request and SWI-prolog gets distorted question, so it can't find an answer.

I've already changed encoding of SWI-Prolog (using environment variable LANG), encoding of page, tried to change encoding of PHP exec (not sure, if it worked). Maybe, I really need to change PHP exec encoding, but I am doing it wrong.

How can I do it?

Just in case, part of the prolog file:

facts('Австралия','S', 7686850).
facts('Австрия', 'S', 83871).
facts('Азербайджан','S', 86600).
facts('Австралия','Столица', 'Канберра').
facts('Австрия', 'Столица', 'Вена').
facts('Азербайджан','Столица', 'Баку').
all_facts(C):- findall(X:Y, facts(X,C,Y), All), write(All).

PHP code:

<?
    $goal = "all_facts('Столица').";
    $cmd = "/software/swipl/bin/swipl -f Domain.pl -g ".$goal." -t halt";

    if (exec($cmd)) {
        $output = exec($cmd);
    }
    else {
        echo "Error!";
    }

    $output = str_replace(array("[","]"), "", $output);
    $facts = explode(",", $output);
    $length = count($facts);
    echo "<ul>";
    for ($i=0; $i<$length; $i++) {
        $all_facts[$i]= explode(":", $facts[$i]);
        echo "<li>", $all_facts[$i][0], " - ", $all_facts[$i][1], "</li>";
    }
    echo "</ul>";
?>
1
How are you capturing the output of the all_facts/1 call? - Paulo Moura
I've added php listing above. - Lazola
Not familiar with PHP but your code seems fine. Did you experimented with facts contain only ASCII characters? So that you can confirm that's an encoding issue? - Paulo Moura
Yes. When I've tested code with ASCII characters everything works fine. It works even when I recive russian characters from prolog. But it doesn't send them correctly. - Lazola
I can only assume that you also checked the encoding the source file holding the PHP code and ensured that's the same used through your workflow? - Paulo Moura

1 Answers

0
votes

Based on the discussion on the comments, I suggest you add at the first line of the Domain.pl file the following directive:

:- encoding(utf8).

and ensure that the text editor used to edit the file saves it as a UTF-8 file. Your modified version of the all_facts/1 predicate should now write utf8.