How to get a value from wunderground .json - php

I'm working on json source from wunderground.com. As the example code displayed in the document. I can adjust and work out for some simple format. But I'm stuck with this one. I tried to googled every where but there's no solution.
Here's the sample codes:
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/b8e924a8f008b81e/geolookup/conditions/q/IA/Cedar_Rapids.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'location'}->{'city'};
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
echo "Current temperature in ${location} is: ${temp_f}\n";
?>
Well, I need some information like "Cedar Rapids" out of pws/station :
"pws": {
"station": [
{
"neighborhood":"Ellis Park Time Check",
"city":"Cedar Rapids",
"state":"IA",
"country":"US",
"id":"KIACEDAR22",
"lat":41.981174,
"lon":-91.682632,
"distance_km":2,
"distance_mi":1
}
]
}
(You can get all code by clicking this : http://api.wunderground.com/api/b8e924a8f008b81e/geolookup/conditions/q/IA/Cedar_Rapids.json )
Now the questions are:
What is this data called? (array, array in array?)
How could I pull this data out of the line?
Regards,

station is an array within the pws object.
To get the data, you can do something like this:
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/b8e924a8f008b81e/geolookup/conditions/q/IA/Cedar_Rapids.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'location'}->{'city'};
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
echo "Current temperature in ${location} is: ${temp_f}\n";
$stations = $parsed_json->{'location'}->{'nearby_weather_stations'}->{'pws'}->{'station'};
$count = count($stations);
for($i = 0; $i < $count; $i++)
{
$station = $stations[$i];
if (strcmp($station->{'id'}, "KIACEDAR22") == 0)
{
echo "Neighborhood: " . $station->{'neighborhood'} . "\n";
echo "City: " . $station->{'city'} . "\n";
echo "State: " . $station->{'state'} . "\n";
echo "Latitude: " . $station->{'lat'} . "\n";
echo "Longitude: " . $station->{'lon'} . "\n";
break;
}
}
?>
Output:
Current temperature in Cedar Rapids is: 38.5
Neighborhood: Ellis Park Time Check
City: Cedar Rapids
State: IA
Latitude: 41.981174
Longitude: -91.682632

Related

Datetime parameter in PHP SOAP call using the SoapClient class

I've developed a PHP SOAP call with code bellow with datetime parameter.
Problem is that it displays "No Items found" if there is only one item, after the given time.
When i make the call for longer period, it shows all items accordinly.
Blockquote
Current DateTime is : 02-18-2020 13:46:20
Display products after DateTime : 02-18-2020 13:40:00
No Items found
No Items found
No Items found
...........
Thanks in advance for the help.
<?php
echo "Current DateTime is : " . date("m-d-Y H:i:s") . "<br/><br/>";
// $dateTime = date('m-d-Y H:i:s',strtotime('-40 minutes'));
$dateTimeCall = '02-18-2020 13:40:00' ;
echo "Display products after DateTime : " . "<b>" . $dateTime . "</b>" . "<br/><br/>";
$soap_client = new SoapClient('http://....... /eCommerceWebService.asmx?WSDL') ;
$params = array('SiteKey' => 'qwerty',
'Date' => $dateTimeCall,
'StorageCode' => '11111' ) ;
// GetProducts
$response = $soap_client->GetProducts($params) ;
$all_products = $response->GetProductsResult->StoreDetails ;
foreach( $all_products as $product ) {
$ItemId = $product->ItemId;
$model = $product->ItemCode;
$ItemDateModified = $product->ItemDateModified;
// echo $ItemId . " - " . $model . " - " . $ItemDateModified . "<br/>";
$date = date_create($ItemDateModified);
$dateTime = date_format($date, 'm-d-Y H:i:s');
if ($ItemId) {
echo $ItemId . " - " . $model . " - " . $dateTime . "<br/>";
}
else {
echo "No Items found " . "<br/>";
}
}
?>

Issues Comparing Different Timestamps

