Jump to content

Import Api


pmflav

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

/**
 * 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 by ignace
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.