Jump to content


Photo

Import Api


  • Please log in to reply
7 replies to this topic

#1 pmflav

pmflav

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 16 November 2012 - 06:29 PM

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....ffset=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

#2 ignace

ignace

    Now mod flavored

  • Moderators
  • 6,248 posts
  • LocationBelgium

Posted 17 November 2012 - 02:56 AM

It's in JSON format.

json_decode

#3 pmflav

pmflav

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 18 November 2012 - 03:20 AM

Thankyou very much for that. Gives me a direction to look.

#4 ignace

ignace

    Now mod flavored

  • Moderators
  • 6,248 posts
  • LocationBelgium

Posted 18 November 2012 - 04:00 AM

Post your current code if you need coding help

#5 pmflav

pmflav

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 18 November 2012 - 07:04 AM

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.

#6 pmflav

pmflav

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 18 November 2012 - 08:41 AM

This is what I have so far:

$f = file_get_contents('http://worldoftanks....ffset=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

#7 ignace

ignace

    Now mod flavored

  • Moderators
  • 6,248 posts
  • LocationBelgium

Posted 18 November 2012 - 11:45 AM

/**

 * 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, 18 November 2012 - 11:47 AM.


#8 pmflav

pmflav

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 18 November 2012 - 04:56 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com