fork download
  1. <?php
  2.  
  3. function scrapeFlights($origin, $destination, $date) {
  4. $url = "https://w...content-available-to-author-only...t.com/search-flight?origin={$origin}&destination={$destination}&date={$date}";
  5.  
  6. // Initialize cURL
  7. $ch = curl_init();
  8. curl_setopt($ch, CURLOPT_URL, $url);
  9. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  10. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
  11.  
  12. // Execute cURL request
  13. $response = curl_exec($ch);
  14. curl_close($ch);
  15.  
  16. // Load HTML response into DOMDocument
  17. $dom = new DOMDocument();
  18. @$dom->loadHTML($response); // Use @ to suppress warnings for invalid HTML
  19.  
  20. $xpath = new DOMXPath($dom);
  21. $flights = [];
  22.  
  23. // XPath query to find flight elements (adjust according to actual HTML structure)
  24. $flightNodes = $xpath->query("//div[contains(@class, 'flight-list')]//div[contains(@class, 'flight-item')]");
  25.  
  26. foreach ($flightNodes as $node) {
  27. $flight = [];
  28.  
  29. // Extract flight details (adjust these queries based on the actual HTML structure)
  30. $flight['airline'] = $xpath->query(".//span[contains(@class, 'airline-name')]", $node)->item(0)->nodeValue ?? '';
  31. $flight['price'] = $xpath->query(".//span[contains(@class, 'flight-price')]", $node)->item(0)->nodeValue ?? '';
  32. $flight['departure'] = $xpath->query(".//span[contains(@class, 'departure-time')]", $node)->item(0)->nodeValue ?? '';
  33. $flight['arrival'] = $xpath->query(".//span[contains(@class, 'arrival-time')]", $node)->item(0)->nodeValue ?? '';
  34.  
  35. if (!empty($flight['airline'])) {
  36. $flights[] = $flight;
  37. }
  38. }
  39.  
  40. return $flights;
  41. }
  42.  
  43. // Example usage
  44. $origin = 'JKT'; // Jakarta
  45. $destination = 'SUB'; // Surabaya
  46. $date = '2024-11-01'; // Flight date
  47. $flightResults = scrapeFlights($origin, $destination, $date);
  48.  
  49. print_r($flightResults);
  50. ?>
  51.  
Success #stdin #stdout 0.03s 26280KB
stdin
Standard input is empty
stdout
Array
(
)