json data using PHP looks like corrupted - php

I'm trying to get JSON data from https://offer-1.betting.superbet.pl/matches/getMatchesByIds?matchIds=2885636
I'm using this code in PHP:
$link = "https://offer-1.betting.superbet.pl/matches/getMatchesByIds?matchIds=2885636";
$json = file_get_contents($link);
$json_data = json_decode($json);
but
var_dump($json_data);
returns NULL
Saving this file using file_put_contents(); file gives me a file that looks something like this:
xÚ͎̽\I’.ö*ƒ\ö”nżnĆĄA#.p—-ü·§4ŐĹFUM.Fó\ÚëÉ„dć9‘á<Af{î†?ÁĚä÷ą›ŮgînnţOă·ß>ýöôq–_~?=őňGů§_ź>ţÇSűôëż}úĺ—ńŰÓǧż”?ÚżŚßź~zúËřă_>ő§ŹOüźź?ý_ţÇ?őíźÚ§żüő·ńűď?úőéăżýŰřéó÷m˙úń‰ÜŐŘžţóóňôńź˙ăé/??}üňůOOŮ~*%J€D˙€ö‘ÓÇ”ž~zúăg|úřdxů=}|RŐí/iO5B~zúů×öŰřËřőŹęOłädA?=}šÇ˙â÷źź>ęOOí秏čyűQOM䧧ß?=}D
I also tried using curl, but it doesn't work as well:
$link = "https://offer-1.betting.superbet.pl/matches/getMatchesByIds?
matchIds=2885636";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $link);
$result = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
What am I forgeting about?

Try this:
$link = "https://offer-1.betting.superbet.pl/matches/getMatchesByIds?matchIds=2885636";
// Output is deflate-compressed. Use gzuncompress() to unpack.
$json = gzuncompress(file_get_contents($link));
$json_data = json_decode($json);

Related

Is there a way to get value of json object from a URL in PHP?

So i am trying to get values of json object from a url, when i hit that url on post man i get something like this
{
"error": "0",
"errorString": "",
"reply": {
"nonce": "5e415334832a8",
"realm": "VMS"
}
}
So, i am trying to write a php code that displays the value of nonce in the browser but it is not working
i have the following code
$getNonceUrl = "https://example.com/api/getNonce";
$getContect = file_get_contents($getNonceUrl);
$jsonNoce = json_decode($getContect, true);
$dd = $jsonNoce->reply[0]->nonce;
echo $dd;
I also did this
$ch = curl_init("https://example.com/api/getNonce");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
echo $ch->reply[0]->nonce;
But it does not seem to work still.
Below your 2 code samples with suggested corrections.
Using file_get_contents : remove 2nd argument of json_decode
$getNonceUrl = "https://example.com/api/getNonce";
$getContect = file_get_contents($getNonceUrl);
$jsonNoce = json_decode($getContect); // note removal of 2nd parameter
$dd = $jsonNoce->reply[0]->nonce;
echo $dd;
Using curl : you forgot to parse curl output with json_decode
$ch = curl_init("https://example.com/api/getNonce");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$data = json_decode($data); // added this line, because curl doesn't decode json automatically
echo $ch->reply[0]->nonce;

Print JSON data from currencyconverterapi

I'm trying to print the value from currency converter API JSON data.
Anyone can help me to print the value from this URL
https://free.currencyconverterapi.com/api/v5/convert?q=USD_IDR&compact=y?
You have to use file_get_contents() along with json_decode()
<?php
$json_data = file_get_contents('https://free.currencyconverterapi.com/api/v5/convert?q=USD_IDR&compact=y');
$array = json_decode($json_data, true);
var_dump($array["USD_IDR"]["val"]);
?>
I have tested it on the local machine and working fine:-
https://prnt.sc/jd1kxo And https://prnt.sc/jd1l7w
Use Json_decode
$data = json_decode('{"USD_IDR":{"val":13965}}', TRUE);
var_dump($data["USD_IDR"]["val"]); //int(13965)
Try this:
ob_start();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://free.currencyconverterapi.com/api/v5/convert?q=USD_IDR&compact=y');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$jsontoarr = json_decode($response);
echo $jsontoarr->USD_IDR->val;
Good luck.

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.
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>';

Json decode comes up as NULL

#input url
$url = 'http://www.example.com';
#get the data
$json = file_get_contents($url);
$contents = utf8_encode($json);
#convert to php array
$php_array = json_decode($json);
var_dump($php_array);
exit;
I'm trying to decode a website but once I decode it my page comes up as NULL, does anyone know how I can fix it? Thanks
In your case http://www.example.com this URL returns 404 error. so file_get_contents($url) get null value.
$url = 'http://www.example.com';
$json = file_get_contents($url); // HTTP 404
echo $json; //returns null
This works fine
<?php
$url = 'http://www.example.com';
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$php_array = json_decode($output, true); // true for returning to an array
echo "<pre>";
print_r($php_array);
echo "</pre>";
Extra Tip:
I too faced null issue sometimes. You could ask json_last_error() to get definite information.
You can use CURL request here:
Example:
<?php
$url = 'http://www.example.com'; // your url
$ch = curl_init(); // initiate
curl_setopt($ch, CURLOPT_URL,$url); // curl url
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch); // curl output
$php_array = json_decode($result,true);
var_dump($php_array);
?>
Output:
array(3) { ["timestamp"]=> string(8) "10:38:38" ["error_num"]=> int(404) ["error_msg"]=> string(20) "File Not Found Error" }
CURL give you more options to fetch remote content and error checking than to file_get_content.

Post json object with php(http post)?

I need to post json object with http post request and to handle responses.
json object :
{
"my_json" : "12345"
}
I wrote somethinh like this,but this don't work.
$url = "http://localhost/my_json.json";
$json_Data = file_get_contents($url,0,null,null);
print_r($json_Data);
And it doesn't print anything.
Help please.
Client:
<?php
$data = array('foo' => 'bar', 'red' => 'blue');
$ch = curl_init();
$post_values = array( 'json_data' => json_encode( $data ) );
curl_setopt($ch, CURLOPT_URL, 'http://localhost/server.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_values);
$data = curl_exec($ch);
if(!curl_errno($ch))
{
echo 'Received raw data' . $data;
}
curl_close($ch);
?>
Server (server.php):
<?php
$data = json_decode( $_POST['json_data'] );
// ... do something ...
header('Content-type: text/json');
print json_encode($response);
?>
$url = "http://localhost/my_json.json";
$json_Data = file_get_contents($url,0,null,null);
$new = json_decode($json_Data);
print_r($new);
I think that might do it
Try this:
$jsonFile = 'http://localhost/my_json.json';
$jsonData = file_get_contents($jsonFile);
$phpData = json_decode($jsonData);
print_r($phpData);
The problem may be from the file_get_contents extraneous arguments :
The 2d arg should be a boolean and is optional (default value is false)
The 3rd arg is ok
The 4th should be an integer, is optional (default value is -1)
So you should try $json_Data = file_get_contents($url);
Furthermore to view the data in your browser you should try with header('Content-type: text/plain'); just before outputting with print_r() so that no processing will be made by your browser
To be sure there is really nothing sent to your browser, you may also try FireFox + FireBug to see HTTP replies...

Categories