下面是mashape api,这里我希望api输出应该在表中。
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"X-Mashape-Key: XXXXXXXXXX"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$res = file_get_contents('https://sphirelabs-indian-telecom-data-recharge-plans-v1.p.mashape.com/telecomdata/v1/get/index.php?circle=tn&opcode=idea&type=Topup', false, $context);
print_r(json_decode($res, true));
输出:Array ( [0] => Array ( [recharge_amount] => 10 [recharge_talktime] => 7.90 [recharge_validity] => N.A [recharge_description] => Idea Talktime of Rs. 7.9 on a recharge of Rs. 10 [recharge_type] => Topup [recharge_operator] => Idea [recharge_circle] => TamilNadu [recharge_isRecommended] => TRUE ) [1] => Array ( [recharge_amount] => 20 [recharge_talktime] => 15.80 [recharge_validity] => N.A [recharge_description] => Idea Talktime of Rs. 15.8 on a recharge of Rs. 20 [recharge_type] => Topup [recharge_operator] => Idea [recharge_circle] => TamilNadu [recharge_isRecommended] => FALSE ) [2] => Array ( [recharge_amount] => 25 [recharge_talktime] => 19.75 [recharge_validity] => N.A [recharge_description] => Idea 2 Local Idea Night Minutes (11pm to 6am) | Tariff Validity (Days): 2 [recharge_type] => Topup [recharge_operator] => Idea [recharge_circle] => TamilNadu [recharge_isRecommended] => FALSE ) [3] => Array ( [recharge_amount] => 30 [recharge_talktime] => 23.70 [recharge_validity] => N.A [recharge_description] => Idea Talktime of Rs. 23.7 on a recharge of Rs. 30 [recharge_type] => Topup [recharge_operator] => Idea [recharge_circle] => TamilNadu [recharge_isRecommended] => FALSE ) [4] => Array ( [recharge_amount] => 35 [recharge_talktime] => 35.00 [recharge_validity] => N.A [recharge_description] => Idea 2 Local Idea Night Minutes (11pm to 6am) | Talktime Validity (Days): 5 | Tariff Validity (Days): 5 [recharge_type] => Topup [recharge_operator] => Idea [recharge_circle] => TamilNadu [recharge_isRecommended] => FALSE ) [5] => Array ( [recharge_amount] => 36 [recharge_talktime] => 29.04 [recharge_validity] => N.A [recharge_description] => Idea 2 Local Idea Night Minutes (11pm to 6am) | Tariff Validity (Days): 2 [recharge_type] => Topup [recharge_operator] => Idea [recharge_circle] => TamilNadu [recharge_isRecommended] => FALSE ) [6] => Array ( [recharge_amount] => 50 [recharge_talktime] => 41.50 [recharge_validity] => N.A [recharge_description] => Idea Talktime of Rs. 41.5 on a recharge of Rs. 50 [recharge_type] => Topup [recharge_operator] => Idea [recharge_circle] => TamilNadu [recharge_isRecommended] => TRUE ) [7] => Array ( [recharge_amount] => 55 [recharge_talktime] => 55.00 [recharge_validity] => N.A [recharge_description] => Idea 2 Local Idea Night Minutes (11pm to 6am) | Tariff Validity (Days): 2 [recharge_type] => Topup [recharge_operator] => Idea [recharge_circle] => TamilNadu [recharge_isRecommended] => TRUE ) [8] => Array ( [recharge_amount] => 100 [recharge_talktime] => 100.00 [recharge_validity] => N.A [recharge_description] => Idea Full Talktime [recharge_type] => Topup [recharge_operator] => Idea [recharge_circle] => TamilNadu [recharge_isRecommended] => TRUE ) )
你可以这样做:
$res = file_get_contents('https://sphirelabs-indian-telecom-data-recharge-plans-v1.p.mashape.com/telecomdata/v1/get/index.php?circle=tn&opcode=idea&type=Topup', false, $context);
$data = json_decode($res, true);
if (count($data) > 0): ?>
<table>
<thead>
<tr>
<th><?= implode('</th><th>', array_keys(current($data))); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): ?>
<tr>
<?php foreach ($row as $item): ?>
<td><?= $item ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>