array(
'header' => "Authorization: Basic " . base64_encode("$user:$password"),
),
));
$result = file_get_contents($tableUrl, true, $context);
$headers=get_headers($context);
$responseCode= substr($headers[0], 9, 3);
if($responseCode != "200")
{
echo "
";
echo "";
echo "Error | ";
echo "Error: Received response code " . $responseCode . " | ";
echo "
";
echo "
";
exit();
}
$content=file_get_contents($tableUrl);
$jsonContent = json_decode($content);
$metadataKey="Meta Data";
$timeSeriesKey="Time Series (Daily)";
$symbolKey="2. Symbol";
$timeKey="3. Last Refreshed";
$openKey="1. open";
$closeKey="4. close";
$volKey="5. volume";
$lowKey="3. low";
$highKey="2. high";
$symbol="";
$timestamp="";
$open="";
$close="";
$vol="";
$prevClose="";
$range="";
if(property_exists($jsonContent, "Error Message"))
{
echo "
";
echo "";
echo "Error | ";
echo "Error: NO record has been found, please enter a valid symbol | ";
echo "
";
echo "
";
exit();
}
if($jsonContent->$metadataKey != null)
{
$symbol=$jsonContent->$metadataKey->$symbolKey;
//$symbol=strtoupper($symbol);
$timestamp=$jsonContent->$metadataKey->$timeKey;
}
if($jsonContent->$timeSeriesKey != null)
{
$count=0;
foreach($jsonContent->$timeSeriesKey as $key=>$value)
{
if($count==0)
{
$open=$value->$openKey;
$close=$value->$closeKey;
$vol=$value->$volKey;
$high=$value->$highKey;
$low=$value->$lowKey;
$range=$low . "-" . $high;
$count=$count+1;
}
else if($count==1)
{
$prevClose=$value->$closeKey;
$count=$count+1;
}
else if($count==2)
{
break;
}
}
}
echo "
";
echo "";
echo "Stock Ticker Symbol | ";
echo "" . $symbol . " | ";
echo "
";
echo "";
echo "Close | ";
echo "" . $close . " | ";
echo "
";
echo "";
echo "Open | ";
echo "" . $open . " | ";
echo "
";
echo "Previous Close | ";
echo "" . $prevClose . " | ";
echo "";
echo "";
echo "Change | ";
$change=$close-$prevClose;
$change=number_format((float)$change, 2, '.', '');
if($change>=0)
{
echo "" . $change . " | ";
}
else
{
echo "" . $change . " | ";
}
echo "
";
echo "";
echo "Change Percent | ";
$percentage=($change/$prevClose)*100;
$percentage=round((float)$percentage, 2);
if($change>=0)
{
echo "" . $percentage . "% | ";
}
else
{
echo "" . $percentage . "% | ";
}
echo "
";
echo "";
echo "Day's Range | ";
echo "" . $range . " | ";
echo "
";
echo "";
echo "Volume | ";
$vol=number_format($vol);
echo "" . $vol . " | ";
echo "
";
echo "";
echo "Timestamp | ";
date_default_timezone_set("America/New_York");
$fullDate=date_create($timestamp);
$formattedDate=date_format($fullDate,"Y-m-d");
echo "" . $formattedDate . " | ";
echo "
";
echo "Indicators | ";
$spaces=" ";
echo "Price" . $spaces . "SMA" . $spaces . "EMA" . $spaces . "STOCH" . $spaces . "RSI" . $spaces . "ADX" . $spaces . "CCI" . $spaces . "BBANDS" . $spaces . "MACD | ";
echo "";
echo "
";
echo "
";
echo "
";
$volArray=[];
$dateArray=[];
$priceArray=[];
$today = new DateTime($formattedDate);
foreach($jsonContent->$timeSeriesKey as $key=>$value)
{
$date=new DateTime($key);
$diff=$today->diff($date);
if($diff->days>181)
{
break;
}
$dateArray[]="\"" . $date->format('m/d') . "\"";
$price=$value->$closeKey;
$price=round((float)$price, 2);
$priceArray[]=$price;
$vol=$value->$volKey;
$vol=$vol/1000000 ;
$volArray[]=$vol;
}
$revDateArray=array_reverse($dateArray);
$revVolArray=array_reverse($volArray);
$revPriceArray=array_reverse($priceArray);
$maxVol=max($volArray);
$minPrice=min($priceArray);
$dateCount=sizeof($revDateArray);
if(($dateCount-1)%5 !=0)
{
$ind=($dateCount-1)%5;
for($j=0;$j<$ind;$j++)
{
array_shift($revDateArray);
array_shift($revPriceArray);
array_shift($revVolArray);
}
}
?>