<?php
$item = $_GET['item'];
$item = str_replace("\"", "", $item);
$item = str_replace("\'", "", $item);
$item = str_replace(" ", "%20", $item);
$item = str_replace("\\", "", $item);
@include_once ("pdocon.php");
$stmt = $dbh->prepare("SELECT * FROM items WHERE name=?");
$stmt->execute(array($item));
$rs = $stmt->fetch(PDO::FETCH_ASSOC);
$stmtt = $dbh->prepare("SELECT auth FROM auth where id=1");
$stmtt->execute();
$code = $stmtt->fetch(PDO::FETCH_ASSOC);
//print_r($code);
if(!empty($rs)) {
if(time()-$rs["lastupdate"] < 604800) die($rs["cost"]);
}
$url = "?api_key=(mykeyhere)&code=".$code."&names=".$item."&delimiter=!END!";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$obj = curl_exec($curl);
curl_close($curl);
if($obj->{'status'} == "fail") die("notfound");
$lowest_price = $obj->data->prices[0]->price;
$lowest_price = (float)($lowest_price);
$stmt = $dbh->prepare("UPDATE items SET `cost` = ?,`lastupdate` = ? WHERE `name` = ?");
$stmt->execute(array($lowest_price, time(), $item));
$stmt = $dbh->prepare("INSERT INTO items (`name`,`cost`,`lastupdate`) VALUES (?, ?, ?)");
$stmt->execute(array($item, $lowest_price, time()));
echo $lowest_price;
?>
提取的內容給出了在數據庫0值。有人知道爲什麼上面的代碼應該顯示「價格」,這是「1300.00」,不是0 API調用應在畫面提供值的結果:https://gyazo.com/a514f7f46e976d43efd008de778cb5c5 或代碼:
{
"status" : "success",
"data" : {
"prices" : [
{
"market_hash_name" : "AWP | Dragon Lore (Minimal Wear)",
"price" : "1300.00"
}
]
}
}
我假設你正在談論這行代碼:'$ LOWEST_PRICE的= $ obj->數據 - >價格[0] - >價格;'你在哪裏使用'json_decode()'實際**生成** $ obj'? – Jan
json_decode()之前沒有工作,所以我不得不做cURL – bogdanb
我不知道爲什麼它沒有工作,但我用:$ string = file_get_contents($ link); $ json = $ string; $ obj = json_decode($ json); – bogdanb