Jump to content
techker

Json decode URL

Recommended Posts

Posted (edited)

Hey guys i got an api and it returns :

[  
   {  
      "id":"1",
      "package_name":"1 Month Full + Adult - 2 Devices",
      "is_trial":"0",
      "is_official":"1",
      "trial_credits":"0",
      "official_credits":"1",
      "trial_duration":"24",
      "trial_duration_in":"hours",
      "official_duration":"1",
      "official_duration_in":"months",
      "groups":"[4,5,6,8]",
      "bouquets":"[27]",
      "can_gen_mag":"1",
      "only_mag":"0",
      "output_formats":"[1,2,3]",
      "is_isplock":"0",
      "max_connections":"2",
      "is_restreamer":"0",
      "force_server_id":"0",
      "can_gen_e2":"1",
      "only_e2":"0",
      "forced_country":"",
      "lock_device":"0"
   },......]

	 
	$json = file_get_contents(URL);
	$data = json_decode($json,true);
	$Decoded = $data['id'][0];       HERE no mater what i put i get and error Undefined index: id in /home/handshak/public_html/XC/get2.php on line 9
	echo "<pre>";
	print_r($Decoded);
	exit;
	

What type of json structure is that?

EDIT:

if i use:

$contents = file_get_contents($json); 
$contents = utf8_encode($contents); 
$results = json_decode($contents);

i get:

[14-Apr-2019 15:38:32 UTC] PHP Warning:  file_get_contents([{&quot;id&quot;:&quot;1&quot;,&quot;package_name&quot;:&quot;1 Month Full + Adult - 2 Devices&quot;,&quot;is_trial&quot;:&quot;0&quot;,&quot;is_official&quot;:&quot;1&quot;,&quot;trial_credits&quot;:&quot;0&quot;,&quot;official_credits&quot;:&quot;1&quot;,&quot;trial_duration&quot;:&quot;24&quot;,&quot;trial_duration_in&quot;:&quot;hours&quot;,&quot;official_duration&quot;:&quot;1&quot;,&quot;official_duration_in&quot;:&quot;months&quot;,&quot;groups&quot;:&quot;[4,5,6,8]&quot;,&quot;bouquets&quot;:&quot;[27]&quot;,&quot;can_gen_mag&quot;:&quot;1&quot;,&quot;only_mag&quot;:&quot;0&quot;,&quot;output_formats&quot;:&quot;[1,2,3]&quot;,&quot;is_isplock&quot;:&quot;0&quot;,&quot;max_connections&quot;:&quot;2&quot;,&quot;is_restreamer&quot;:&quot;0&quot;,&quot;force_server_id&quot;:&quot;0&quot;,&quot;can_gen_e2&quot;:&quot;1&quot;,&quot;only_e2&quot;:&quot;0&quot;,&quot;forced_country&quot;:&quot;&quot;,&quot;lock_device&quot;:&quot;0&quot;},{&quot;id&quot;:&quot;2&quot;,&quot;package_name&quot in /home/handshak/public_html/XC/get2.php on line 7
 

Edited by techker

Share this post


Link to post
Share on other sites

print_r will print an array. Instead use 'var_dump' and you will see exactly what it is.

Share this post


Link to post
Share on other sites

Did you try

echo $data[0]['id'];

 

Share this post


Link to post
Share on other sites

That was what I was hoping the OP would see.

Share this post


Link to post
Share on other sites

When I decoded and used print_r I got this

 $data Array
(
    [0] => Array
        (
            [id] => 1
            [package_name] => 1 Month Full + Adult - 2 Devices
            [is_trial] => 0
            [is_official] => 1
            [trial_credits] => 0
            [official_credits] => 1
            [trial_duration] => 24
            [trial_duration_in] => hours
            [official_duration] => 1
            [official_duration_in] => months
            [groups] => [4,5,6,8]
            [bouquets] => [27]
            [can_gen_mag] => 1
            [only_mag] => 0
            [output_formats] => [1,2,3]
            [is_isplock] => 0
            [max_connections] => 2
            [is_restreamer] => 0
            [force_server_id] => 0
            [can_gen_e2] => 1
            [only_e2] => 0
            [forced_country] => 
            [lock_device] => 0
        )

)

If he didn't see it there it's unlikely a var_dump would have clarified things

Share this post


Link to post
Share on other sites

hey guys thx for the help.i will test with var dump and see. So its an array....

Share this post


Link to post
Share on other sites
Posted (edited)

The JSON object is in an array. See barand's first post.

Edited by gw1500se

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.