我正在从Groupon的api中提取交易,我不知道如何将数据放入php数据库,我知道如何在html中显示,但不在数据库中,我需要将其拉入数据库,这样我就可以更好地控制信息,如果有人知道如何做到这一点或知道更好的方法,我会全力以赴,哈哈,谢谢
<script type='text/javascript'>
$(function () {
$.getJSON("https://api.groupon.com/v2/deals.json?callback=?",
{
client_id: "b252ad3634a4ab2985b79d230ccc4e49a3ea9d19",
show: "all",
division_id: "los-angeles"
})
.done(function (data) {
console.log(data);
// do whatever processing you need to do to the data
// right here, then drop it in the div
$.each(data.deals, function (i, v) {
$title = $("<h2/>", {
html: v.title,
class: "heading"
});
$img = $("<img/>", {
src: v.mediumImageUrl
});
$deal = $("<div/>", {
html: v.highlightsHtml + v.pitchHtml
});
$("#main").append($deal);
$deal.prepend($title, $img);
});
});
});
</script>
理论
好吧,我只是要开始运行整个过程。。。
首先,了解您正在处理的驱动程序,并研究PHP如何与它们交互。看看这个列表,开始阅读。。。http://www.php.net/manual/en/refs.database.php
将数据发送到PHP脚本以处理其余部分,这取决于您是如何获得数据的。以下是一些基本流程。。。
- 使用jQuery提取它,并在将它保存到php脚本后立即使用AJAX发送它。(需要额外的HTTP请求)
- 使用PHP提取它,将其保存到DB,然后在同一页面上格式化并输出。(降低初始页面加载时间)
- 使用jQuery提取它,格式化它,并允许用户按下一个按钮,然后将该条目ajax到PHP保存脚本中(更灵活,但大大增加了请求)
在可以连接到数据库之后,只需要使用SQL查询(很可能使用INSERT或UPDATE)将其保存到表中。对于JSON数据,我更喜欢将其保存到数据类型为TEXT的列中。这里唯一真正的风险是,您必须确保能够验证数据。尤其是如果数据是从JAVASCRIPT/AJAX源提供给PHP的!
从这一点提取数据只是使用一个"SELECT"sql语句。PHP的数据库模块将提取这些数据,并将其放入一个可爱的数组中,使操作变得容易。
示例
这就是理论,还有一些行动。我将选择第一流的想法。现在这个只会将它保存到数据库中。我没有做任何花哨的检查或真正的拉动。但这将向您展示ajaxing和保存到php的工作原理。
查看交易.html
<script type='text/javascript'>
$(function () {
$.getJSON("https://api.groupon.com/v2/deals.json?callback=?",
{
client_id: "b252ad3634a4ab2985b79d230ccc4e49a3ea9d19",
show: "all",
division_id: "los-angeles"
}).done(function (data) {
console.log(data);
// do whatever processing you need to do to the data
$.post('save-deals.php',{dealData: data}, function(finishData) {
//This is an optional function for when it has finished saving
});
// Your formatting comes next
....
});
</script>
现在,它将使用AJAX Post调用将您从groupon获得的所有数据(完整)发送到一个单独的php脚本。我使用post,因为这就是它的用途。
保存交易.php
ob_start(); //I like output-buffering because if we need to change headers mid script nothing dies
$DealData = isset( $_POST['dealData'] )?$_POST['dealData']:die("Malformed form data!");
if($DealData!='') {
$DB = new mysqli("example.com", "user", "password", "database");
if ($DB->connect_errno) {
echo "Failed to connect to MySQL: " . $DB->connect_error;
}
$DealData = $DB->real_escape_string($DealData); //Sanitize it for MySQL
if (!$DB->query("INSERT INTO deals(data) VALUES ($DealData)") {
echo "Insert failed: (" . $DB->errno . ") " . $DB->error;
} else {
//AT THIS POINT IT SHOULD HAVE BEEN INSERTED!
//You could return a success string, or whatever you want now.
}
} else {
http_response_code("400");
die("Bad Request, please check your entry and try again");
}
ob_end_flush(); //Close up the output-buffer
关于该脚本,需要注意的一些重要事项是ob_*函数是完全可选的。DealData的设置方式是一种非常简单的方式,可以检查post数据是否包含该值,并正确设置该值;如果没有,则给出一个错误。
下一个脚本将向您展示如何立即从数据库中提取数据,并根据需要对其进行操作。它还将以JSON信息的形式返回数据,以便可以与javascript $.getJSON()
调用一起使用。这主要是的参考片段
操纵交易.php
//First connect to the database!
$DB = new mysqli("example.com", "user", "password", "database");
if ($DB->connect_errno) die("Failed to connect to MySQL: " . $DB->connect_error);
//Get ALL the entries!
if(!$results = $DB->query("SELECT * FROM data")) die("Failed to retrieve data! ".$DB->error);
//Decode the datas!
$returnResults = [];
while($entry = $results->fetch_assoc()) {
$JSON = json_decode($entry['data']);
//Manipulate however you wish!
$JSON->whatever->tags[1]->you->want = "whatever value";
//Add it to the results!
$returnResults[] = $JSON;
}
echo json_encode($returnResults);
最后一节只是为了好玩。它将导出一个包含结果数组的json字符串。该数组的每个条目都将是一个有效的对象,就像groupon给您的一样。希望能有所帮助!