Download 如何使用symfony 4从csv提要URL下载数据?
使用Symfony 4,我需要从远程URL下载数据。我可以使用symfony4吗,还是需要使用JQuery、Python或。。。?我是解析URL的内容还是可以从URL下载csv文件 我是个新手,所以请像个傻瓜一样跟我说话 我正在用Symfony 4开发一个web应用程序,该应用程序应该能够从合作伙伴商店下载数据(通过Symfony命令和CRON任务),这要归功于他们在自己的web应用程序上提供的URL,例如:Download 如何使用symfony 4从csv提要URL下载数据?,download,symfony4,Download,Symfony4,使用Symfony 4,我需要从远程URL下载数据。我可以使用symfony4吗,还是需要使用JQuery、Python或。。。?我是解析URL的内容还是可以从URL下载csv文件 我是个新手,所以请像个傻瓜一样跟我说话 我正在用Symfony 4开发一个web应用程序,该应用程序应该能够从合作伙伴商店下载数据(通过Symfony命令和CRON任务),这要归功于他们在自己的web应用程序上提供的URL,例如: Wine Title Vintage Country Region Sub reg
Wine Title Vintage Country Region Sub region Appellation Color Bottle Size Price URL FORMAT
The Last Drop 1971 Scotch Scotland 750ML 3999.99 HTTP://buckhead.towerwinespirits.com/sku63174.html 1x750ML
Petrus Pomerol 2015 France Bordeaux 750ML 3799.99 HTTP://buckhead.towerwinespirits.com/sku40582.html 1x750ML
Remy Martin Louis XIII Cognac France Cognac 750ML 3499.99 HTTP://buckhead.towerwinespirits.com/sku15758.html 1x750ML
Hennessy Paradis Imperial Cognac France Cognac 750ML 3299.99 HTTP://buckhead.towerwinespirits.com/sku51487.html 1x750ML
我看到了这条线索:
第一个答案看起来很有趣,但正如我所说,我是一个新手,不知道如何在命令中实现脚本。
我还看到了Ruby或Angular的其他线程:
但这对我没什么帮助
编辑:我试图将url传递给fopen,但它返回HTTP/1.1 403禁止:访问被拒绝
更新:以下是我迄今为止的代码(我承认不多),包括我尝试过的所有内容和结果:
class UpdateArticlesCommand extends Command
{
protected static $defaultName = 'app:update-articles';
protected $em = null;
protected function configure()
{
$this
->setDescription('Updates the articles of the stores having set a feed URL')
->setHelp('This command allows you to update the articles of the stores which have submitted a feed URL');
}
/**
* UpdateArticlesCommand constructor.
* @param EntityManagerInterface $em
* @param string|null $name
*/
public function __construct(EntityManagerInterface $em, ?string $name = null)
{
$this->em = $em;
parent::__construct($name);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$io->title('Attempting to import the feeds...');
$converter = new ConverterToArray();
$io->writeln([$store->getFeedUrl()]);
$url = $store->getFeedUrl();
// dd($url); //OK
$feedColumnsMatch = $store->getFeedColumnsMatch();
// dd($feedColumnsMatch); //OK
$fileName = $store->getName().'Feed.txt';
$filePath = $fileUploader->getTargetDirectory() . "/" . $fileName;
/* //sends a http request and save the given file
set_time_limit(0);
$fp = fopen($filePath, 'x+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
// give curl the file pointer so that it can write to it
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);//get curl response
curl_close($ch);
dd($data); //return false*/
/*dd($this->curl_get_file_contents($url)); //returns false*/
$client = new Client();
$response = $client->request('GET', $url);
echo $response->getStatusCode(); # 200
echo $response->getHeaderLine('content-type'); // 'application/json; charset=utf8'
echo $response->getBody(); // '{"id": 1420053, "name": "guzzle", ...}'
$articlesArray = $converter->convert("https://myURL.com", $feedColumnsMatch);
}
$io->success('Successful upload');
}
下面是我的转换器的代码:
/**
* is used to convert a csv file into an array of data
* @param $filePath
* @param FeedColumnsMatch $feedColumnsMatch
* @return array|string
*/
public function convert($filePath, $feedColumnsMatch)
{
// if(!file_exists($filePath) ) {
// return "existe pas";
// }
// if(!is_readable($filePath)) {
// return "pas lisible";
// }
//this array will contain the elements from the file
$articles = [];
$headerRecord = [];
if($feedColumnsMatch->getFeedFormat()==="tsv" | $feedColumnsMatch->getFeedFormat()==="csv"){
if($feedColumnsMatch->getFeedFormat()==="csv"){
$delimiter = $feedColumnsMatch->getDelimiter();
}else{
$delimiter = "\t";
}
//if we can open the file on mode "read"
if (($handle = fopen($filePath, 'r')) !== FALSE) {
//represents the line we are reading
$rowCounter = 0;
//as long as there are lines
while (($rowData = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
//At first line are written the keys so we record them in $headerRecord
if(0 === $rowCounter){
$headerRecord = $rowData;
}else{ //for every other lines...
foreach ($rowData as $key => $value){ //in each line, for each value
// we set $value to the cell ($key) having the same horizontal position than $value
// but where vertical position = 0 (headerRecord[]
$articles[$rowCounter][$headerRecord[$key]]= $value;
}
}
$rowCounter++;
}
fclose($handle);
}
}
return $articles;
}
我想我错过了一步。我无法直接从URL读取文件,因此我必须在尝试读取之前检索该文件。我如何才能做到这一点?要从提要URL下载数据,在我的例子中,是一个csv文件, 您必须向URL发送请求。Symfony不是用来向外部URL发送请求的,所以您必须使用cURL或。 我选择了狂饮。下面是我如何使用它的:
$client = new Client();
$response = $client->request('GET', $url);
echo "Status Code = ".$response->getStatusCode()."\n"; # 200
echo 'Content Type = '.$response->getHeaderLine('content-type')."\n";
$body = $response->getBody();
$url是我必须将请求发送到的url
不要忘记在命名空间和类之间导入Guzzle:
使用http\Client
使用此代码,您将获得页面的整个主体,即您获得的内容包含如下html标记:
<!DOCTYPE html>
<html lang="en">
<body>
<pre>
Wine Directory List
<BR>
//here is the content of the csv file
</pre>
</body>
</html>
然后创建/打开该文件:
fwrite($fp, $body);
fclose($fp);
然后在文件中写入:
fwrite($fp, $body);
fclose($fp);
不要忘记关闭文件以避免内存泄漏:
最后,您只需在方便的时候转换文件。请记住,fopen()中的模式“x”会创建一个文件,如果已经存在同名文件,则会返回一个错误。您是否尝试了一个或可能(因为您每次都可以提供准确文档的URL,对不对?)此外,请将问题更新为不需要外部链接。例如,从CSV文件URL复制几行,并将它们作为代码块粘贴到问题中就足够了。根据需要提供其他信息,例如该链接使用“制表符”字符作为列分隔符。@rkeet done:)(感谢您提供有关外部链接的提示。我刚刚提出了我的问题)太好了,我还没有看到fopen使用URL。我想试试。保持更新。我尝试在ConverterToArray类中为fopen提供url,但它向我发送了一个```警告:fopen():无法打开流:HTTP请求失败!HTTP/1.1 403禁止:访问被拒绝。我尝试在ConverterToArray类中将url提供给fopen,但它向我发送了一个```警告:fopen(myurl.com):无法打开流:HTTP请求失败!HTTP/1.1 403禁止:访问被拒绝。它对文件内容也有同样的作用。这是否意味着远程网站正在阻止访问?或者可能是我的代码有问题?