I have setup this code for myself.
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$ipaddress = array("8.8.8.8", "1.1.1.1", "8.8.4.4");
foreach ($ipaddress as $key => $val) {
$url="https://example.com/test/check?ip=$val"; //
print_r(get_data($url)); //dumps the content, you can manipulate as you wish to
/* gets the data from a URL */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
$word = 'active';
// Test if string contains the word
if(strpos($data, $word) !== false){
echo "Word Found!";
} else{
echo "Word Not Found!";
}
}
}
?>
However for some reason (read my incapability), the value for $val is only 8.8.8.8, i want to substitute each IP in the array at the end of $url till i get echo "Word Found!".
I am stuck with the part $val, once i solve that, i can perhaps setup if/else command?
Can anyone help me with completing this code?
function get_datain a loop?print_r(get_data($url));, no need to define the function itself that often. Move the function definition out of that loop to aftererror_reporting(E_ALL);You wouldn't even need to make it a functionfunction get_data ($url)to before the$ipaddress? wouldn't it miss$url?