php - 如何用php中的变量替换url值?并在获取数组中获得动态结果?
问题描述
我想让数字“12345”和日期“02-07-2018”动态从文本框中获取它的变量值..我也想在数组中获取结果(动态)..像mysql_fetch_array(结果)..如何实现这个??..任何帮助都会得到
<?php
$json = '';
//url
$url = 'http://api.website.com/account/12345/date/02-07-2018/apikey/api_key/';
$content = file_get_contents($url);
$json = json_decode($content, true);
$acc_name = $json['account']['name'];
$acc_number = $json['account']['number'];
?>
<table width='100%' border=1>
<thead>
<tr bgcolor='#00ffcc'>
<th>account name</th>
<th>account number</th>
</tr>
</thead>
<tbody>
<td> <?php echo $acc_name; ?></td>
<td><?php echo $acc_number; ?></td>
解决方案
我希望这是您检索所需要的。
$number = $_POST['number'] ;
$date = $_POST['date'] ; /* format if necessary */
$url = "http://api.website.com/account/$number/date/$date/apikey/api_key/";
要迭代,您可以使用
<?php
foreach( $json['account'] as $value ) {
$acc_name = $value['name'];
$acc_number = $value['number'];
?>
<td> <?php echo $acc_name; ?></td>
<td><?php echo $acc_number; ?></td>
<?php } ?>
推荐阅读
- javascript - 异步消息还是(消息)?- 不和谐.js
- .net-core - Blazor 未接收在服务器端创建的信号器
- java - SpringBoot Admin 中的 Hazelcast 通过 Docker Swarm 运行 2 个实例
- solr - 将两个或多个字段复制到 Solr 中的一个字段中
- android - 无法为 /DCIM/Camera/.thumbnails 创建 .thumbs 目录
- wordpress - Wordpress:带有“关闭菜单”文本的白条出现在我的页脚下方并且无法删除
- asp.net - 拆分 ascx 文件以实现可维护性
- abap - cmd_ei_api=>maintain_bapi 更新 kna1 表字段 (ICMSTAXPAY)
- c# - C# 返回带有事件的对象
- javascript - 存储事件侦听器不在当前窗口上执行