2013-08-05 86 views
0

所以昨天我問到如何發佈數據到表中。現在它確實給出了很好的結果,但至少需要30秒才能得出結果。我用捲曲試過,結果幾乎是瞬間的。現在我的問題是:我如何將數據從curl發佈到表中。捲曲和張貼在表

我的代碼:

function curl_download($Url){ 

    // is cURL installed yet? 
    if (!function_exists('curl_init')){ 
     die('Sorry cURL is not installed!'); 
    } 

    // OK cool - then let's create a new cURL resource handle 
    $ch = curl_init(); 

    // Now set some options (most are optional) 

    // Set URL to download 
    curl_setopt($ch, CURLOPT_URL, $Url); 

    // Set a referer 
    curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm"); 

    // User agent 
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); 

    // Include header in result? (0 = yes, 1 = no) 
    curl_setopt($ch, CURLOPT_HEADER, 0); 

    // Should cURL return or print out the data? (true = return, false = print) 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    // Timeout in seconds 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 

    // Download the given URL, and return output 
    $output = curl_exec($ch); 

    // Close the cURL resource, and free system resources 
    curl_close($ch); 

    return $output; 
} 

print curl_download('http://services.runescape.com/m=itemdb_rs/api/catalogue/items.json?category=1&alpha=a&page=1'); 

隨着作爲結果:

