【发布时间】:2019-08-02 18:18:46
【问题描述】:
我正在尝试使用 CURL 请求将 xml 转换为 json,但特殊字符未正确解码。
下面是我的代码
function APIRequest($zip) {
$URL = "http://www.example.com";
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "test", // name of client
CURLOPT_AUTOREFERER => true, // set referrer on redirect
);
$ch = curl_init($URL);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string(utf8_encode($response));
$json = json_encode($xml);
$json_response = json_decode($result);
return $json_response;
}
【问题讨论】:
-
然后呢?我们应该猜测输入和输出?
-
源代码中的编码是什么?打印 JSON 时使用什么编码?也检查这个答案:stackoverflow.com/a/6607228/1346234