I'm using the Binance REST API to get previous trade information:
Endpoint: https://api.binance.com/api/v1/trades?symbol=BTCUSDT
$btc_trades = file_get_contents('https://api.binance.com/api/v1/trades?symbol=BTCUSDT');
$btc_trades = json_decode($btc_trades, true);
$five_minutes_ago = strtotime('-5 minutes');
echo "five minutes ago: " . $five_minutes_ago . "<br><br>";
foreach ($btc_trades as $btc_trade) {
$btc_trade_time = strtotime($btc_trade['time']);
$btc_trade_total = $btc_trade['qty'] * $btc_trade['price'];
if ($btc_trade_time >= $five_minutes_ago) {
$btc_trade_time = $btc_trade['time'];
echo "Time: " . $btc_trade_time . "<br>";
echo "Qty: " . $btc_trade['qty'] . "<br>";
echo "Price: $" . $btc_trade['price'] . "<br>";
echo "Total: " . $btc_trade_total . "<br><br>";
}
}
I can confirm by testing that there are trades returned that meet the condition (happened less than five minutes ago) however nothing is returned in this case.
Are the timestamps formatted differently? It seems as if I have everything correct.
From Binance API (https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md):
All time and timestamp related fields are in milliseconds.
Thanks for the help!
change to
$btc_trade_time = $btc_trade['time']/1000

PHP is it possible to assign a variable to an "IF"

I am starting on PHP, and i need a little help, I have my php file ytpost.php with the following content:
EDIT: I have my code a little bit disorganized,
<?php
$yturl = $_POST['urlyt']; //gets a youtube url
$yturlhttp = str_replace("https","http",$yturl); //replaces HTTPS with http
$ytid = substr(strstr($yturlhttp, 'http://www.youtube.com/watch?v='), strlen('http://www.youtube.com/watch?v=')); //removes everything before the video ID
$apikey = "blabla"; //youtube api
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=$ytid&key=$apikey"); //using youtube api to get video info
$json_data = json_decode($JSON, true);
$getvideoviews = $json_data['items'][0]['statistics']['viewCount'];
$getvideolikes = $json_data['items'][0]['statistics']['likeCount'];
$getvideodislikes = $json_data['items'][0]['statistics']['dislikeCount'];
$getvideofavorites = $json_data['items'][0]['statistics']['favoriteCount'];
$getvideocomments = $json_data['items'][0]['statistics']['commentCount'];
$paloma = "✔"; //checkmark in spanish
$tacha = "✘"; //cross in spanish
$minimumvideoviews = "4000"; //minimum video views to be accepted
$minimumvideolikes = "100"; //minimum video likes to be accepted
//echo "views: " . $getvideoviews . "<br/>"; //TESTING
//echo "likes: " . $getvideolikes . "<br/>"; //TESTING
//echo "dislikes: " . $getvideodislikes . "<br/>"; //TESTING
//echo "favorites: " . $getvideofavorites . "<br/>"; //TESTING
//echo "comments: " . $getvideocomments . "<br/>"; //TESTING
if ($getvideoviews < $minimumvideoviews) {
echo "$tacha you dont have the minimum views to qualify for VIP ($minimumvideoviews minimum)<br/>";
}
else{
echo "$paloma you do have the minimum views to qualify for VIP ($minimumvideoviews minimum)<br/>";
};
if ($getvideolikes < $minimumvideolikes) {
echo "$tacha you dont have the minimum likes on your video to qualify for VIP ($minimumvideolikes minimum)<br/>";
}
else{
echo "$paloma you do have the minimum likes on your video to qualify for VIP ($minimumvideolikes minimo)<br/>";
};
?>
my problem is the following, i need to display something if both youtube views and youtube likes are exactly or over $minimumvideoviews and $minimumvideolikes, thank you.
, I dont have an idea on how to accomplish this,

Apple warranty check script

