Mike Barbaro
Mike Barbaro

Reputation: 245

Handling JSON API response in PHP for data storage as variable

I need to store each individual value from the JSON API response as a variable so I can then store them in MySQL.

I am able to access the top level data shown here when echoing id, but cannot seem to access the nested data such as name>text.

I am new to API usage so any help is appreciated.

<?php
 include("restclient.php");



 $api = new RestClient(array(
    'base_url' => "https://www.eventbriteapi.com/v3", 
));
$result = $api->get("/events/" . $_POST['id_eventbrite'] . "/?token=MYTOKENISHERE");


echo $result['id'];


?>

Upvotes: 1

Views: 1337

Answers (2)

PekosoG
PekosoG

Reputation: 264

First you need to create a table with the corresponding columns of your Json result. Then you simply generate a query and place the values in place. For example, lets say your Json result is like this

  • id : 1234
  • name : Josh
  • age : 25

You'll need to create the corresponding table

CREATE TABLE person ( id int, name varchar(25), age int);

The query would be something like this:

INSERT INTO person (id, name, age) VALUES(?,?,?);

To bind the data, you'll do something like this:

$stmt = $mysqli->prepare("INSERT INTO person (id, name, age) VALUES(?,?,?)");
$stmt->bind_param($response['id'], $response['name'], $response['age']);

Then you execute the query and done.

For detailed information about parameter binding, check the php documentation

http://php.net/manual/en/mysqli-stmt.bind-param.php

Upvotes: 0

Vahe Galstyan
Vahe Galstyan

Reputation: 1729

Api result or json or xml, you should parse it. if json use var_dump(json_decode($result, true)); if xml use json_decode(json_encode(simplexml_load_string($result)),true);

Upvotes: 2

Related Questions