1

what i want to do is : i have an CRM API which produce data in xml format,
i am using DOMDocument() to fetch the data into pieces. The problem is when i tried to load the data like : $dom->loadXML($data);

i got the error :

DOMDocument::loadXML() [domdocument.loadxml]: Input is not proper UTF-8

i google i got an alternative solution to use with file_get_contents() but still i am getting warning like:

enter image description here

here is the code that i am using to fetch the xml data:

$text = file_get_contents(stripslashes(utf8_decode($data)));

$doc = new DOMDocument();
$doc->loadXML($text);

and here i put my XML return data, not complete but just a small piece:

<?xml version="1.0" encoding="utf-8"?>
<whmcsapi version="5.0.3">
<action>getticket</action>
<result>success</result>
<ticketid>5767</ticketid>
<tid>409865</tid>
<c>NwLldOG6</c>
<deptid>2</deptid>
<deptname>Technical</deptname>
<userid>27476</userid>
<name>shirley b broyles (home)</name>
<email>sbroyles1@stx.rr.com</email>
<cc></cc>
<date>2012-08-27 19:52:12</date>
<subject>printer not working</subject>
<status>Customer-Reply</status>
<priority>High</priority>
<admin></admin>
<lastreply>2012-08-28 23:34:17</lastreply>
<flag>0</flag>
<service></service>
<replies>
<reply>

can anyone please tell me where is the problem? or any alternative ??

jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
  • 1
    Are you passing an XML string to `file_get_contents()` instead of a file name? – Wiseguy Aug 30 '12 at 06:32
  • @Wiseguy yes, i am passing the XML string, because i got the XML via CURL – jogesh_pi Aug 30 '12 at 06:33
  • @Wiseguy was being rhetorical ;p – Lawrence Cherone Aug 30 '12 at 06:35
  • file_get_contents is to get the content of a file. If you have it in a string already you could do $text = stripslashes(utf8_decode($data)); Although I'm not sure why you want to strip the slashes – Nin Aug 30 '12 at 06:36
  • the XML you show is invalid. I assume you are just showing an excerpt of it, but this way, we cannot find where the UTF-8 error occurs. – Gordon Aug 30 '12 at 07:12
  • @Gordon thanks, but the above XML is just a small part, because the CURL request provide the big collation of DATA, actually i got where was that UTF-8 error, the CURL provide the mailing replies in XML format. And the UTF-8 error is in message part. – jogesh_pi Aug 30 '12 at 07:19

3 Answers3

1

Okay, so let's strip down the problem. If $data contains invalid UTF-8, you should be thinking how to make it valid; one way (not sure if that works for you) is with utf8_encode() rather than utf8_decode() (which is used to turn UTF-8 into ISO-8859-1):

$doc = new DOMDocument();
$doc->loadXML(utf8_encode($data));

Otherwise you will need to find out which part of the text includes the bad input. I'll see what I can come up with.

Resources

Ensuring valid utf-8 in PHP

Community
  • 1
  • 1
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • But do it on your API serverm e,g, send out the correct encoding already. Doing the encoding fixes on the client side would be wrong (just noting because the answer focus on client but you wrote you create the XML yourself). – hakre Aug 30 '12 at 06:51
  • @jogesh_p hakra makes a good point; if you have control over the XML creation, fix it there! – Ja͢ck Aug 30 '12 at 07:00
  • @Jack thanks, but i don't have control over the XML creation, i get this XML data via CURL request from a CRM API. I didn't mentioned that CURL code in the above question.. – jogesh_pi Aug 30 '12 at 07:07
0

you can try simplexml_load_file php function..

$xml = simplexml_load_file("addinsrss.xml");
echo "<pre>";print_r($xml);echo "</pre>";
Rukmi Patel
  • 2,619
  • 9
  • 29
  • 41
0

If you already have the xml source then dont pass it to FGC. Pass it directly to domDocument.

Or you can also use simplexml_load_string(),

<?php
$xml = '<?xml version="1.0" encoding="utf-8"?>
<whmcsapi version="5.0.3">
<action>getticket</action>
<result>success</result>
<ticketid>5767</ticketid>
<tid>409865</tid>
<c>NwLldOG6</c>
<deptid>2</deptid>
<deptname>Technical</deptname>
<userid>27476</userid>
</whmcsapi>
';

$xml = simplexml_load_string($xml);

print_r($xml);
/*
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [version] => 5.0.3
        )

    [action] => getticket
    [result] => success
    [ticketid] => 5767
    [tid] => 409865
    [c] => NwLldOG6
    [deptid] => 2
    [deptname] => Technical
    [userid] => 27476
    ...
    ...
)
*/
echo $xml->tid; //409865 
?>

Or directly use $xml = simplexml_load_file('http://example.com/somexml.xml'); to load from an external url.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • Thanks for your help, but i get the XML output via CURL request, anyway @Jack answer solved my problem,, Again Thanks. – jogesh_pi Aug 30 '12 at 06:52