Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize | SQLize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php require 'vendor/autoload.php'; // برای استفاده از کتابخانه‌ها use Symfony\Component\DomCrawler\Crawler; use GuzzleHttp\Client; function getPriceFromSite($url, $partNumber, $selector) { $client = new Client([ 'timeout' => 10, 'headers' => [ 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' ] ]); try { $response = $client->get($url . urlencode($partNumber)); $html = (string)$response->getBody(); $crawler = new Crawler($html); $priceText = $crawler->filter($selector)->text(); // استخراج عدد از متن قیمت preg_match('/\d{1,3}(?:,\d{3})*/', $priceText, $matches); $price = str_replace(',', '', $matches[0]); return (int)$price; } catch (Exception $e) { return null; } } function getAveragePrice($productName, $partNumber) { $sites = [ [ 'url' => 'https://hpelka.com/search?q=', 'selector' => '.product-price' ], [ 'url' => 'https://asapardazesh.ir/search?query=', 'selector' => '.price' ], [ 'url' => 'https://ysorkh.net/?s=', 'selector' => '.woocommerce-Price-amount' ] ]; $prices = []; foreach ($sites as $site) { $price = getPriceFromSite($site['url'], $partNumber, $site['selector']); if ($price) { $prices[] = $price; } } return count($prices) > 0 ? array_sum($prices) / count($prices) : null; } // دریافت اطلاعات از کاربر if ($_SERVER['REQUEST_METHOD'] === 'POST') { $productName = $_POST['product_name'] ?? ''; $partNumber = $_POST['part_number'] ?? ''; if (!empty($productName) && !empty($partNumber)) { $averagePrice = getAveragePrice($productName, $partNumber); } } ?> <!DOCTYPE html> <html lang="fa" dir="rtl"> <head> <meta charset="UTF-8"> <title>مقایسه قیمت محصولات</title> <style> body { font-family: Tahoma; padding: 20px; max-width: 800px; margin: 0 auto; } .form-group { margin-bottom: 15px; } input { width: 100%; padding: 8px; } button { background: #4CAF50; color: white; border: none; padding: 10px 15px; } .result { margin-top: 20px; padding: 15px; border: 1px solid #ddd; } .price { background: #f0f0f0; padding: 10px; margin: 5px 0; } </style> </head> <body> <h1>مقایسه قیمت محصولات</h1> <form method="post"> <div class="form-group"> <label>نام محصول:</label> <input type="text" name="product_name" required> </div> <div class="form-group"> <label>پارت نامبر:</label> <input type="text" name="part_number" required> </div> <button type="submit">محاسبه قیمت</button> </form> <?php if (isset($averagePrice)): ?> <div class="result"> <h3>نتایج برای <?= htmlspecialchars($productName) ?> (<?= htmlspecialchars($partNumber) ?>)</h3> <?php if ($averagePrice): ?> <div class="price">میانگین قیمت: <?= number_format($averagePrice) ?> تومان</div> <?php else: ?> <div class="price">قیمتی یافت نشد</div> <?php endif; ?> <small>اطلاعات از سایت‌های hpelka.com, asapardazesh.ir, ysorkh.net استخراج شده است</small> </div> <?php endif; ?> </body> </html>
Copy Clear