【发布时间】:2016-02-12 07:43:16
【问题描述】:
我制作了这个 php 代码来从 API 获取结果,但它不起作用。我为变量做了一些回声,以检查它们是否显示在屏幕上,但仍然没有显示。任何解决方案或想法为什么?
谢谢。
<?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");
$code='SELECT auth FROM auth where id=1';
echo $code;
$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=(an api key here)&code=".$code"&names=".$item;
$string = file_get_contents($link);
$json = $string;
$obj = json_decode($json);
echo $obj;
//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;
?>
更新:由于某种原因,结果未导入数据库,因此我将尝试将所有内容分解为更小的部分: 这是来自 pdocon.php 的代码:
<?php
$user="";
$pass="";
try {
$dbh = new PDO('mysql:host=localhost;dbname=testjackpot', $user, $pass);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
这是表 auth 的样子: https://gyazo.com/75ea3a77773820f0c3adbdc24fc99185
这是 API 调用的结果: https://gyazo.com/efe5680baad104be97281c5dac3d2c58
由于某种原因,即使执行 print_r($code) 假设,从这个 php 生成的网页上也没有显示任何内容。
更新2:
尝试使用
<?php
$item = $_GET['item'];
$item = str_replace("\"", "", $item);
$item = str_replace("\'", "", $item);
$item = str_replace(" ", "%20", $item);
$item = str_replace("\\", "", $item);
@include_once ("set.php");
$code=mysql_query("SELECT auth FROM auth where id=1");
echo $code;
$rs = mysql_query("SELECT * FROM items WHERE name='$item'");
if(mysql_num_rows($rs) > 0) {
$row = mysql_fetch_array($rs);
if(time()-$row["lastupdate"] < 3600) die($row["cost"]);
}
$link = "https://bitskins.com/api/v1/get_item_price/?api_key=(code)&code=".$code."&names=".$item."&delimiter=!END!";
$string = file_get_contents($link);
echo $string;
$obj = json_decode($string);
if($obj->{'success'} == "0") die("notfound");
$lowest_price = $obj->data->prices[0]->price;
$lowest_price[strlen($lowest_price)] = 0;
$lowest_price = str_replace("$","",$lowest_price);
$lowest_price = (float)($lowest_price);
mysql_query("DELETE FROM items WHERE name='$item'");
mysql_query("INSERT INTO items (`name`,`cost`,`lastupdate`) VALUES ('$item','$lowest_price','".time()."')");
echo $lowest_price;
?>
仍然没有显示任何内容。 UPDATE3:对于上面的最后一个代码,我终于得到了一些东西:gyazo。 com/9fde818695abac2f4fe7997482ba5865
【问题讨论】:
-
json_decode生成数组,所以添加print_r()或var_dump()检查 -
如果您甚至没有得到第一个回声,那是因为 include_once 正在杀死您。并删除那个“@”关闭,看在上帝的份上。永远不要使用它!如果您不想显示警告,请整理您的代码
-
它仍然没有从 API 中获取价格..