2016-01-15 33 views
1

所以我得到了一個Steam市場價格API,但如果我通過我的PHP文件去它,它會顯示「1」。例如:cost.php?name = blablablaPhp文件只顯示數字1?

<?php 
if (isset($params,$item)){ 
$params['format'] = 'json'; 
$params['data'] =json_encode($package_data); 
$item = $_GET['item']; 
$item = str_replace("\"", "", $item); 
$item = str_replace("\'", "", $item); 
$item = str_replace(" ", "%20", $item); 
$item = str_replace("\\", "", $item); 
@include_once ("set.php"); 
$rs = mysql_query("SELECT * FROM items WHERE name='$item'"); 
$url = "http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name=".$item; 
if(mysql_num_rows($rs) > 0) { 
    $row = mysql_fetch_array($rs); 
    if(time()-$row["lastupdate"] < 3600) die($row["cost"]); 
} 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($params)); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); 
$result = curl_exec($ch); 
curl_close($ch); 
$results = json_decode($result, true); 
var_dump($results); 
if($results->{'success'} == "0") die("notfound");{ 
$lowest_price = $results->{'median_price'}; 
$lowest_price[strlen($lowest_price)] = 0; 
$lowest_price = str_replace("$","",$lowest_price); 
$lowest_price = floatval($lowest_price); 
} 
mysql_query("DELETE FROM items WHERE name='$item'"); 
mysql_query("INSERT INTO items (`name`,`cost`,`lastupdate`) VALUES ('$item','$lowest_price','".time()."')"); 
print_r($results); 
} 
?> 

我的問題是會有人知道該解決方案嗎?

+0

嘗試通過瀏覽器訪問該鏈接,看看你得到了什麼,如果你得到propper json字符串,那麼它可能是由API返回的頭。通常我不會使用file_get_contents作爲API,因爲它可能有一些特定的頭文件集,可能不允許它被讀爲文件。 Tru使用cURL功能連接到API。 – Auris

+0

嘿,我只是試過捲曲。現在這只是一個白頁 –

+0

本身不會給你任何輸出。設置捲曲設置,進行請求,獲取結果對象並顯示結果對象。 http://php.net/manual/en/book.curl.php如果你以前從未使用過cURL,那麼起初可能會有點壓倒性,但經過一些練習後,它變得非常簡單。 – Auris

回答

0

$lowest_pricearray

$lowest_price = $obj->{'lowest_price'}; 
$lowest_price[strlen($lowest_price)] = 0; // <-- here you are making it into an array 
$lowest_price = str_replace("$","",$lowest_price); 
$lowest_price = floatval($lowest_price); 

你投以float所以基本上進程工作正常。

查看如何floatval行爲http://php.net/manual/en/function.floatval.php#refsect1-function.floatval-returnvalues

你應該投的數組的值,或只是讓error_reporting趕上所有通知。

+0

我再次更新了我的代碼,不是它不顯示「1」,而是顯示第29行的錯誤 –