{ 「總」:5 「項」:[{ 「圖標」: 「http://services.runescape.com/m=itemdb_rs/4173_obj_sprite.gif?id=4798」, 「icon_large」:」 http://services.runescape.com/m=itemdb_rs/4173_obj_big.gif?id=4798「id」:4798「type」:「Ammo」,「typeIcon」:「http://www.runescape.com/img/categories/Ammo」,「name」:「堅定的殘忍」,「描述」:「鈍金剛箭頭......哎喲」,「當前「:{」trend「:」neutral「,」price「:222},」today「:{」trend「:」neutral「,」price「:0}},{」icon「:」http://services.runescape.com/m=itemdb_rs/4173_obj_sprite.gif?id=810「,」 「:」http://services.runescape.com/m=itemdb_rs/4173_obj_big.gif?id=810「,」id「:810,」type「:」Ammo「,」typeIcon「: 「」,「name」:「Adamant dart」,「description」:「一個致命的投擲飛鏢與堅定的小費。」,「當前」:{「trend」:「neutral」,「price」:11},「今日「:{」trend「:」neutral「,」price「:0}},{」icon「:」http://services.runescape.com/m=itemdb_rs/4173_obj_sprite.gif?id=829「,」icon_large「:」http://services.runescape.com/m=itemdb_rs/4173_obj_big.gif?id=829「,」id「:829,」type「 ,「typeIcon」:「http://www.runescape.com/img/categories/Ammo」,「name」:「Adamant標槍」,「描述」:「堅​​韌的標槍。」,「當前」:{「趨勢」:「中立」,「價格」:64}, 「today」:{「trend」:「positive」,「price」:「+ 1」}},{「icon」:「http://services.runescape.com/m=itemdb_rs/4173_obj_sprite.gif?id=867」,「icon_large」:「http://services.runescape.com/m=itemdb_rs/4173_obj_big.gif?id=867」,「id」:867, :「Ammo」,「typeIcon」:「http://www.runescape.com/img/categories/Ammo」,「name」:「Adamant knife」,「description」:「一把精細平衡的投擲刀。」,「當前」:{「trend」:「neutral」,「price 「:23」,「today」:{「trend」:「neutral」,「price」:0}},{「icon」:「http://services.runescape.com/m=itemdb_rs/4173_obj_sprite.gif?id=804」,「icon_large」:「http://services.runescape.com/m=itemdb_rs/4173_obj_big.gif?id=804」,「id」:804,鍵入「:」彈藥「,」typeIcon「:」http://www.runescape.com/img/categories/Ammo「,」名稱「:」堅定的投擲斧子「 ,「描述」:「精細平衡的投擲斧」,「當前」:{「趨勢」:「中性」,「價格」:180},「今日」:{「趨勢」 :0}}]}

我試過谷歌,但因爲我關於捲曲和數組的知識是零我沒有任何進一步。

+0

表返回值郵寄它,你只想渲染的數據表提取回來? – MajorCaiger

+0

@MajorCaiger我不完全知道你的意思,但這是我的問題回答後昨天的結果:http://puu.sh/3UfYe.png –

回答

1

響應看起來像json,所以你可以嘗試以下。

編輯:

這對我很好。

<?php 

$response = '{"total":5,"items":[{"icon":"http://services.runescape.com/m=itemdb_rs/4173_obj_sprite.gif?id=4798","icon_large":"http://services.runescape.com/m=itemdb_rs/4173_obj_big.gif?id=4798","id":4798,"type":"Ammo","typeIcon":"http://www.runescape.com/img/categories/Ammo","name":"Adamant brutal","description":"Blunt adamantite arrow...ouch","current":{"trend":"neutral","price":222},"today":{"trend":"neutral","price":0}},{"icon":"http://services.runescape.com/m=itemdb_rs/4173_obj_sprite.gif?id=810","icon_large":"http://services.runescape.com/m=itemdb_rs/4173_obj_big.gif?id=810","id":810,"type":"Ammo","typeIcon":"http://www.runescape.com/img/categories/Ammo","name":"Adamant dart","description":"A deadly throwing dart with an adamant tip.","current":{"trend":"neutral","price":11},"today":{"trend":"neutral","price":0}},{"icon":"http://services.runescape.com/m=itemdb_rs/4173_obj_sprite.gif?id=829","icon_large":"http://services.runescape.com/m=itemdb_rs/4173_obj_big.gif?id=829","id":829,"type":"Ammo","typeIcon":"http://www.runescape.com/img/categories/Ammo","name":"Adamant javelin","description":"An adamant tipped javelin.","current":{"trend":"neutral","price":64},"today":{"trend":"positive","price":"+1"}},{"icon":"http://services.runescape.com/m=itemdb_rs/4173_obj_sprite.gif?id=867","icon_large":"http://services.runescape.com/m=itemdb_rs/4173_obj_big.gif?id=867","id":867,"type":"Ammo","typeIcon":"http://www.runescape.com/img/categories/Ammo","name":"Adamant knife","description":"A finely balanced throwing knife.","current":{"trend":"neutral","price":23},"today":{"trend":"neutral","price":0}},{"icon":"http://services.runescape.com/m=itemdb_rs/4173_obj_sprite.gif?id=804","icon_large":"http://services.runescape.com/m=itemdb_rs/4173_obj_big.gif?id=804","id":804,"type":"Ammo","typeIcon":"http://www.runescape.com/img/categories/Ammo","name":"Adamant throwing axe","description":"A finely balanced throwing axe.","current":{"trend":"neutral","price":180},"today":{"trend":"neutral","price":0}}]}'; 

$data = json_decode($response, true); 

?> 
<table> 
    <?php 
    foreach($data['items'] as $row) 
    { 
     ?> 
     <tr> 
      <td><?php echo $row['id']; ?></td> 
      <td><?php echo $row['type']; ?></td> 
      <td><?php echo $row['name']; ?></td> 
      <td><img src="<?php echo $row['icon']; ?>" /></td> 
     </tr> 
     <?php 
    } 
    ?> 
</table> 

在你的榜樣的反應是從curl_download

$response = curl_download('http://services.runescape.com/m=itemdb_rs/api/catalogue/items.json?category=1&alpha=a&page=1'); 
+0

獲取以下錯誤:警告:'爲foreach提供了無效參數()in'foreach行($ data ['items'] as $ row) ' –

+1

@JacobBrol你是否傳入true作爲json_decode的第二個參數?你可能需要var_dump $ data來查看你的數組看起來像json_decode – MajorCaiger

+0

var dump給NULL –