I do a JSON call and return it With:
$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);
Now I would like to display the first 5 values of title:
string(906287) "{"data":[{"id":12238,"title":"Title no 1"},{"id":7510,"title":"Title no 2"},
etc.
How am I supposed to display a result like:
<a href=page.php?id=12238>Title no 1</a> <br>
<a href=page.php?id=7510>Title no 2</a> <br> etc
Thanks in advance
Working code:
$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>';
}
}
If you need to write down all links, use just
<?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>';
}
If you need just 5 links, foreach
would be
foreach ($data->data as $key => $row) {
if ($key === 5) break;
echo '<a href=page.php?id=' . $row->id . '>' . $row->title . '</a> <br>';
}