1
我很头痛,想要弄明白这一点。无法通过类方法获取csv
我的文件夹结构如下:
index.php
helpers:
API.php
helpers.php
assets:
products.csv
debug:
debug_info.txt
我的索引文件看起来如下:
<?php
require_once 'helpers/API.php';
file_put_contents('debug/debug_info.txt', "New request started");
if (in_array($_GET['action'],array('insertOrder','updateOrder'))){
$date = date(DATE_RFC2822);
$api = new API();
file_put_contents('debug/debug_info.txt', "New: {$_GET['action']} at {$date}\n", FILE_APPEND | LOCK_EX);
file_put_contents('debug/debug_info.txt', "This is the product " . $api->getOrder());
}
API.php
<?php
class API {
private $order;
private $product_table;
function __construct(){
$this->order = $this->setOrder();
$this->product_table = $this->setProductTable();
}
public function setOrder(){return $this->readJSON();}
public function setProductTable(){return $this->readProductsCSV(__DIR__ . '/../assets/products.csv');}
public function getOrder(){return $this->order;}
public function getProductsTable(){return $this->product_table;}
private function readJSON(){
$stream = fopen('php://input', 'rb');
$json = stream_get_contents($stream);
fclose($stream);
return print_r(json_decode($json, true), true);
}
private function readProductsCSV($csv = '', $delimiter = ','){
if (!file_exists($csv) || !is_readable($csv)){
return "Someone f*cked up -_-";
}
$header = NULL;
$data = array();
if (($handle = fopen($csv, 'r')) !== false){
while (($row = fgetcsv($csv, 100, $delimiter)) !== false){
if (!$header)
$header = $row;
else if($row[0] != ''){
$row = array_merge(array_slice($row,0,2), array_filter(array_slice($row, 2)));
$sku = $row[0];
$data[$sku]['productCode'] = $row[1];
$data[$sku]['Description'] = $row[2];
}
}
fclose($handle);
}
array_change_key_case($data, CASE_LOWER);
return print_r($data, true);
}
}
当我使用file_put_contents('debug/debug_info.txt', $api->getOrder());
我得到数据正确(我必须评论所有的p roduct_table部件为它工作)。
但无论我做什么,我都无法获取CSV文件。 我跑file_exists() && is_readable
(他们通过)但仍然没有。
如果我在index.php
中声明函数readProductsCSV,它可以工作..但它似乎使用它作为一种方法来bug一切。
有人能帮我吗?
不错,赶上!非常感谢,M8!我还必须将'public function setProductTable()'中的'return'从'return $ this-> readProductsCSV(__ DIR__。'/../ assets/products.csv')''更改为'return $ this-> readProductsCSV $ csv = __DIR__。'/../ assets/products.csv')' – Onilol