Page 1 of 1

function file_get_contents

Posted: Fri Aug 06, 2010 2:42 pm
by majobsas
Hello!

I'm having some trouble getting the raw data from POST. I'm working with a company that sends by post a txt message from mobiles phone. The company documentation says that their system will send a XML with all the information from the txt message: phone number, time, text, etc.

Inorder to get the info, i'm using the php's function file_get_contents("php://input") , that works perfectly... except when the text message contains special characters ( like á or ñ ). I get a broken XML.

If the XML is like
<?xml version="1.0" encoding="utf-8"?>
<TXT>
<number>12345</number><destination>1111</destination><text>helló world!</text><time>2005-08-12</time>
</TXT>

With file_get_contents I get:

<?xml version="1.0" encoding="utf-8"?>
<TXT>
<number>12345</number><destination>1111</destination><text>

I know this, because I write the txt with fwrite on a file. The company told me that If the message has a special character, they change the text form utf-8 to unicode... but the XML keeps coming as encoding="utf-8" .

Then I've tried to open the file with the XML with an editor with encoding unicode, and the result was
<?xml version="1.0" encoding="utf-8"?>
<TXT>
<number>12345</number><destination>1111</destination><text> h e l l ó w o r l d ! e l l ó w o r l d </text><time>2005-08-12</time>
</TXT>

The thing is , when I try to parser the xml to an array, the fields text and time are missing in the array.

I don't know if the problem is mine, cause I'm doing something wrong when I try to get the information. Or is a problem from the company that's sending the XML in a wrong format.

Greetings, Majobsas :banghead:

Re: function file_get_contents

Posted: Fri Aug 06, 2010 11:01 pm
by requinix
UTF-8 is Unicode, in the same way that my Ford Focus out front is a car.

What exactly does

Code: Select all

var_dump(file_get_contents("php://input"));
produce? Do a View Source in your browser and copy/paste the result into some

Code: Select all

 tags.
You may censor numbers by replacing each individual digit with an asterisk.

Re: function file_get_contents

Posted: Mon Aug 09, 2010 2:59 pm
by majobsas
Hi!

This is my code:

Code: Select all

<?php

     $filename = "checkXML.txt";
     $gestor = fopen($filename, "w");

     if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {

        $xml = file_get_contents("php://input");
        
        fwrite($gestor, $xml);

        if ( $xml == "" ) {
          sendErrorInProcess();
        } else {
          processData($xml);
        }

     }

     fclose($gestor);
?>
I can't check the result of "var_dump(file_get_contents("php://input"))", because this page is managed by another company. The data is send by POST. I could try to send a similar XML by POST but I won't be able to reproduce the problem with the encoding.