- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 Ekomi 的产品评论反馈集成到我们的产品详细信息页面中。他们提供了一个带有 GET 参数的 URL,如下所示:
http://api.ekomi.de/get_productfeedback.php
interface_id* - Your eKomi interface ID)
interface_pw* - Your eKomi interface password
type* - csv = RFC 4180 - comma-separated values (delimiter=, enclosure=")
product - A single product_id or the keyword "all"
最终看起来像这样(我们的详细信息显然已被隐藏)。
http://api.ekomi.de/get_productfeedback.php?interface_id=[OURID]&interface_pw=[OURPW]&version=cust-1.0.0&type=csv
在 RFC4180(delimiter=,enclosure=") 之后返回一个 CSV,其中包含以下项目:
UNIXTIME,customer_id,product_id,rating,feedback
看起来像这样......
1420215053,498,LWP0004,5,"Excellent service on a quality product"
1420215053,498,LWP0040,5,"Excellent service on a quality product"
1420573617,535,LWP0350,5,"Bought to experiment with, very good kit"
1421229382,552,LWP0173,4,"Good price ,"
1421343151,558,LWP0004,5,"Quality very good"
1421343151,558,LWP0016,5,"Quality very good"
1421412155,560,LWP0004,5,"Replacement of high energy bulbs"
1421574027,562,LWP0038,5,"tell everyone"
1421959977,575,LWP0004,5,"Lighting our revamped kitchen"
1422129969,591,LWP0038,5,"All good thanks, will use again"
1422129969,591,LWP0284,5,"All good thanks"
etc
EKomi 还提供了以下 PHP 脚本...
<?php
/*
* Creates a table to store product reviews on your server
*
* You must be connected to your server's DB before running this function
*/
function create_table($tableName){
$sql = "
CREATE TABLE IF NOT EXISTS `$tableName` (
`timestamp` int(11) unsigned NOT NULL,
`order_id` varchar(64) character set latin1 NOT NULL,
`product_id` varchar(64) character set latin1 NOT NULL,
`stars` int(1) unsigned NOT NULL,
`review` text character set latin1 NOT NULL,
`id` int(11) unsigned NOT NULL auto_increment,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=40110 DEFAULT CHARSET=latin1
COLLATE=latin1_general_ci
";
mysql_query($sql) or die(mysql_error());
}
/*
* Access the eKomi API to check for new feedback
*
* You can find your eKomi API inteface ID and interface password in your
* eKomi customer area on the right hand side.
*/
function check_product_feedback($tableName, $interfaceId, $interfacePass) {
$url = "http://api.ekomi.de/get_productfeedback.php?
interface_id=$interfaceId&interface_pw=$interfacePass&type=csv&range=1m&charset=utf-8";
if (($handle = fopen($url, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 2048, ",")) !== FALSE) {
$sql = "INSERT INTO `$tableName`
(`timestamp`,`order_id`,`product_id`,`stars`,`review`)
VALUES
('" . mysql_real_escape_string($data[0]) . "',
'" . mysql_real_escape_string($data[1]) . "',
'" . mysql_real_escape_string($data[2]) . "',
'" . mysql_real_escape_string($data[3]) . "',
'" . mysql_real_escape_string($data[4]) . "')";
mysql_query($sql) or die(mysql_error());
}
fclose($handle);
return true;
}
return false;
}
/*
* Look in the feedback table for reviews for a specific product ID
*
* Return results as an array.
*/
function get_feedback_for_product($tableName, $productId){
$sql="SELECT * FROM `$tableName` WHERE `product_id` like '$productId'";
$rs = mysql_query($sql) or die(mysql_error());
while($re = mysql_fetch_assoc($rs)){
$reviews[] = $re;
}
return isset($reviews)?$reviews:array();
}
/*
* Get the average feedback score for a given product ID
*
* Return average as a float
*/
function get_average($tableName, $productId){
$avgArr = mysql_fetch_assoc(mysql_query("SELECT AVG( `stars` ) AS `avg`
FROM `$tableName`
WHERE `product_id` = '$productId'"));
return $avgArr['avg'];
}
?>
我有 MySQL 数据库连接详细信息,我只是完全不熟悉如何开始通过使用 GET 参数调用 URL 将 CSV 列表捕获到数据库中?
难道不需要以某种方式触发吗?
如果有人能帮助我解决这个问题,我将非常感激,因为除上述之外,EKomi 不提供任何支持。
非常感谢。新泽西州
最佳答案
首先,您需要从另一台服务器收集 csv。因此,如果您的托管提供商支持 url 上的 fopen,您可以直接使用
$url = "http://api.ekomi.de/get_productfeedback.php?interface_id=[OURID]&interface_pw=[OURPW]&version=cust-1.0.0&type=csv"
if (($handle = fopen($url, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 2048, ",")) !== FALSE)
{
/*process your data here*/
$timestamp = $data[0]; //timestamp
$orderId = $data[1];
$productId = $data[2];
$stars = $data[3];
$review = $data[4];
}
}
如果您的主机不支持外部网址上的 fopen,您应该首先使用 cURL 或类似工具获取 csv,
//generate a unique temporary filename
$tmpFile = tempnam ("/tmp", "yourPrefix");
//initialize a filehandle
$fp = fopen($tmpFile, 'w');
//initialize a curl session
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1, //is used to return the transfer after execution
CURLOPT_FILE => $fp,
CURLOPT_URL => $url //pass the $url from above with the GET values
));
// Send the request
curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
// Close file handle
fclose($fp);
/*process your data as before but use fopen($tmpFile... instead of fopen($url...*/
更新:
对于您的 sql 语句,我建议您使用 mysqli
或 PDO
扩展,因为 mysql
已弃用。
要加快大量值的插入,请在查询中使用类似以下内容:
//before your while loop
$query = array();
//in your loop where you process your data
$query[] = "('".$data[0]."', '".$data[1]."', '".$data[2]."', '".$data[3]."', '".$data[4]."')";
循环之后
$tableName = "yourTable";
$sql = "INSERT INTO
`$tableName`
( `$tableName`.`timestamp`,
`$tableName`.`orderId`,
`$tableName`.`productId`,
`$tableName`.`stars`,
`$tableName`.`review`)
VALUES
";
$sql .= implode(",", $query);
并将您的sql语句$sql
发送到服务器
关于php - 使用 GET 参数从 URL 将 CSV 列表保存到数据库 - PHP/MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28231737/
我知道使用 GET 和 SET 函数的公共(public)变量的缺点/私有(private)变量的优点,但目前我正在使用 Ogre3D 开发自己的第一个“真实”游戏(C++)..同时,我有时需要 6-
我正在开发一个 GSM/GPRS 应用程序,它将每 10 秒报告一些值。我必须使用的 SIM 卡每月只有 15MB 可用数据。我使用的是 SIM900 GSM 芯片供您引用。 我到达服务器的方式是通过
这三者有什么区别:gets - 它获取带有 '\n' 的行gets.chomp - 它得到一行,但删除 '\n' 这样对吗? gets.chomp! 怎么样? 最佳答案 gets - 它得到一个末尾带
问题和我现在遇到的问题 脚本 顺便说一句,评论是挪威语的,如果它们看起来很奇怪哈哈 Connect-AzureAD #variabel $Users = Get-AzureADUser -All:$t
我现在面临的问题是获取一个 URL,如下所示: www.example.com/example.php?url=www.google.com 现在的问题是,如果我的网址中有一个 get,如下所示: w
我有一个 queryString 传递给 servlet 的 doGet() 方法,如下所示: count=9&preId0=-99&objId0=-99&preId1=-99&objId1=-99&
这是我在 Django 模板中的代码: {% for tag in tags %} {{ tag }} {% endfor %} 在view.py中: def tag_find(
我正在尝试在express.js中为我的网络应用程序创建一个路由系统,我需要知道是否需要使用app.get/post/put/delete.apply以编程方式设置多个功能对于一条路线。 也是如此 a
我正在通过示例查看 A.Mele Django,第 1 章 def post_list(request, category=None): object_list = Post.publishe
如果我想找到与IIS站点或应用程序关联的目录,我该怎么做? 我似乎无法从Get-Website和Get-WebApplication的对象的任何属性中找到任何允许我这样做的东西。 最佳答案 只需查看一
不知道发生了什么。当我执行以下代码时......它运行良好......但它产生了错误。如果我将以下内容粘贴到我的浏览器地址栏中并点击它,我会得到一个 URL。如果我通过 KRL http:get 输入
Curl 提供了一系列不同的带有 X 前缀的 http 方法调用,但也提供了不带 X 的相同方法。我两种都试过了,但我似乎无法弄清楚其中的区别。有人可以快速向我解释这两种操作有何不同吗? 最佳答案 默
request.GET.get 是什么意思?我在 Django 中看到类似的东西 page = request.GET.get('page', 1) 我认为它与类似的东西有关 « 它们是如
我正在从我的 Angular2 站点查询一些 Elasticsearch 服务器。为了帮助提高安全性,我们希望锁定对 GET 请求的访问权限。 Elasticsearch 支持带主体的 GET,但我在
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4年前关闭。 Improve t
调用 HTable.get(List) 返回的 Result 数组的顺序是什么? ? 我的意思是,假设与输入列表的顺序相同是否正确? 最佳答案 结果数组中的顺序将与输入列表的顺序相同。与批处理方法一样
所以我有一个看起来像这样的 JSON 数组: var myData = { foo : { biz : 'baz', fig : 'tree' } }
我正在学习 Ajax、javascript 和 html,并且有一个应用程序可以触发“get”请求,然后再触发另一个“get”请求。这些请求是用户按下按钮的结果。在我的 servlet 中,我使用 T
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 6 年前。 Improv
运行以下 cmdlet 适用于组成员(Amer 域中的组)中的所有用户,无论列出的用户位于哪个域: Get-ADGroupMember -Server amer 但是,当尝试通过管道传输到 Get-
我是一名优秀的程序员,十分优秀!