在PHP中处理JSON API响应以将数据存储为变量

时间:2015-10-01 06:02:02

标签: php mysql json api parsing

我需要将JSON API响应中的每个单独值存储为变量,然后我可以将它们存储在MySQL中。

我可以在回显id时访问此处显示的顶级数据,但似乎无法访问嵌套数据,例如name> text。

我是API使用的新手,所以感谢任何帮助。

<?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'];


?>

2 个答案:

答案 0 :(得分:2)

Api结果或json或xml,你应该解析它。如果json使用var_dump(json_decode($result, true)); 如果xml使用json_decode(json_encode(simplexml_load_string($result)),true);

答案 1 :(得分:0)

首先,您需要创建一个包含Json结果的相应列的表。然后,您只需生成一个查询并将值放在适当的位置。例如,假设你的Json结果是这样的

  • id:1234
  • 姓名:Josh
  • 年龄:25

您需要创建相应的表格

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

查询将是这样的:

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

要绑定数据,您可以执行以下操作:

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

然后执行查询并完成。

有关参数绑定的详细信息,请查看php文档

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