How can I check each result on a function and then go to the other function?
For example, if the first result is found echo found, After that go to check the second result on the second function
value = "'what is my ip'";
value1 = "'what is my country'";
value2 = "'what is best food'";
function check1($func) {
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://www.google.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
}
function check2($func) {
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://www.bing.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
}
function check3($func) {
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://www.example.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
}
if (empty($buffer)){
echo "found";
}else{
echo "not found";
}
CodePudding user response:
You should return the $buffer
. By the way, you are not ussing the $func
parameter so you don't really need it.
function check1() {
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://www.google.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
return $buffer;
}
Then, you can just call the functions sequentially and assign their return value to a variable, doing the checks as you go:
$buffer1 = check1()
if (!empty($buffer1)) {
echo 'Check1 - found!'
$buffer2 = check2()
if (!empty($buffer2) {
echo 'Check2 - found!'
$buffer3 = check3()
if (!empty($buffer3) {
echo 'Check3 - found!'
}
}
}
And obviously you can echo
the buffers in whenever you want. You can make this code better, but this way should be easy to understand.
CodePudding user response:
In order to check the result of a function, the function first needs to return something.
Your request is a bit vague, but maybe this is what you mean? It calls each function, echoes whether a response was received or not, and then calls the next one:
function check1() {
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://www.google.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
return $buffer;
}
function check2() {
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://www.bing.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
return $buffer;
}
function check3() {
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,'http://www.example.com');
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
return $buffer;
}
$res1 = check1();
if (!empty($res1)) echo "found 1";
else echo "not found 1";
$res2 = check2();
if (!empty($res2)) echo "found 2";
else echo "not found 2";
$res3 = check3();
if (!empty($res3)) echo "found 3";
else echo "not found 3";
If you want it so that the 2nd and 3rd functions are only executed if the previous one(s) found a result, then you'll need to adjust the if
statements accordingly.
N.B. The $func
arguments to each function seemed to be redundant, so I removed them.