fork download
  1. <?php
  2. function getRoyalMailStatus($trackingNumber) {
  3. // Validate tracking number format
  4. if (!preg_match('/^[A-Z0-9]{9,13}$/i', $trackingNumber)) {
  5. return "Error: Invalid tracking number format";
  6. }
  7.  
  8. $url = "https://w...content-available-to-author-only...l.com/track-your-item#/tracking-results/" . $trackingNumber;
  9.  
  10. $ch = curl_init();
  11.  
  12. CURLOPT_URL => $url,
  13. CURLOPT_RETURNTRANSFER => true,
  14. CURLOPT_FOLLOWLOCATION => true,
  15. 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',
  16. CURLOPT_SSL_VERIFYPEER => true, // Always verify SSL
  17. CURLOPT_TIMEOUT => 30,
  18. CURLOPT_HTTPHEADER => [
  19. 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  20. 'Accept-Language: en-US,en;q=0.5',
  21. 'Connection: keep-alive',
  22. ],
  23. ]);
  24.  
  25. $response = curl_exec($ch);
  26. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  27.  
  28. if (curl_error($ch)) {
  29. $error = curl_error($ch);
  30. curl_close($ch);
  31. return "cURL Error: " . $error;
  32. }
  33.  
  34. curl_close($ch);
  35.  
  36. if ($httpCode !== 200) {
  37. return "HTTP Error: " . $httpCode;
  38. }
  39.  
  40. // Check if we got the actual tracking data or just a loading page
  41. if (strpos($response, 'tracking-results') === false) {
  42. return "Error: Unable to retrieve tracking data. The site may require JavaScript.";
  43. }
  44.  
  45. return parseTrackingData($response);
  46. }
  47.  
  48. function parseTrackingData($html) {
  49. // This is a simplified parser - Royal Mail's actual structure is more complex
  50. if (preg_match('/<div class="tracking-status">(.*?)<\/div>/s', $html, $matches)) {
  51. return trim(strip_tags($matches[1]));
  52. }
  53.  
  54. if (preg_match('/<title>(.*?)<\/title>/', $html, $matches)) {
  55. return "Status not found in page. Title: " . $matches[1];
  56. }
  57.  
  58. return "Unable to parse tracking information";
  59. }
  60.  
  61. // Usage
  62. $trackingNumber = "MZ396583547GB";
  63. $result = getRoyalMailStatus($trackingNumber);
  64. echo $result;
  65. ?>
Success #stdin #stdout 0.03s 26400KB
stdin
Standard input is empty
stdout
cURL Error: Could not resolve host: www.royalmail.com