apache_request_headers() 使用 PHP 获取特定值
apache_request_headers() get specific value with PHP
我打了一个 JSON 电话,return 电话:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'https://www.example.com/sdfsldfsd3/all-result.json');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('AccessKey:
dasdasdq312312qWQEQ'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
$json = curl_exec($ch);
curl_close($ch);
现在我想显示标题的前 5 个值:
string(906287) "{"data":[{"id":12238,"title":"Title no 1"},{"id":7510,"title":"Title no 2"},
etc.
我应该如何显示如下结果:
<a href=page.php?id=12238>Title no 1</a> <br>
<a href=page.php?id=7510>Title no 2</a> <br> etc
提前致谢
工作代码:
$json = curl_exec($ch);
$data = json_decode($json);
foreach ($data->data as $key => $row) {
if ($key === 5) break;
echo '<a href=page.php?id=' . $row->id . '>' . $row->title . '</a> <br>';
}
}
如果您需要写下所有链接,只需使用
<?php
$str = '{"data":[{"id":12238,"title":"Title no 1"},{"id":7510,"title":"Title no 2"}]}';
$data = json_decode($str);
foreach ($data->data as $row) {
echo '<a href=page.php?id=' . $row->id . '>' . $row->title . '</a> <br>';
}
如果您只需要 5 个链接,foreach
就是
foreach ($data->data as $key => $row) {
if ($key === 5) break;
echo '<a href=page.php?id=' . $row->id . '>' . $row->title . '</a> <br>';
}
我打了一个 JSON 电话,return 电话:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
'https://www.example.com/sdfsldfsd3/all-result.json');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('AccessKey:
dasdasdq312312qWQEQ'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
$json = curl_exec($ch);
curl_close($ch);
现在我想显示标题的前 5 个值:
string(906287) "{"data":[{"id":12238,"title":"Title no 1"},{"id":7510,"title":"Title no 2"},
etc.
我应该如何显示如下结果:
<a href=page.php?id=12238>Title no 1</a> <br>
<a href=page.php?id=7510>Title no 2</a> <br> etc
提前致谢
工作代码:
$json = curl_exec($ch);
$data = json_decode($json);
foreach ($data->data as $key => $row) {
if ($key === 5) break;
echo '<a href=page.php?id=' . $row->id . '>' . $row->title . '</a> <br>';
}
}
如果您需要写下所有链接,只需使用
<?php
$str = '{"data":[{"id":12238,"title":"Title no 1"},{"id":7510,"title":"Title no 2"}]}';
$data = json_decode($str);
foreach ($data->data as $row) {
echo '<a href=page.php?id=' . $row->id . '>' . $row->title . '</a> <br>';
}
如果您只需要 5 个链接,foreach
就是
foreach ($data->data as $key => $row) {
if ($key === 5) break;
echo '<a href=page.php?id=' . $row->id . '>' . $row->title . '</a> <br>';
}