0

trying to find the best solution. I have a form which takes two inputs: input1 and input2. I use javascript to validate these fields - as long as one of them is filled in correctly that is ok.

At the moment, I have the following PHP (removed includes)

if (isset($_POST["input1"]) && !empty($_POST["input1"])){
    $input1 = $_POST["input1"];
    $connection = new APIConnection1();
    $response = $connection->obtainResponse($input1);
}

if (isset($_POST["input2"]) && !empty($_POST["input2"])){
    $input2 = $_POST["input2"];
    $connection = new APIConnection2();
    $response = $connection->obtainResponse($input2);
}

So if input1 has data - APIConnection1 is called. If input2 has data - APIConnection2 is called. If both of them have valid data - both APIs are called. So each input has its own API (different).

Here is my problem. I now have a third API, lets call it APIConnection3. If response returned for input1 is true, I need to send input1 to APIConnection3. Same applies with input2.

Problem is, I cant really make these calls within the above if statements because it will make separate calls to APIConnection3. So I need to somehow perform the above if statements, and then get the data as a whole to send to APIConnection3.

So, if both input1 and input2 return true, I want to make one call which would be

APIConnection3($input1, $input2);

If only one of them returns true, then it should be something like

APIConnection3($input1);

So how would I handle doing this?

Thanks

1

4 Answers 4

2

As you add more inputs , your code will be more difficult to extend, thats when design patterns become handy.

For example the Factory Method:

$inputs = new array();

if (isset($_POST["input1"]) && !empty($_POST["input1"])){
    $input1 = $_POST["input1"];
    $connection = new APIConnection1();
    $response = $connection->obtainResponse($input1);
    $inputs[0] = $input1;
}

if (isset($_POST["input2"]) && !empty($_POST["input2"])){
    $input2 = $_POST["input2"];
    $connection = new APIConnection2();
    $response = $connection->obtainResponse($input2);
    $inputs[1] = $input2;
}

//Call for each input
$connection = new APIConnection3();
foreach ($inputs as $input)
{   
    $response = $connection->obtainResponse($input);
}
Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest using a PHP function "call_user_func_array()" which accepts controller, method and parameters.

So you could do something like this:

$params = array();

if (isset($_POST["input1"]) && !empty($_POST["input1"])) {
    $input1 = $_POST["input1"];
    $connection = new APIConnection1();
    $response1 = $connection->obtainResponse($input1);
    array_push($params, $response1);
}
if (isset($_POST["input2"]) && !empty($_POST["input2"])) {
    $input2 = $_POST["input2"];
    $connection = new APIConnection2();
    $response2 = $connection->obtainResponse($input2);
    array_push($params, $response2);
}

$controller = new APIConnection3();
$response = call_user_func_array(array($controller, "obtainResponse"), $params);

2 Comments

Thats really helpful, works fantastically. One strange thing though. In my obtainResponse function I try to loop the params array. Its complaining that its an Invalid argument supplied for foreach(). Am I missing something here?
The parameters are passed as individual variables, so your function should expect 0 to 2 parameters coming. so you could declare the method like so: obtainResponse($response1 = null, $response2 = null). I edited the answer so the params array should contain response1 and/or response2.
1

There's dozens of ways you can do that. Here is one (i omitted most code, since your question is more about the logic):

if(CASE_01 || CASE_02) {
    if(CASE_01 && CASE_02) {
        //Both Inputs...
    }
    else if(CASE_01) {
        //Only Input 1...
    }
    else {
        //only input 2...
    }
}

Comments

1

You can achieve using CURL. Please check these CURL functions curl_multi_add_handle(),curl_multi_exec() and curl_multi_getcontent()

Create single function like multithread() and pass input1 and input2 value to this function , create loop of request [ your case will be 2] and execute using curl_multi_getcontent you will get result for both APIS like

    $master = curl_multi_init();

for($i = 0; $i < $node_count; $i++)
{
        $curl_arr[$i]       = curl_init();
       curl_setopt($curl_arr[$i], CURLOPT_URL, $apiturl);
            curl_multi_add_handle($master, $curl_arr[$i]);


 }
do {
    curl_multi_exec($master,$running);
    usleep(10000);
} while($running > 0);


for($i = 0; $i < $node_count; $i++)
{

    $results[$i] = curl_multi_getcontent  ( $curl_arr[$i]  );
}
 /* check result*/
 print_r($results);

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.