Returning an xml as string, but when I print it, a

2019-09-02 14:30发布

问题:

im trying to read an xml as a string that i get from a json from a webservice, when I check the request the xml looks fine, but when I print said json from php, all the start tags are ignored. Is there a reason for this behavior?

{
    "response": "ok",
    "xmlData": "<Tag1><Tag2>data</Tag2><Tag3><Tag4>data</Tag4></Tag3></tag1>"
}

php output:

{
    "response":"ok",
    "xmlData":"data<\/Tag2>data<\/tag4><\/Tag3><\/tag1>"
}

function webPostRequest($resourceURL, $postContent)
{
    $url = $resourceURL;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postContent);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($ch);
    curl_close($ch);
    echo $result;
    return $result;
}

回答1:

Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings.

htmlspecialchars() is what you need to fix this



标签: php json xml rest