After looking for some Ruby scripts i tried to write on PHP with some help the script.
My Problem is now that I am not sure if the jSon Objects are correct cause I dont know the source for it now.
My Question is if I am making anything wrong with jSon in PHP? If not than the Objects of the Sources are wrong.
<?php
$sn = isset($_GET['sn']) ? $_GET['sn'] : '';
if($sn)
{
$url = 'https://selfsolve.apple.com/warrantyChecker.do?sn='.$sn . "&country=USA";
$json = file_get_contents($url);
$json = substr($json, 5, -1);
$json_obj = json_decode($json);
if(isset($json_obj->ERROR_CODE))
{
echo $json_obj->ERROR_DESC;
}
else
{
echo "$json_obj->PROD_DESCR <img src=\"$json_obj->PROD_IMAGE_URL\" alt=\"\"><br>";
echo"Product Description: $json_obj->PROD_DESCR <br>";
echo"Purchase date: $json_obj->PURCHASE_DATE <br>";
echo"Warranty exp date: $json_obj->COVERAGE_DATE <br>";
}
}
?>
<form action="" method="get" accept-charset="utf-8">
<p><input name="sn" value="<?=$sn?>"><input type="submit" value="Lookup serial"></p>
</form>
Another way I have tried to do it is
<?php
$sn = $argv[1];
$data = json_decode(file_get_contents(
"https://selfsolve.apple.com/warrantyChecker.do?sn=". $sn . "&country=USA"));
echo "Product Description" .$data->PROD_DESCR."\n";
echo "Coverage for " . $sn . " ends on " . $data->COVERAGE_DATE . "\n";
?>
<?php
$sn = isset($_GET['sn']) ? $_GET['sn'] : '';
if($sn)
{
$url = 'https://selfsolve.apple.com/warrantyChecker.do?sn='.$sn . "&country=USA";
$json = file_get_contents($url);
//This line you are splitting the json form then it wont work
$json = substr($json, 5, -1);
$json_obj = json_decode($json);
if(isset($json_obj->ERROR_CODE))
{
echo $json_obj->ERROR_DESC;
}
else
{
echo "$json_obj->PROD_DESCR <img src=\"$json_obj->PROD_IMAGE_URL\" alt=\"\"><br>";
echo"Product Description: $json_obj->PROD_DESCR <br>";
echo"Purchase date: $json_obj->PURCHASE_DATE <br>";
echo"Warranty exp date: $json_obj->COVERAGE_DATE <br>";
}
}
?>

PHP looped data will not echo outside the while function OR how can I pass looped data by echoing out of the while function?

