execute(array($item)); $rs = $stmt->fetch(-6ren">
gpt4 book ai didi

php - 从 API 调用中提取的内容给出 0 值

转载 作者:行者123 更新时间:2023-11-29 11:53:11 25 4
gpt4 key购买 nike

提取的内容

<?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"
}
]
}
}

最佳答案

除了我的评论之外,您还需要使用 json_decode() 解析curl 输出:

$obj = json_decode(curl_exec($curl)); // json_decode gives back an object
curl_close($curl);
if($obj->{'status'} == "fail") die("notfound");

关于php - 从 API 调用中提取的内容给出 0 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33665873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com