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
Related
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/>";
}
}
?>
I recently uploaded my client's site onto a temporary server so that they could get started on data input while I fine tune the design.
When I launched it however, one of my sliders and one of my pages broke down completely [note: this only occurs on the server side; my localhost site continues to work perfectly].
I narrowed it down to my use of the date_create_from_format() function as I use it on both pages, and when I remove the element holding that bit of php, the site works fine. I have scoured my file for any missing semi-colons, or brackets, and I can't find any glaring errors. Here is my code as it was orginally on my localhost.
<p>
<?php
$end = date_create_from_format('Ymd',$ending_date);
$start = date_create_from_format('Ymd',$starting_date);
echo "<span class='month'>" . $start->format('F') . "</span>";
echo " ";
echo "<span class='day'>" . $start->format('j') . "</span>";
echo ", ";
echo "<span class='year'>" . $start->format('Y') . "</span>";
echo " - ";
echo "<span class='month'>" . $end->format('F') . "</span>";
echo " ";
echo "<span class='day'>" . $end->format('d') . "</span>";
echo ", ";
echo "<span class='year'>" . $end->format('Y') . "</span>";
echo ", $location";
?>
</p>
I have also tried converting it from the object to the procedural function (See below) but the result is the exact same.
<p>
<?php
$end = DateTime::createFromFormat('Ymd',$ending_date);
$start = DateTime::createFromFormat('Ymd',$starting_date);
echo "<span class='month'>" . date_format($start,'F') . "</span>";
echo " ";
echo "<span class='day'>" . date_format($start,'j') . "</span>";
echo ", ";
echo "<span class='year'>" . date_format($start,'Y') . "</span>";
echo " - ";
echo "<span class='month'>" . date_format($end,'F') . "</span>";
echo " ";
echo "<span class='day'>" . date_format($end,'d') . "</span>";
echo ", ";
echo "<span class='year'>" . date_format($end,'Y') . "</span>";
echo ", $location";
?>
</p>
Thank you "Sysix" for the answer regarding assigning a timezone. I made the following adjustment (See below) but the problem persists.
<?php
$end = DateTime::createFromFormat('Ymd', $ending_date, new DateTimeZone('America/Toronto'));
$start = DateTime::createFromFormat('Ymd', $starting_date, new DateTimeZone('America/Toronto'));
?>
Anyone have any ideas?
From your comment: Your PHP Version ist 5.2.17
but DateTime::createFromFormat is avaible since PHP 5.3
Look at the doc: http://php.net/manual/en/datetime.createfromformat.php
I have found a notice, that DateTime::createFromFormat ignore the setted TimeZone. You must set the Timezone explicit for the object.
http://php.net/manual/de/datetime.createfromformat.php#116027
I know this has been discussed a lot here, but I still don't seem able to find aworking solution. Either it's out of date, it's the wrong programming-language or it's no longer supported.
All I want to do is: Get the last 'n' tweets from a public Twitter profile using PHP/JavaScript.
What API should I use best?
How do I keep it as lightweight as possible?
I can't use Node. js
I tried this, but I can't seem to get it to work, as simple as it may look like.
Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
I've already registered for a developer account and created a "Twitter-App".
You can use this API, which I have used once and it works perfectly.
Download the file TwitterApiExchange.php from the repository above and you may use the following sample code to fetch tweets of a user:
<?php
echo '<pre>';
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
$user = "rootcss";
$count = 100;
if (isset($_GET['user'])) {
$user = $_GET['user'];
}
if (isset($_GET['count'])) {
$count = $_GET['count'];
}
$getfield = "?screen_name=$user&count=$count";
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(), $assoc = TRUE);
if (isset($string["errors"][0]["message"])) {
echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>" . $string['errors'][0]["message"] . "</em></p>";
exit();
}
foreach ($string as $items) {
echo "Time and Date of Tweet: " . $items['created_at'] . "<br />";
echo "Tweet: " . $items['text'] . "<br />";
echo "Tweeted by: " . $items['user']['name'] . "<br />";
echo "Screen name: " . $items['user']['screen_name'] . "<br />";
echo "Followers: " . $items['user']['followers_count'] . "<br />";
echo "Friends: " . $items['user']['friends_count'] . "<br />";
echo "Listed: " . $items['user']['listed_count'] . "<br /><hr />";
}
echo '</pre>';
?>
It worked perfectly for me, Let me know if you face any issues.
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
I want to change the original code I have,
echo "<p><strong>" . __('Area:', 'honegumi') . "</strong> " . number_format($productarea) . " m² (";
echo metersToFeetInches($productarea) . " ft²)" . "</p>";
into a single echo line as shown here:
echo "<p><strong>" . __('Area:', 'honegumi') . "</strong> " . number_format($productarea) . " m² (" . metersToFeetInches($productarea) . " ft²)" . "</p>";
But I'm getting some strange line breaks in this second case for metersToFeetInches($productarea).
Generated HTML:
24,757
<p>
<strong>Area:</strong>
2,300 m² ( ft²)
</p>
Output:
24,757
Area:
2,300 m² ( ft²)
How can I solve it? Is there any documentation I could read to learn how to do it by myself in the future?
I'm pretty sure I know what's going on here, your function metersToFeetInches is echoing a value rather than returning it.
function metersToFeetInches() {
echo 'OUTPUT';
}
echo 'FIRST '.metersToFeetInches().' LAST';
// Outputs: OUTPUTFIRST LAST
echo metersToFeetInches() is actually redundant.
This is because the function runs before the string you built is actually output. Note that both examples you posted would have this problem. Change your function to return a value instead. Afterwards, any places where you have used it like so:
echo 'Something';
metersToFeetInches();
echo 'Something Else';
You'll have to use an echo:
echo 'Something';
echo metersToFeetInches();
echo 'Something Else';
Functions should pretty much always return a value. Lesson learned, perhaps?
If you are really in a bind and cannot change the function, you'll have to resort to output buffering:
ob_start();
metersToFeetInches($productarea);
$metersToFeetInches = ob_get_clean();
echo "<p><strong>" . __('Area:', 'honegumi') . "</strong> " . number_format($productarea) . " m² (" . $metersToFeetInches . " ft²)" . "</p>";
...which is rather silly to have to do.