Rui Rui - 1 year ago 128
JSON Question

Receive JSON response while POST urlencoded

I want to receive and decode JSON response after submitting urlencoded request by POST.

But all I got ($content) after running below is garbled characters.

I believe the server returns correct values (I checked via

PHP code is below.

$target_url = "";]
$headers = array(
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
'Accept-Language: en-us',
'Accept-Encoding: gzip, deflate',
'User-Agent: Appcelerator Titanium/3.5.0',
$data = array(
'id' => 'hogehoge',
'command' => 'renew'

$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data),
'header' => implode("\r\n", $headers),

$contents = file_get_contents($target_url, false, stream_context_create($options));
echo $contents;

Headers of response is

Connection: keep-alive
Content-Encoding: gzip
Content-Length: 32
Content-Type: application/json; charset=utf-8
Date: ***** GMT
Server: Apache
Vary: Accept-Encoding,User-Agent

When I echo $contents, I got


although it should be


Thanks in advance.

When I
,I got

Answer Source

In response, server is informing you that it is sending zipped content, you must unzip it.

Content-encoding: gzip

You can use gzdecode() to unzip gzip.

echo gzdecode($content);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download