gpt4 book ai didi

php - 使用 GET 参数从 URL 将 CSV 列表保存到数据库 - PHP/MySQL

转载 作者:行者123 更新时间:2023-11-29 12:26:29 25 4
gpt4 key购买 nike

我正在尝试将 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 语句,我建议您使用 mysqliPDO 扩展,因为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com