【发布时间】:2015-11-10 21:38:14
【问题描述】:
所以我正在尝试编写一个 .php 文件,该文件应该从 API 调用中提取字符串(此处要提取的文本示例:https://gyazo.com/1efffc2360bd6aba15ece3437251aebd)
代码:
<?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);
if(!empty($rs)) {
if(time()-$rs["lastupdate"] < 604800) die($rs["cost"]);
}
$link = "https://bitskins.com/api/v1/get_item_price/?api_key=(NOTSHOWINGYOUGUYSMYAPIKEY)&names=".$item;
$string = file_get_contents($link);
$json = $string;
$obj = json_decode($json);
//print $obj->{"median_price"}; // 12345
//$obj = json_decode($string);
if($obj->{'status'} == "success") die("notfound");
$lowest_price = $obj->{'price'};
$lowest_price=str_replace("$", "", $lowest_price);
$lowest_price = (float)($lowest_price);
//$stmt = $dbh->prepare("DELETE FROM items WHERE name=?");
//$stmt->execute(array($item));
$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;
?>
问题是代码没有提取我需要的字符串($lower_price 变量)。我尝试通过自己以哈希市场名称值格式定义 $item 变量以及使用显示的脚本来运行它。没有结果。知道为什么吗?
谢谢。
【问题讨论】: