我正在调用一个返回json文本的web服务,它在开始时会产生一些垃圾“”、“”。任何帮助或指示都很感激。我对卷发选项有点生疏,这是我使用过的一些旧代码,我已经有一段时间没有做过这样的工作了。
当我通过浏览器调用web服务时,我得到了很好的json文本,如下所示。我删除了一些值,只生成了几行
{ "values": [[1511596680,3],[1511596740,2],[1511596800,0],[1511596860,6],[1511596920,0],[1511596980,0],[1511597040,0],[1511597100,0],[1511597160,0],[1511603220,0],[1511603280,0],[1511603340,0],[1511603400,0],[1511603460,0],[1511603520,0],[1511603580,0],[1511603640,0],[1511603700,0],[1511603760,0],[1511603820,0]]}当我通过充当包装器的php页面进行调用时。我在它面前得到了一些垃圾,这阻止了php在上面调用json_decode。被调用的url与我以前在浏览器中调用web服务时使用的相同。
,{“值”:
我调用web服务的php代码如下所示。我不确定$post_string是否是一个问题。url由格式?param=val¶m2=val2等格式的url字符串中传递的参数组成。
$contenttype = 'application/json';
$headers = array(
'Content-Type: ' . $contenttype,
'Content-Length: ' . strlen($post_string) /* this an empty string */
);
/* dump of headers
Array
(
[0] => Content-Type: application/json
[1] => Content-Length: 0
)
*/
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); // this is get */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (is_array($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch); // this contains the crap at the start */发布于 2017-11-25 11:17:42
我已经插入了下面的内容来封住拜德命令的标记。
$output =preg_replace(‘/x00-\x1Fx80-\xFF/’,'',$output);
感谢下面的链接
发布于 2017-11-25 21:26:36
“有趣的字符”是由UTF-8BOM引起的,这意味着字符串以EF BB BF信号开始,表示它是在UTF-8中编码的。
您可以像这样删除BOM:(jasonhao在另一个answer中找到):
//Remove UTF8 Bom
function remove_utf8_bom($text)
{
$bom = pack('H*','EFBBBF');
$text = preg_replace("/^$bom/", '', $text);
return $text;
}https://stackoverflow.com/questions/47485215
复制相似问题