Okay so I have a basic index html file with a button that functions like this:
$("#button").click(function() {
$.get("./echo.php", function(data) {
alert(data);
});
});
I have one page called normal.php and all it has is some text:
Hello Test
I have another page called echo.php and for this basic example, it can find "Hello Test" in the normal.php file using regex:
<?php
$contents = file_get_contents("./normal.php");
preg_match('/Hello Test/s', $contents, $matches);
echo $matches[0];
?>
Makes sense so far? Okay this works all nice and well. You press the button, and it alerts "Hello Test" on the index page just fine. Likewise, if you just open up the echo.php page, it will just display "Hello Test" on the page.
Now the issue comes up when I am trying to access another website. So I'm basically trying to scrape weather data for a basic weather scraping website, and let's say echo.php is updated to have this following code instead:
<?php
$contents = file_get_contents("http://www.weather-forecast.com/locations/london/forecasts/latest");
preg_match('/3 days/s', $contents, $matches);
echo $matches[0];
?>
If you just load up echo.php manually on a web browser, this works. It will display "3 days" like it's supposed to. However, when I click the button on the website, nothing happens. It doesn't alert "3 days" like it's supposed to, even though it does display on the page just fine if echo.php is loaded up. What is the issue here?
I have been at this for a few hours already. I'm using the latest PHP version (7.2) and the latest JQuery (3.3.1). I've taken a look at these other threads but they were all similar but they did not solve my problem:
JQuery GET request won't get correct data from php echo
Problem with the jquery $.get function
jQuery get Function Not Returning Data