I use to go to school for only a short time in programming. A while ago and I'm rusty. I've been trying to re-learn everything by myself and something is bothering me. I'm trying to print out a specific object from an external API but nothing I try seems to work out. I don't really know what to google to get the right answer I am looking for. Anyway here's my code.
<?php
$url = 'http://apis.is/flight?language=en&type=departures';
$json = file_get_contents($url);
$results = json_decode($json, TRUE);
for ($x = 0; $x < count($results); $x++) {
echo $results[$x]['results']['flightNumber']."<br/>";
}
?>
If you do a debug (by the way, learn what is it), you will see, that your $results has one key: result, over which you can iterate with a simple foreach:
foreach ($results['result'] as $item) {
echo $item['flightNumber'];
}
You are trying to access the data returned from the API in the wrong order, do this instead:
<?php
$url = 'http://apis.is/flight?language=en&type=departures';
$json = file_get_contents($url);
$results = json_decode($json, TRUE);
// To loop through an array, use foreach instead of for
// It is easier to use
foreach($results['results'] as $result){
echo $result['flightNumber'].'<br />';
}
?>
<?php
$url = 'http://apis.is/flight?language=en&type=departures';
$json = file_get_contents($url);
$results = json_decode($json, TRUE);
foreach ($results['results'] as $res) {
echo $res['flightNumber']."<br/>";
}
?>
Related
how can I output this array into html table?
for each row I would like to output it like this, within the foreach;
echo "<td>".$lat."</td><td>".$long."</td>";
as per example on https://developer.here.com/documentation/routing-waypoints/topics/quick-start-simple-car.html
I have tried the code
$api_url = "https://wse.api.here.com/2/findsequence.json?start=Berlin-Main-Station;52.52282,13.37011&destination1=East-Side-Gallery;52.50341,13.44429&destination2=Olympiastadion;52.51293,13.24021&end=HERE-Berlin-Campus;52.53066,13.38511&mode=fastest;car&app_id=ID&app_code=CODE";
$api_response = file_get_contents($api_url);
$api_response_decoded = json_decode($api_response, true);
foreach($api_response_decoded as $api_response_decoded_row){
print_r($api_response_decoded_row[0][waypoints]);
}
and also tried
print_r($api_response_decoded_row[0][waypoints][id]);
and also tried
echo($api_response_decoded_row[0][waypoints][id]);
and also tried
implode($api_response_decoded_row[0][waypoints][id]);
Here's one way you could do it if the comments didn't already help you enough.
foreach($api_response_decoded as $api_response_decoded_rows){
foreach ($api_response_decoded_rows[0]['waypoints'] as $waypoint) {
$html = '
<td>'.$waypoint['lat'].'</td>
<td>'.$waypoint['lng'].'</td>
';
echo $html;
}
}
Thanks to commenters and answerers. In case it helps someone else, full working code is therefore;
$api_url = "https://wse.api.here.com/2/findsequence.json?start=Berlin-Main-Station;52.52282,13.37011&destination1=East-Side-Gallery;52.50341,13.44429&destination2=Olympiastadion;52.51293,13.24021&end=HERE-Berlin-Campus;52.53066,13.38511&mode=fastest;car&app_id=ID&app_code=CODE";
$api_response = file_get_contents($api_url);
$api_response_decoded = json_decode($api_response, true);
echo "<table>";
foreach($api_response_decoded as $api_response_decoded_rows){
foreach ($api_response_decoded_rows[0]['waypoints'] as $waypoint) {
$html = '<tr><td>'.$waypoint['sequence'].'</td><td>'.$waypoint['id'].'</td><td>'.$waypoint['lat'].'</td><td>'.$waypoint['lng'].'</td></tr>';
echo $html;
}
}
echo "</table>";
Hi I'm trying get a json from fixer.io and then for each rates echo it but cant get it to work.
the code are
<?php
function usd(){
echo 'HEJ test';
$fixer_access_key = my_access_key;
$url= 'https://data.fixer.io/api/latest?access_key=' . $fixer_access_key;
echo $url;
$json = file_get_contents($url);
$data = json_decode($json);
echo $url . "<br>";
echo 'printing json foreach <br>';
foreach($data as $obj){
echo '...';
$prefix = $obj;
echo $prefix;
echo '<br>';}
echo 'done printing json foreach';
}
usd(); ?>
and the result are:
https://data.fixer.io/api/latest?access_key=my_fixer_key
printing json foreach
done printing json foreach
instead of
$data = json_decode($json);
use
$data = json_decode($json, true);
This should allow foreacha to works - however you will only see first level of json object keys (not nested ones). The second parameter of json_decode change result from object to array.
You will also need to change foreach - to following: foreach($data as $key => $obj) and inside it echo $obj to echo $key;.
Here is simplified working example.
ALTERNATIVE SOLUTION
If working foreach is not your goal but rather pretty printed json, then instead use following code:
$json_string = json_encode($data, JSON_PRETTY_PRINT);
echo $json_string;
I am trying to pull values from the following url: https://api.binance.com/api/v1/ticker/allPrices
What I want to accomplish:
Obtain all the "symbol" values printed in the website (one under the other)
The result I am looking for, would be:
ETHBTC
LTCBTC
...
This is what I have done so far:
<?php
$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);
for ($i=0; $i < count($json_data); $i++) {
# code...
print_r($json_data);
}
?>
The result it brings is the entire data (the symbols, prices, plus other irrelevant data).
Thanks in advance.
You can use foreach loop, then echo the value by symbol key
$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);
foreach ($json_data as $value) {
echo $value["symbol"] . '<br>';
}
This ought to do it:
<?php
$url = "https://api.binance.com/api/v1/ticker/allPrices";
$content = file_get_contents($url);
$json_data = json_decode($content, true);
for ($i=0; $i < count($json_data); $i++) {
echo $json_data[$i]['symbol'] . "\n";
}
?>
json_data["symbol"]
instead in the print
I am trying to scrap the categories using cURL and Regex. But the code that I have only extract one of the categories (Arts, Antiques & Collectibles).
This is the code I have:
<?php
$curl = curl_init('http://www.lelong.com.my/Auc/List/BrowseAll.asp');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$page = curl_exec($curl);
if(curl_errno($curl)) // check for execution errors
{
echo 'Scraper error: ' . curl_error($curl);
exit;
}
curl_close($curl);
$regex = '/<span class=CatLevel1>(.*?)<\/a>/s';
if ( preg_match($regex, $page, $list) )
echo $list[0]. "<br>";
else
print "Not found";
?>
Can anyone help me correct this code to extract all the categories(without the numbers)? I've been stuck on this for a long time.
Thanks!
Sample output:
Arts, Antiques & Collectibles
B2B & Industrial Products
Baby
etc....
here's a working code with DOMDocument and DOMXPath classes
$grep = new DoMDocument();
#$grep->loadHTMLFile("http://www.lelong.com.my/Auc/List/BrowseAll.asp");
$finder = new DomXPath($grep);
$class = "CatLevel1";
$nodes = $finder->query("//*[contains(#class, '$class')]");
foreach ($nodes as $node) {
$span = $node->childNodes;
echo $span->item(0)->nodeValue."<br>"
}
I read the comment to your question suggesting a different approach and an alternative answer that probably are better suited for this job, but if you still want to do it this way you need to do a global search (preg_match_all()) so it doesn't stop when it finds the first match and then use a loop to print the contents of the array where the results are saved. I haven't used cURL and can't test it and php isn't my strong, but the code should be something like:
if ( preg_match_all($regex, $page, $list) )
$i = 0;
while(isset($list[1][$i])) {
echo $list[1][$i]. "<br>";
$i++;
}
else
print "Not found";
Sorry for any mistakes in the code.
Currently trying to get this code to print everything in the array but there is a small
problem.
$json = file_get_contents('https://api.twitch.tv/kraken/channels/zettslive/follows?limit=10&offset=0');
$vid_arr = json_decode($json,true);
$vid_count = count($vid_arr);
foreach ($vid_arr as $vid)
{
echo '<img src="'.$vid[1]['user']['logo'].'">';
}
I have to use the [1] to even get it to work. and I want to be able to echo them all. and not do them one at a time.
$json = file_get_contents('https://api.twitch.tv/kraken/channels/zettslive/follows?limit=10&offset=0');
$vid_arr = json_decode($json,true);
$vid_count = count($vid_arr);
foreach ($vid_arr['follows'] as $follow)
{
echo '<img src="'.$follow['user']['logo'].'">';
}