2015-06-16 78 views
0

试图找到最佳解决方案。我有一个表单需要两个输入:input1和input2。我使用JavaScript来验证这些字段 - 只要其中一个填写正确无误。php处理多个API调用

目前,我有以下PHP(除去包含)

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); 
} 

因此,如果输入1有数据 - APIConnection1被调用。 如果input2有数据 - APIConnection2被调用。 如果它们都有有效的数据 - 两个API都被调用。所以每个输入都有自己的API(不同)。

这是我的问题。我现在有第三个API,可以称之为APIConnection3。如果为input1返回的响应为true,则需要将input1发送到APIConnection3。同样适用于input2。

问题是,我不能在上面的if语句中真正地调用这些调用,因为它会分别调用APIConnection3。所以我需要以某种方式执行上面的if语句,然后将整个数据发送到APIConnection3。

因此,如果同时输入1和输入2返回true,我想打一个电话,这将是

APIConnection3($input1, $input2); 

如果只有一个返回true,那么就应该是这样的

APIConnection3($input1); 

那么我会如何处理这个问题呢?

谢谢

回答

1

我会使用PHP函数“call_user_func_array()”,它接受控制器,方法和参数的建议。

所以你可以做这样的事情:

$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); 
+0

那是真正有用的,作品充满了想象。一个奇怪的事情,但。在我的obtainResponse函数中,我尝试循环参数数组。它抱怨说它为foreach()提供了一个无效参数。我在这里错过了什么吗? –

+0

参数作为单个变量传递,因此您的函数应该会有0到2个参数。所以你可以像这样声明方法:obtainResponse($ response1 = null,$ response2 = null)。我编辑了答案,因此params数组应该包含response1和/或response2。 – eselskas

1

有几十种方法可以做到这一点。这里是一个(我省略了大部分代码,因为您的问题更多地是关于逻辑):

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

您可以使用CURL来实现。请检查这些卷曲功能 curl_multi_add_handle(),curl_multi_exec()和curl_multi_getcontent()

创建像多线程(单一功能),并输入2的值通过输入1到这个功能,创建的请求循环[你的情况将是2]执行使用curl_multi_getcontent你会得到导致两个API,如

$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); 
2

当您添加更多的投入,你的代码将更加难以扩展,这就是在设计模式变得得心应手。

例如工厂方法:

$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); 
}