我正在尝试将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除此之外不会提供任何支持。
非常感谢你。 NJ
答案 0 :(得分:1)
首先,您需要从另一台服务器收集csv。因此,如果您的托管服务提供商支持在网址上进行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
发送到服务器
答案 1 :(得分:0)
您需要首先解析csv,使用explode()函数解析产品评论数组,然后您可以使用数组中的foreach轻松地将这些数据插入数据库中。