I have a loop of data that will only echo the loop inside the while function, but if i call/echo the looped data outside the while function, it only runs the 1st loop.
SAMPLE:
$num = mysql_num_rows($queryFromDB);
$i=0;
while($i < $num)
{
$field1= mysql_result($queryFromDB,$i,"field1");
$field2= mysql_result($queryFromDB,$i,"field2");
$bothFields = $field1 . " " . $field2 "\n";
// This will show 2 rows of data
echo $bothFields;
$i++;
// This will only show 1 row of data. How can I pass the looped data to another variable?
echo $bothFields;
}
The output that I wanted to show is:
TITLE/HEADER GOES HERE in the 1st Line
-1st Row of Data from DB
-2nd Row of Data from DB
Here's the actual code:
$num = mysql_num_rows($qWoundAssessment);
$i=0;
while ($i < $num)
{
$wndType = mysql_result($qWoundAssessment,$i,"wndType");
$wndNum = mysql_result($qWoundAssessment,$i,"wndNum");
$wndLocation = mysql_result($qWoundAssessment,$i,"wndLocation");
$wndStage = mysql_result($qWoundAssessment,$i,"wndStage");
$wndL = mysql_result($qWoundAssessment,$i,"wndL");
$wndD = mysql_result($qWoundAssessment,$i,"wndD");
$wndW = mysql_result($qWoundAssessment,$i,"wndW");
$wndAseptic = mysql_result($qWoundAssessment,$i,"wndAseptic");
$wndIrrigate = mysql_result($qWoundAssessment,$i,"wndIrrigate");
$wndIrrigateBox = mysql_result($qWoundAssessment,$i,"wndIrrigateBox");
$wndPat = mysql_result($qWoundAssessment,$i,"wndPat");
$wndCover = mysql_result($qWoundAssessment,$i,"wndCover");
$wndCoverBox = mysql_result($qWoundAssessment,$i,"wndCoverBox");
$wndSecure = mysql_result($qWoundAssessment,$i,"wndSecure");
$wndSecureBox = mysql_result($qWoundAssessment,$i,"wndSecureBox");
$wndQvisit = mysql_result($qWoundAssessment,$i,"wndQvisit");
$wnd = "-" . $wndType . " " . "#" . $wndNum . ", " . "LOCATION " . $wndLocation . ", " . "STAGE " . $wndStage;
$wndSize = "SIZE " . $wndL . "CM" . " X " . $wndW . "CM" . " X " . $wndD;
if($wndAseptic=="1"){$wndAsepticTech = "USING ASEPTIC TECHNIQUE";}
if($wndIrrigate=="1"){$wndIrrigateWith = "IRRIGATE WITH " . $wndIrrigateBox;}
if($wndPat=="1"){$wndPatDry = "PAT DRY";}
if($wndCover=="1"){$wndCoverWith = "COVER WITH " . $wndCoverBox;}
if($wndSecure=="1"){$wndSecureWith = "COVER WITH " . $wndSecureBox;}
if($wndQvisit=="1"){$wndQv = "Q VISIT";}
if(isset($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQv)){
$woundCare = implode(", ",array($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQv)) . "\n\n ";}
$wndCare .= $woundCare;
$i++;
}
$snWoundCare = "SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:" . "\n" . $wndCare;
if I echo $wndCare, it shows the "Undefined variable" error with the actual looped data. But if I pass this variable to PDF, it works.
SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:
-PRESSURE ULCER #1, LOCATION COCCYX, 3, SIZE 2.0CM X 1.5CM X 0.07, USING ASEPTIC TECHNIQUE, IRRIGATE WITH NORMAL SALINE, PAT DRY, COVER WITH AQUACEL AG, COVER WITH MEPILEX BORDER, Q VISIT
-SURGICAL WOUND #2, LOCATION (R) KNEE, , SIZE 29CM X 0CM X 0, USING ASEPTIC TECHNIQUE, IRRIGATE WITH NORMAL SALINE, PAT DRY, COVER WITH AQUACEL AG, COVER WITH MEPILEX BORDER, Q VISIT
================ CODE NOW WORKS!!! HERE's MY FINAL SOLUTION ======================
$num = mysql_num_rows($qWoundAssessment);
$i=0;
$storeMyData = array();
while($i < $num)
{
$wnd= "-" . mysql_result($qWoundAssessment,$i,"wndType") . " #" . mysql_result($qWoundAssessment,$i,"wndNum"). ", LOCATION " . mysql_result($qWoundAssessment,$i,"wndLocation") . ", STAGE " . mysql_result($qWoundAssessment,$i,"wndStage");
$wndSize = "SIZE " . mysql_result($qWoundAssessment,$i,"wndL") . "CM" . " X " . mysql_result($qWoundAssessment,$i,"wndW") . "CM" . " X " . mysql_result($qWoundAssessment,$i,"wndD") . "CM";
if(isset($rowWoundAssessment['wndAseptic'])){$wndAsepticTech = "USING ASEPTIC TECHNIQUE";}
if(isset($rowWoundAssessment['wndIrrigate'])){$wndIrrigateWith = "IRRIGATE WITH " . mysql_result($qWoundAssessment,$i,"wndIrrigateBox");}
if(isset($rowWoundAssessment['wndPat'])){$wndPatDry = "PAT DRY";}
if(isset($rowWoundAssessment['wndCover'])){$wndCoverWith = "COVER WITH " . mysql_result($qWoundAssessment,$i,"wndCoverBox");}
if(isset($rowWoundAssessment['wndSecure'])){$wndSecureWith = "SECURE WITH " . mysql_result($qWoundAssessment,$i,"wndSecureBox");}
if(isset($rowWoundAssessment['wndQvisit'])){$wndQvisit = "Q VISIT";}
$wndCare = implode (", ", array($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQvisit)). "\n\n";
// This will show 2 rows of data
$storeMyData[] = $wndCare ; // store current data in array
$i++;
}
/* this will echo your storedData of loop */
foreach($storeMyData as $prevData)
/* or join the data using string concatenation /
$allFinalData2 = "";
/ this will echo your storedData of loop */
foreach($storeMyData as $prevData)
{
$allFinalData2 = $allFinalData2.$prevData ; // keep on concatenating
}
echo "SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:" . "\n" . $allFinalData2;
thanks to DhruvPathak and Antonio Laguna! You guys are the best! Just made my day! jumps around the room
This should work:
<?php
$wndCare = '';
while ($row = mysql_fetch_assoc($qWoundAssessment)){
$wnd = '-'.$row['wndType'].' #'..$row['wndNum'].', LOCATION '.$row['wndLocation'].', STAGE '.$row['wndStage'];
$wndSize = 'SIZE '.$row['wndL'].'CM X '.$row['wndW'].'CM X '.$row['wndD'];
$wndAsepticTech = ($row['wndAseptic'] == 1) ? 'USING ASEPTIC TECHNIQUE' : '';
$wndIrrigateWith = ($row['wndIrrigate'] == 1) ? 'IRRIGATE WITH '.$row['wndIrrigateBox'] : '';
$wndPatDry = ($row['wndPat'] == 1) ? 'PAT DRY' : '';
$wndCoverWith = ($row['wndCover'] == 1) ? 'COVER WITH'.$row['wndCoverBox'] : '';
$wndSecureWith = ($row['wndSecure'] == 1) ? 'COVER WITH'.$row['wndSecureBox'] : '';
$wndSecureWith = ($row['wndSecure'] == 1) ? 'COVER WITH'.$row['wndSecureBox'] : '';
$wndQvisit = ($row['wndQvisit'] == 1) ? 'Q VISIT' : '';
$wndCare .= implode (", ", array($wnd, $wndSize, $wndAsepticTech, $wndIrrigateWith, $wndPatDry, $wndCoverWith, $wndSecureWith, $wndQv)). '\n\n';
}
$snWoundCare = "SN TO PROVIDE SKILLED NURSING VISITS FOR WOUND CARE:" . "\n" . $wndCare;
?>
The issue I see is that you were testing if all variables where previously setted and this could make strange things as you were stablishing them sometimes and sometimes don't.
I am not sure what you want to do with your data. It seems you want to store
all the data to use it outside the loop, then this is the way to go :
<?php
$num = mysql_num_rows($queryFromDB);
$i=0;
$storeMyData = array();
while($i < $num)
{
$field1= mysql_result($queryFromDB,$i,"field1");
$field2= mysql_result($queryFromDB,$i,"field2");
$bothFields = $field1 . " " . $field2 "\n";
// This will show 2 rows of data
echo $bothFields;
$storeMyData[] = $bothFields ; // store current data in array
$i++;
}
/* this will echo your storedData of loop */
foreach($storeMyData as $prevData)
{
echo $prevData."\n";
}
?>
$allFinalData = implode("",$prevData); // implode will join all the data as string
echo $allFinalData."\n" ;
/* or join the data using string concatenation */
$allFinalData2 = "";
/* this will echo your storedData of loop */
foreach($storeMyData as $prevData)
{
$allFinalData2 = $allFinalData2.$prevData ; // keep on concatenating
}
echo $allFinalData2,"\n";
?>

Categories