关于通过CURL GET创建客户端数据库本地副本的代码,我有几个问题。
情况:
https://CLIENT_SERVER_URL/sync?last_id=LAST_ID&since=ISO_8601_TIMESTAMP&access_token=TOKEN
我有什么问题,我需要建议
Q1。)BFU问题(对不起),如何以这种格式生成时间戳
2016-06-22T23%3A34%3A20.169659Z
?时间戳必须生成
CURRENT_DATE - 1 day
Q2。)如何创建循环脚本重复生成新的URL和 将记录存储在数据库中?
Q3。)如何限制每分钟只能拨打60个电话?
Q4。)如何将INSERT插入数据库以便输入 在它不存在时插入或在存在时插入UPDATEd
我的PHP代码
<?php
// select DB
$servername = "MY_DATABASE";
$username = "USER";
$password = "PASS";
$dbname = "DB_NAME";
// Create connection
$corporateBodies = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT id FROM my_table ORDER BY id DESC LIMIT 1";
$lastID = $corporateBodies->query($sql);
$lastID = mysqli_fetch_array($lastID);
$base = "CLIENT_SERVER_URL/";
$since = "GENERATED_TIMESTAMP";
$accessToken = "ACCESS_TOKEN";
$url = $base . "sync?last_id=" . $lastID["id"] . "&since=" . $since . "&access_token=" . $accessToken;
//function httpGet($url) {
$crl = curl_init($url);
curl_setopt($crl,CURLOPT_URL,$url);
curl_setopt($crl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($crl,CURLOPT_HEADER, false);
curl_setopt($crl, CURLOPT_TIMEOUT, 60);
$reply =curl_exec($crl);
curl_close($crl);
//decoding the json data
$decoded_data = json_decode($reply, true);
$insertArray = $corporateBodies->prepare(
"INSERT INTO my_table (
id,
cin,
tin,
vatin,
name,
formatted_address,
street,
reg_number,
building_number,
postal_code,
municipality,
country,
established_on,
terminated_on,
vatin_paragraph,
registration_office,
registration_number,
formatted_street,
street_number,
created_at,
updated_at
)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
);
$corporateBodies->query("START TRANSACTION");
foreach ($decoded_data as $row) {
$bind = $insertArray->bind_param('iiissssisisssssssssss',
$row['id'],
$row['cin'],
$row['tin'],
$row['vatin'],
$row['name'],
$row['formatted_address'],
$row['street'],
$row['reg_number'],
$row['building_number'],
$row['postal_code'],
$row['municipality'],
$row['country'],
$row['established_on'],
$row['terminated_on'],
$row['vatin_paragraph'],
$row['registration_office'],
$row['registration_number'],
$row['formatted_street'],
$row['street_number'],
$row['created_at'],
$row['updated_at']
);
$exec = $insertArray->execute();
// Close the prepared statement
$insertArray->close();
$commit = $corporateBodies->query("COMMIT");
$corporateBodies->close();
?>
答案 0 :(得分:0)
您的时间戳与ISO8601完全匹配Z
而非+00:00
,精确度为微秒,因此要创建时间戳 - 1天,可能
$stamp=(new DateTime('-1 day',new DateTimeZone("UTC")))->format('Y-m-d\TH:i:s.u\Z');
引用How do I create a loop to script repeatedly generate new URLs and store the records in the database?
如果您想知道如何创建循环,
for($i=0;$i<60;++$i){/*code in here will be looped 60 times!*/}
如果您想要生成新网址,请查看API文档,我们无法在不知道API如何工作的情况下回答该问题,并且您未在问题中包含api规范。
如果您想知道如何在数据库中存储某些东西,这取决于您的数据库,我们无法在不知道您正在使用何种数据库的情况下回答该问题。但假设您正在使用SQL数据库(而不是像MongoDB或其他数据库这样的NoSQL数据库),它通常通过insert语句,如
INSERT INTO records VALUES('new record');
引用How can I limit calls to only 60 calls per minute?
while(!$finished){
$quota_reset_time=microtime(true)+60;
for($i=0;$i<60;++$i){
//code in here runs 60 times
}
@time_sleep_until($quota_reset_time); // this line sleeps until 60 seconds has passed since $quota_reset_time was set.
}
for循环中的代码将以每分钟60次的最大速度运行。
引用Q4.) How do I make a INSERT into the database so that the entry is inserted when it does not exist or UPDATEd when it exists?
再次,它取决于数据库。但是,假设使用MySQL或MariaDB数据库,将值设为UNIQUE列,并生成INSERT OR REPLACE
语句,如
INSERT OR REPLACE INTO tbl(`value`,`last_update_time`)
VALUES('FOO',NOW());