pmflav Posted November 16, 2012 Share Posted November 16, 2012 I would just like to start by saying sorry if this is the wrong area. I am trying to import data from a a Game site API http://worldoftanks.com/community/clans/api/1.0/?source_token=WG-WoT_Assistant-test&search=KST&offset=0&limit=1, however I am not sure where to start with this one. It doesn't appear to be standard xml. What I am trying to do is create a script on my website which will read this page and import the data into the database, and via a cron will update it every day. A tap in the right direction with this one would be a great help. Thankyou in advance Quote Link to comment Share on other sites More sharing options...
ignace Posted November 17, 2012 Share Posted November 17, 2012 It's in JSON format. json_decode Quote Link to comment Share on other sites More sharing options...
pmflav Posted November 18, 2012 Author Share Posted November 18, 2012 Thankyou very much for that. Gives me a direction to look. Quote Link to comment Share on other sites More sharing options...
ignace Posted November 18, 2012 Share Posted November 18, 2012 Post your current code if you need coding help Quote Link to comment Share on other sites More sharing options...
pmflav Posted November 18, 2012 Author Share Posted November 18, 2012 At the moment I don't have any code, reading up and giving myself a crash course, but will definately post it here for any feedback as I am sure it won't be perfect initially. Thankyou again. Quote Link to comment Share on other sites More sharing options...
pmflav Posted November 18, 2012 Author Share Posted November 18, 2012 This is what I have so far: $f = file_get_contents('http://worldoftanks.com/community/clans/api/1.0/?source_token=Intellect_Soft-WoT_Mobile-site&search=KST&offset=0&limit=1'); if(!function_exists('json_decode')) die('Your host does not support json'); $feed = json_decode($f); for ($i=0;$i<count($feed["data"]);$i++) { $sql = array(); foreach($feed['data'][$i] as $key => $value){ $sql[] = (is_numeric($value)) ? "`$key` = $value" : "`$key` = '" . mysql_real_escape_string($value) . "'"; } $sqlclause = implode(",",$sql); $rs = mysql_query("INSERT INTO items SET $sqlclause"); } // This is minus the mysql_connect at the start. However there seems to be a problem with: for ($i=0;$i<count($feed["data"]);$i++) Fatal error: Cannot use object of type stdClass as array Quote Link to comment Share on other sites More sharing options...
ignace Posted November 18, 2012 Share Posted November 18, 2012 (edited) /** * Takes an URL and returns a JSON object, throws an Exception if anything goes wrong. * * @param string $url A WOT URL * @param bool $isUrl true = http, false = file for testing * @return object The JSON response * @throws Exception When something goes wrong */ function readWotResponse($url, $isUrl = true) { if ($isUrl && !ini_get('allow_url_fopen')) { throw new Exception('allow_url_fopen = Off'); } $content = file_get_contents($url); if ($isUrl) { $status_code = substr($http_response_header[0], 9, 3); if ($status_code != 200) { throw new Exception('Got status code ' . $status_code . ' expected 200'); } } $json = json_decode($content); if (!$json) { throw new Exception('Response contained an invalid JSON response'); } if ($json->status != 'ok') { throw new Exception($json->status_code); } return $json; } /** * Inserts $data in $table * * @param mysqli $dbconn * @param string $table * @param array $data */ function sqlInsert(mysqli $dbconn, $table, $data) { //.. } $url = '..'; try { $json = readWotResponse($url); foreach ($json->data->items as $item) { sqlInsert($dbconn, 'items', (array) $item); } } catch (Exception $e) { echo $e->getMessage(); } Edited November 18, 2012 by ignace Quote Link to comment Share on other sites More sharing options...
pmflav Posted November 18, 2012 Author Share Posted November 18, 2012 Thankyou very much for the code above, and please do excuse me not knowing the basics. I am still a bit lost with the code. I don't know how to connect to the database and do the inserts with the above. Sorry Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.