fork download
  1. <?php
  2. function fetchUrlContent($url) {
  3. // Initialize cURL session
  4. $ch = curl_init();
  5.  
  6. // Set cURL options
  7. curl_setopt($ch, CURLOPT_URL, $url);
  8. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the content as a string
  9. curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Set a timeout (in seconds)
  10. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects
  11. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification (optional)
  12.  
  13. // Resolve DNS dynamically by adding Google's DNS (8.8.8.8)
  14. // We'll allow cURL to resolve the URL to its IP using Google's DNS server.
  15. $domain = parse_url($url, PHP_URL_HOST);
  16. curl_setopt($ch, CURLOPT_RESOLVE, ["$domain:80:8.8.8.8"]);
  17.  
  18. // Execute cURL and get the response
  19. $response = curl_exec($ch);
  20.  
  21. // Check for errors
  22. if(curl_errno($ch)) {
  23. echo 'Error: ' . curl_error($ch);
  24. }
  25.  
  26. // Close the cURL session
  27. curl_close($ch);
  28.  
  29. return $response;
  30. }
  31.  
  32. function handleURL($url) {
  33. if (filter_var($url, FILTER_VALIDATE_URL)) {
  34. // If the URL is valid, fetch its content
  35. $content = fetchUrlContent($url);
  36.  
  37. if ($content) {
  38. return $content;
  39. } else {
  40. return "Failed to retrieve content from $url.";
  41. }
  42. } else {
  43. return "Invalid URL: $url.";
  44. }
  45. }
  46.  
  47. // Example Usage
  48. $urls = [
  49. 'https://w...content-available-to-author-only...s.com/sites/robertadams/2017/03/02/top-income-earning-blogs/',
  50. 'https://b...content-available-to-author-only...a.com',
  51. 'https://w...content-available-to-author-only...e.com'
  52. ];
  53.  
  54. foreach ($urls as $url) {
  55. $result = handleURL($url);
  56. echo "Result for $url:\n";
  57. echo $result . "\n\n";
  58. }
  59.  
  60.  
  61. ?>
Success #stdin #stdout 0.04s 26004KB
stdin
Standard input is empty
stdout
Error: Could not resolve host: www.forbes.comResult for https://w...content-available-to-author-only...s.com/sites/robertadams/2017/03/02/top-income-earning-blogs/:
Failed to retrieve content from https://w...content-available-to-author-only...s.com/sites/robertadams/2017/03/02/top-income-earning-blogs/.

Error: Could not resolve host: blogsikka.comResult for https://b...content-available-to-author-only...a.com:
Failed to retrieve content from https://b...content-available-to-author-only...a.com.

Error: Could not resolve host: www.example.comResult for https://w...content-available-to-author-only...e.com:
Failed to retrieve content from https://w...content-available-to-author-only...e.com.