-1

I am using StackExchange get all answers API to my project. so that I am hitting the URL https://api.stackexchange.com/2.2/answers?page=10&pagesize=10&order=desc&sort=activity&site=stackoverflow While hitting in browser the original json shows, but the problem is using program.

PHP
<?php 
//step1
$cSession = curl_init(); 
//step2
curl_setopt($cSession,CURLOPT_URL,"https://api.stackexchange.com/2.2/answers?page=10&pagesize=10&order=desc&sort=activity&site=stackoverflow");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false); 
//step3
$result=curl_exec($cSession);
//step4
curl_close($cSession);
echo"<pre>";
var_dump($result);
echo "<br>";
echo"</pre>";

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($result, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:";
        echo"<br>";
    } else {
        echo "$key => $val";
        echo"<br>";
    }
}
?>
string(1194) "���mO�8ǿJ���m���=!��n�e�ǞN��84���K)��~NZ�4
�t���d����?�����O1��]�����;�=����:P�>�+�3eOb�U�f���1�O���T�G�Ȫt�E�ɴ�5ɣ�X�$�$Yj�խ,�T���dK�i��M��+k�Ǯ��#��r����HKٵJ_毀�.Jm�:�M�|m�!u���k���˼�$�r�,:u�����ϩ,�矗c��t�|�2�1u�@��ɨ��᨝�l6�G��Zu�_m�Cz����{4\脄Oȃ����'����C�W�9��a��U�2Qv.�~C��淋ܩ����'�|a�.�/��ۼ���� �l
��u8".}��P�Y�e�he��
!a�`����<���>>������Aė!�[�;�E.�1�\�E ���rcv��7�#�4[�V��J����q�� �$�̇S���_W��d�t�'�W��'y�m����8��x�)���ӫ^q8&�#�F�6���X��+�6�����O2������{�g��I(�εR[Ld���7K�޼rߍzC8h#;�]��0�M�P1�~A��l�N�G^��2dBB#!��!$����3������I� )��
�-���n�םH�7H�
�g���y�"E���ۘ��2b��O�f�^}�u9tf�����!Ð�t�4}O 
��MJ�[��iw�1n���uR��m=�6��<��Mw��]+�(��u��s��e��t>�~&�$7��FD�=��R�^�"

it shows the result like some encoded string, give the solutions for that. Thanks in advance.

udayasankar
  • 1
  • 1
  • 4
  • I don't see how the code above could produce the output below: The only `var_dump()` you do should return a resource or an error number. – jeroen Jun 13 '19 at 10:20
  • 1
    Looking at the documentation, it looks likely the response is GZIP, not basic ZIP? – Jonnix Jun 13 '19 at 10:22
  • after unzip it does not produce any results, but the actual result is [ https://api.stackexchange.com/2.2/answers?page=10&pagesize=10&order=desc&sort=activity&site=stackoverflow ] @Jonnix – udayasankar Jun 13 '19 at 10:40
  • thanks for your hint. @Jonnix – udayasankar Jun 13 '19 at 11:45

2 Answers2

0

Using 'gzdecode' function will decode from GZIP JSON

$ch = curl_init();
// Set the url
$url = "https://api.stackexchange.com/2.2/answers?page=10&pagesize=10&order=desc&sort=activity&site=stackoverflow";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3

$result = gzdecode($result); // Here Decoding 

print_r($result);

I received Out like below

{"items":[{"owner":{"reputation":1642,"user_id":6373435,"user_type":"registered", ... . . . 
Rasa Mohamed
  • 882
  • 1
  • 6
  • 14
0

Try this it's working to me check this URL with terminal it shows encode format:gzip. so that we need to encode that with the following line.

curl_setopt($cSession, CURLOPT_ENCODING, 'gzip');

Full code=>

$cSession = curl_init(); 
//step2
curl_setopt($cSession,CURLOPT_URL,"https://api.stackexchange.com/2.2/answers?page=10&pagesize=10&order=desc&sort=activity&site=stackoverflow");
curl_setopt($cSession, CURLOPT_ENCODING, 'gzip');//--------->>> Solution step
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
//curl_setopt($cSession,CURLOPT_HEADER, false); 
//step3
$result=curl_exec($cSession);
//step4
curl_close($cSession);
echo"<pre>";
var_dump($result);
echo "<br>";
echo"</pre>";

Thanks - Udayasankar.

udayasankar
  • 1
  • 1
  • 4