SoulieBaby
SoulieBaby

Reputation: 5471

using php to get a json feed

I'm trying to use php to get an external json feed to use within my site, I've tried using get_file_contents and also CURL. But both are returning the same thing.

�~�X�ݘ]O�0��'��\�fԳ�o�JK7>�h�!M ��l�S�d�B���iKHC�Pi�M���W��G>���I�YD��G��F���,F��O��3a7�E�C��GC���MB������Q�&�׿���O���ك+��tzf�c�/ ���<-���"1�!(۩�[F��w�m��a�8t}*�y��fBr�O;�{cۉ�s�B��~���l�� ��v��@^&��@���,ӊ#ԙ��/MO����$Lj��%,]�9Js��z�[I�<8w6�%���Ջ�b�y�;ӄ�f"UM�!�ipC�) �|���~����}p���w�R�7D�eޑ��Oΐ��LS�5��.�{[j�� 3|D�h�FB'b��=���G~H.�}D����� �4�ՇpGA;� VMl��<�|�,6�R,N}J�ߘ��d�x�a��.�O܊���Z���j�x����W燭���n��+<Ό�* �4 _N|�}�5�a��Aմ�����p��1V]��j����hxA<7,I. &Ϭ2�~y�LD�+6��z�QLQ�9T��A��V�5���L&�,�JNJfi��q5J��� +P��:�<�:���Q⋊ �[*0�Z(Q E��K�u��Ҍ�[�d�W��� �2&�˒���+�R�|�\"n6]_ �����z���(���������b3%,�T��r��U@��Z-�[��]լ(�k6S֚N��-;�{E��?�VET4`���V ��1�c���%�XJ�[L�0����ė��`$S�n�d\�\�k}�Z�G��B7 �^4J��k���?���W;

If I add json_decode, it echoes out nothing at all.

Here's the code I'm using:

        $apistr = 'https://remitradar.com/JsonRequests.aspx?action=getOnlineQuotes&companyKey=23e9b66aspp6z&countryFrom=AU&countryTo=FJ&currencyFrom=AUD&currencyTo=FJD&amount=200';

        #get file contents
        $json = file_get_contents($apistr);
        echo '<pre>';print_r($json);echo '</pre>';

        #curl 1
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,$apistr);
        curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        $jsonData = json_decode(curl_exec($ch));
        curl_close($ch);
        echo '<pre>CURL 1';print_r($jsonData);echo '</pre>';

        #curl 2
        $ch = curl_init($apistr);
        curl_setopt($ch,CURLOPT_MUTE,1);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:text/xml'));
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        $output = json_decode(curl_exec($ch));
        curl_close($ch);
        echo '<pre>CURL 2';print_r($output);echo '</pre>';

I'm not sure if the problem is on my end, or their end. But going to the URL directly works fine.

Upvotes: 1

Views: 69

Answers (1)

Robert I
Robert I

Reputation: 1527

Their server uses gzip compression You must handle this

    #curl 1
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$apistr);
    //curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_ENCODING , "gzip");  // handle gzip

    $data = curl_exec($ch);
    $data = json_decode($data);
    curl_close($ch);
    echo '<pre>CURL 1';print_r($data);echo '</pre>';

Upvotes: 3

Related Questions