0

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?

12
  • 2
    Why do you define function get_data in a loop? Commented May 20, 2020 at 8:15
  • I need to check each and every $val page for the word active. Commented May 20, 2020 at 8:16
  • And defining funciton once outside the loop is not an option? Commented May 20, 2020 at 8:16
  • You do that by calling print_r(get_data($url));, no need to define the function itself that often. Move the function definition out of that loop to after error_reporting(E_ALL); You wouldn't even need to make it a function Commented May 20, 2020 at 8:17
  • @kerbh0lz so should i move function get_data ($url) to before the $ipaddress? wouldn't it miss $url? Commented May 20, 2020 at 8:22

1 Answer 1

1

Here's a working code. I renamed the function to something more meaningful and added the url to check inside the function. Note, you only need to declare the function once.

The function checkActiveIp() will now return true (if active was found in the response) or false if it wasn't. if ( checkActiveIp($ip) ) {... will call the function and check for the result and if the function returns true, will push the checked IP to the end of the array $activeIps using array_push().

In the end, the array $activeIps will contain all IPs whose response contains the word active.

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

function checkActiveIp($ip) {
    $url = "https://example.com/test/check?ip=$ip";
    $timeout = 5;

    $ch = curl_init();
    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){
        return true;
    } else {
        return false;
    }
}


$activeIps = array();
$ips = array("8.8.8.8", "1.1.1.1", "8.8.4.4");
foreach ($ips as $ip) {
    if ( checkActiveIp($ip) ) {
        array_push($activeIps, $ip);
    }
}

print_r($activeIps);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.