1

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.

2
  • 1
    It looks like you can simply use the file_get_contents()' here. The URL loads fine in my browser. Try just echoing your $json variable without the <pre> tags Commented Mar 29, 2017 at 0:51
  • 1
    # get file contents works fine as well. you need to print_r(json_decode($json)); to see it. Commented Mar 29, 2017 at 1:02

1 Answer 1

3

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>';
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, gzip wouldn't have crossed my mind!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.