PHP获取汽车配件目录表格数据及零件编码存入数组求助
Hey there! I get that working with car parts catalogs can be tricky when you're not super comfortable with curl and parsing—let's walk through how to fix this step by step, from replacing curl to building clean tables and grabbing those part codes like PA7458.
替代CURL的更友好方案
Native curl can feel clunky, especially when handling request params and response parsing. Instead, use wrapped HTTP clients that simplify the process:
For PHP: Guzzle
Guzzle is the most popular HTTP client in the PHP ecosystem, and it takes care of most curl boilerplate for you. First install it via Composer:
composer require guzzlehttp/guzzle
For Frontend JS: Fetch API
Modern browsers support Fetch natively—no extra dependencies needed. It’s way simpler than XMLHttpRequest and handles promises out of the box.
Build a Standardized Parts Table
Whether your data comes as HTML or JSON, the goal is to output a semantic, maintainable table that’s easy to style and read. Here’s how:
Core Table Best Practices
- Use
<thead>for headers and<tbody>for data rows—this helps browsers and screen readers understand the table structure - Add a custom class (like
standard-parts-table) to make styling with CSS easier (e.g., borders, hover effects, responsive scrolling) - Include clear column headers like "Part Code", "Part Name", "Fitment", "Price"
Parse HTML to Generate Tables
If you’re pulling data from an existing HTML parts page, never use regex to scrape tables—use DOM parsing tools instead. They’re far more reliable if the page structure changes slightly:
PHP Example with DOMDocument & XPath
<?php require 'vendor/autoload.php'; use GuzzleHttp\Client; use DOMDocument; use DOMXPath; // 1. Fetch data with Guzzle (replace with your catalog URL) $client = new Client(); $response = $client->get('your-parts-catalog-url', [ 'query' => [ 'model' => 'FIAT Punto Evo 1.3 Multijet 16V' ] ]); $html = $response->getBody()->getContents(); // 2. Parse HTML to extract tables and part codes $dom = new DOMDocument(); libxml_use_internal_errors(true); // Ignore minor HTML parsing warnings $dom->loadHTML($html); libxml_clear_errors(); $xpath = new DOMXPath($dom); $partsTable = $xpath->query('//table[contains(@class, "parts-table")]')->item(0); // Target your table $partCodes = []; // 3. Build standardized table HTML $tableHtml = '<table class="standard-parts-table">'; // Add table headers $headers = $partsTable->getElementsByTagName('th'); $tableHtml .= '<thead><tr>'; foreach ($headers as $header) { $tableHtml .= '<th>' . htmlspecialchars($header->textContent) . '</th>'; } $tableHtml .= '</tr></thead><tbody>'; // Add table rows and extract part codes $rows = $partsTable->getElementsByTagName('tr'); foreach ($rows as $row) { $cells = $row->getElementsByTagName('td'); if ($cells->length === 0) continue; // Skip header rows $tableHtml .= '<tr>'; foreach ($cells as $cell) { $cellText = htmlspecialchars($cell->textContent); $tableHtml .= '<td>' . $cellText . '</td>'; // Extract PA-style part codes if (preg_match('/PA\d+/', $cellText, $matches)) { $partCodes[] = $matches[0]; } } $tableHtml .= '</tr>'; } $tableHtml .= '</tbody></table>'; // Output the table and part codes array echo $tableHtml; echo '<pre>' . print_r($partCodes, true) . '</pre>'; ?>
Parse JSON to Generate Tables
If your catalog provides a JSON API, this is even simpler—just decode the response and loop through the data:
<?php $response = $client->get('your-parts-api-url', [ 'query' => ['model' => 'FIAT Punto Evo 1.3 Multijet 16V'] ]); $partsData = json_decode($response->getBody(), true); $tableHtml = '<table class="standard-parts-table">'; $tableHtml .= '<thead><tr><th>Part Code</th><th>Part Name</th><th>Price</th></tr></thead><tbody>'; $partCodes = []; foreach ($partsData as $part) { $tableHtml .= "<tr><td>{$part['code']}</td><td>{$part['name']}</td><td>{$part['price']}</td></tr>"; if (str_starts_with($part['code'], 'PA')) { $partCodes[] = $part['code']; } } $tableHtml .= '</tbody></table>'; echo $tableHtml; print_r($partCodes); ?>
Extract Part Codes to an Array
To reliably grab codes like PA7458:
- Target the table column that contains part codes (or check all cells if codes are scattered)
- Use a regex pattern like
/PA\d+/to match the code format - Push matches into an array for later use
This method works whether you’re parsing HTML or JSON data.
内容的提问来源于stack exchange,提问作者Medy

