我试图制作一个php脚本,从他们的网站上获取竞争对手产品的价格,我成功地获得了一个带有json输出的功能良好的脚本,但是当我想让竞争对手的urls成为用户输入的变量时,这一切都被抛到了一边。
我知道我一定是搞砸了,这实际上是我的第一个php脚本。
以下是“第一个版本”(起作用的版本):
<?php
require 'C:\Users\dgayg\vendor\autoload.php';
use \GuzzleHttp\Client;
use \PHPHtmlParser\Dom;
use \CurrencyDetector\Detector;
$productPairs = [
'ASICS FUJITRABUCO LYTE' => [
'RunnerInn' => [
'url' => 'https://www.runnerinn.com/magasin-running/asics-chaussures-de-femme-asics-fujitrabuco-lyte/138541586/p',
'selectorPath' => '#total_dinamic'
],
'iRun' => [
'url' => 'https://www.i-run.fr/chaussures_homme/Trail_c15/Asics_m1/Asics-FujiTrabuco-Lyte-M_Asics_fiche_92311.html',
'selectorPath' => '.price'
],
'Zalando' => [
'url' => 'https://www.zalando.fr/asics-gel-trabuco-terra-chaussures-de-running-deep-sea-tealblazing-coral-as141a0r6-k11.html',
'selectorPath' => 'span.uqkIZw.ka2E9k.uMhVZi.dgII7d._6yVObe._88STHx.cMfkVL'
]
]
];
$detector = new Detector();
$comparison = [];
foreach ($productPairs as $productName => $pair) {
foreach($pair as $provider => $product) {
$client = new Client();
$parser = new Dom;
$request = $client->request('GET', $product['url']);
$response = (string) $request->getBody();
$parser->loadStr($response);
$price = $parser->find($product['selectorPath'])[0];
$priceString = $price->text;
$fmt = new NumberFormatter( 'fr_FR', NumberFormatter::CURRENCY );
$comparison[$productName][$provider] = [
'Prix' => $detector->getAmount($priceString)." ".$detector->getCurrency($priceString)
];
}
}
echo json_encode($comparison);它的输出如下:{"ASICS FUJITRABUCO LYTE":{"RunnerInn":{"Prix":"120.26 EUR"},"iRun":{"Prix":"94 EUR"},"Zalando":{"Prix":"76.95 EUR"}}}
下面是我将代码放入函数并试图让用户输入URL的版本:
<?php
require 'C:\Users\dgayg\vendor\autoload.php';
use \GuzzleHttp\Client;
use \PHPHtmlParser\Dom;
use \CurrencyDetector\Detector;
function comparateur($site1, $site2) {
return
$productPairs = [
'ASICS FUJITRABUCO LYTE' => [
'iRun' => [
'url' => $site1,
'selectorPath' => '.price'
],
'Zalando' => [
'url' => $site2,
'selectorPath' => 'span.uqkIZw.ka2E9k.uMhVZi.dgII7d._6yVObe._88STHx.cMfkVL'
]
]
];
$detector = new Detector();
$comparison = [];
foreach ($productPairs as $productName => $pair) {
foreach($pair as $provider => $product) {
$client = new Client();
$parser = new Dom;
$request = $client->request('GET', $product['url']);
$response = (string) $request->getBody();
$parser->loadStr($response);
$price = $parser->find($product['selectorPath'])[0];
$priceString = $price->text;
$fmt = new NumberFormatter( 'fr_FR', NumberFormatter::CURRENCY );
$comparison[$productName][$provider] = [
'Prix' => $detector->getAmount($priceString)." ".$detector->getCurrency($priceString)
];
}
}
echo json_encode($comparison);
}
// echo comparateur(($_POST['site1']), ($_POST['site2']));
if (isset($_POST['site1']) && isset($_POST['site2'])) {
$result = comparateur($_POST['site1'], $_POST['site2']);
}
?>
<html>
<body>
<!-- <p>https://www.i-run.fr/chaussures_homme/Trail_c15/Asics_m1/Asics-FujiTrabuco-Lyte-M_Asics_fiche_92311.html</p>
<p>https://www.zalando.fr/asics-gel-trabuco-terra-chaussures-de-running-deep-sea-tealblazing-coral-as141a0r6-k11.html</p> -->
<form action="" method="post">
<p>iRun: <input type="text" name="site1" value="https://www.i-run.fr/chaussures_homme/Trail_c15/Asics_m1/Asics-FujiTrabuco-Lyte-M_Asics_fiche_92311.html" /></p>
<p>Zalando: <input type="text" name="site2" value="https://www.zalando.fr/asics-gel-trabuco-terra-chaussures-de-running-deep-sea-tealblazing-coral-as141a0r6-k11.html" /></p>
<p><input type="submit"/></p>
<?php if (isset($result)) { ?>
<h1> Result: <?php print_r($result);?></h1>
<?php } ?>
</body>
</html>它的输出如下:Result: Array ( [ASICS FUJITRABUCO LYTE] => Array ( [iRun] => Array ( [url] => https://www.i-run.fr/chaussures_homme/Trail_c15/Asics_m1/Asics-FujiTrabuco-Lyte-M_Asics_fiche_92311.html [selectorPath] => .price ) [Zalando] => Array ( [url] => https://www.zalando.fr/asics-gel-trabuco-terra-chaussures-de-running-deep-sea-tealblazing-coral-as141a0r6-k11.html [selectorPath] => span.uqkIZw.ka2E9k.uMhVZi.dgII7d._6yVObe._88STHx.cMfkVL ) ) )
有人能告诉我我在哪里搞砸了吗?因为我真的搞不清楚。
非常感谢!
发布于 2022-01-12 05:00:22
尝试此操作,将回显内部函数更改为返回。
<?php
function comparateur($site1, $site2) {
return
$productPairs = [
'ASICS FUJITRABUCO LYTE' => [
'iRun' => [
'url' => $site1,
'selectorPath' => '.price'
],
'Zalando' => [
'url' => $site2,
'selectorPath' => 'span.uqkIZw.ka2E9k.uMhVZi.dgII7d._6yVObe._88STHx.cMfkVL'
]
]
];
$detector = new Detector();
$comparison = [];
foreach ($productPairs as $productName => $pair) {
foreach($pair as $provider => $product) {
$client = new Client();
$parser = new Dom;
$request = $client->request('GET', $product['url']);
$response = (string) $request->getBody();
$parser->loadStr($response);
$price = $parser->find($product['selectorPath'])[0];
$priceString = $price->text;
$fmt = new NumberFormatter( 'fr_FR', NumberFormatter::CURRENCY );
$comparison[$productName][$provider] = [
'Prix' => $detector->getAmount($priceString)." ".$detector->getCurrency($priceString)
];
}
}
return json_encode($comparison); // CHANGE echo to return
}
// echo comparateur(($_POST['site1']), ($_POST['site2']));
if (isset($_POST['site1']) && isset($_POST['site2'])) {
$result = comparateur($_POST['site1'], $_POST['site2']);
echo $result;
}https://stackoverflow.com/questions/70675198
复制相似问题