2015-11-12 18 views
0

內容給出0值

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

我假設你正在談論這行代碼:'$ LOWEST_PRICE的= $ obj->數據 - >價格[0] - >價格;'你在哪裏使用'json_decode()'實際**生成** $ obj'? – Jan

+0

json_decode()之前沒有工作,所以我不得不做cURL – bogdanb

+0

我不知道爲什麼它沒有工作,但我用:$ string = file_get_contents($ link); $ json = $ string; $ obj = json_decode($ json); – bogdanb

回答

1

除了我的評論,你需要分析與json_decode()的捲曲輸出:

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

仍然得到0個結果.. – bogdanb

+0

你可以在echo_print_r($ obj)中包含你的結果嗎?只需編輯它。 – Jan

+0

stdClass Object([status] => success [data] => stdClass Object([error_message] =>啓用了API訪問,但無法驗證雙因素驗證代碼,如果需要幫助,請聯繫support @ bitskins。 ))10現在顯示...和我重新加載後,我得到0 – bogdanb