Home php教程 php手册 Solution to the problem of no data (empty) when php reads json

Solution to the problem of no data (empty) when php reads json

Jul 11, 2016 am 08:43 AM

When using PHP to call some json interface files, if you use file_get_contents to obtain the page json data

After using json_decode() to parse, the data cannot be output normally. The return value is null

This is because there are three invisible BOM characters in front of the data obtained by PHP's file_get_contents. Transcoding PHP or setting the header encoding to no BOM still cannot solve the problem

One possible way is:

<span style="color: #800080;"><?php<br>$str</span> = <span style="color: #008080;">file_get_contents</span>('json接口地址'); <span style="color: #008000;">//</span><span style="color: #008000;">获取页面地址</span>
<span style="color: #800080;">$str</span> = <span style="color: #008080;">substr</span>(<span style="color: #800080;">$str</span>,3); <span style="color: #008000;">//</span><span style="color: #008000;">由于php问题file_get_contents得到的数据前面有三个看不到的BOM字符 使用substr函数提取第三个字符后的内容</span>
<span style="color: #800080;">$json</span> = json_decode(<span style="color: #800080;">$str</span>, <span style="color: #0000ff;">true</span>);<span style="color: #008000;">//</span><span style="color: #008000;">解析json代码 返回为 array 值</span>
<span style="color: #0000ff;">echo</span> <span style="color: #800080;">$json</span>['motd'];<span style="color: #008000;">//</span><span style="color: #008000;">以array 输出json中的 motd 数组</span>
Copy after login

Another one:

<?<span style="color: #000000;">php
</span><span style="color: #800080;">$str</span> = <span style="color: #008080;">file_get_contents</span>('json接口地址'); <span style="color: #008000;">//</span><span style="color: #008000;">获取页面地址</span>
<span style="color: #800080;">$json</span> = json_decode(<span style="color: #008080;">trim</span>(<span style="color: #800080;">$str</span>,<span style="color: #008080;">chr</span>(239).<span style="color: #008080;">chr</span>(187).<span style="color: #008080;">chr</span>(191)),<span style="color: #0000ff;">true</span>); <span style="color: #008000;">//</span><span style="color: #008000;">chr(239).chr(187).chr(191)在输出时 组成了utf8文件的bom头,之后用trim函数将其移除</span>
<span style="color: #008080;">print_r</span>(<span style="color: #800080;">$json</span>); <span style="color: #008000;">//</span><span style="color: #008000;">以array 输出json</span>
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)