多个API调用 - 许多卷曲响应

时间:2014-09-22 14:50:20

标签: php json curl

我可能在错误的树上吠叫,我需要指向正确的方向。我正在构建WIFI门户。实质上:

  1. 用户详细说明。
  2. 详细信息正通过cURL发送到REST后呼叫。
  3. <?php
    
    include 'functions.php';
    
    session_start();
    
    $user_mac = $_SESSION["id"];        //user's mac address
    $ap_mac = $_SESSION["ap"];            //AP mac
    $ssid = $_SESSION["ssid"];          //ssid the user is on (POST 2.3.2)
    $time = $_SESSION["t"];             //time the user attempted a request of the portal
    $url = $_SESSION["url"];         //url the user attempted to reach
    $sesh = $_SESSION["rand"];
    
    /*
    //Check what gets send
    echo $user_mac;        
    echo '<br/>';
    echo $ap_mac;         
    echo '<br/>';
    echo $ssid;       
    echo '<br/>';
    echo $time;            
    echo '<br/>';
    echo $url;         
    echo '<br/>';
    echo $sesh;
    echo '<br/>';
    echo $_POST["EMAIL"];
    */
    
    $ch = curl_init();
    
    $service_url = 'https://blablabla.com/rest/wifi/post_test';
    
    $parm =  array(
          'KEY' => '1',
          'BOOKING_ID' => $_POST["BOOKING_ID"],
          'GUEST_NAME' => $_POST["GUEST_NAME"],
          'EMAIL' => $_POST["EMAIL"],
          'AP_MAC' => $ap_mac,
          'USER_MAC' => $user_mac,
          'SSID' => $ssid,
          'REACH_ID' => $url,     
          'LOG_TIME' => $time,
          'SESH_ID'=> $sesh
          );
    
    
    curl_setopt($ch, CURLOPT_URL,$service_url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($parm));
    
    
    // curl_setopt($ch, CURLOPT_POSTFIELDS, 
    //          http_build_query(array('postvar1' => 'value1')));
    
    // get the reposne from serv
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $server_output = curl_exec($ch);
    
    $jsonString = json_encode($server_output, true);
    
    $data = json_decode($jsonString);
    
    
    
    //show the response
    
    
    echo '<pre>',print_r($data),'</pre>';
    
        curl_close ($ch);
    
    
    
    
    
         $macreturned = $data[0]['usermac'];
         $macreturned1 = $jsonString[0]["usermac"];
    
      echo    $macreturned;
      echo    $macreturned1;
    
    
      $status = $jsonString[0]["Status"];
    
      $status_id = (int)$status;
    
    
    
      IF ($status_id == 1 ) {
    
          echo "Access denied.";
        }
      ELSE 
      {
          echo "Access provided.";
    
          sendAuthorization($macreturned,10);
      }
    
    
    
    
        curl_close ($ch);
    
    
    ?>
    
    1. Oracle DB处理输入。
    2. DB以JSON字符串的形式向门户发送响应,其中包含MAC地址和会话长度等数据。
    3.   

      {      &#34;状态&#34;:&#34; 2&#34 ;,      &#34; usermac&#34;:&#34; 11:11:11:11:11&#34 ;,      &#34;返回消息&#34;:&#34;我们在&#34;,      &#34; sesh_length&#34;:&#34; 14400&#34 ;,      &#34; sesh_id&#34;:&#34; 4568&#34;   }

      1. Oracle DB处理输入。
      2.   

        {      &#34;状态&#34;:&#34; 2&#34 ;,      &#34; usermac&#34;:&#34; 11:11:11:11:11&#34 ;,      &#34;返回消息&#34;:&#34;我们在&#34;,      &#34; sesh_length&#34;:&#34; 14400&#34 ;,      &#34; sesh_id&#34;:&#34; 4568&#34;   }

        1. DB以JSON字符串形式向门户发送响应,其中包含MAC地址和会话长度等数据。

        2. 根据状态输出,它会通过函数 sendAuthorization

        3. 执行另一个调用
          <?php
          
          
          function sendAuthorization($id, $minutes)
          {
              $unifiServer = "https://1.1.1.1";
              $unifiUser = "Admin";
              $unifiPass = "Admin";
          
              // Start Curl for login
              $ch = curl_init();
              // We are posting data
              curl_setopt($ch, CURLOPT_POST, TRUE);
              // Set up cookies
              $cookie_file = "/tmp/unifi_cookie";
              curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
              curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
              // Allow Self Signed Certs
              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
              curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
              // Force SSL3 only
              curl_setopt($ch, CURLOPT_SSLVERSION, 3);
              // Login to the UniFi controller
              curl_setopt($ch, CURLOPT_URL, "$unifiServer/login");
              curl_setopt($ch, CURLOPT_POSTFIELDS,
                  "login=login&username=$unifiUser&password=$unifiPass");
              // send login command
              curl_exec ($ch);
          
              // Send user to authorize and the time allowed
              $data = json_encode(array(
                  'cmd'=>'authorize-guest',
                  'mac'=>$id,
                  'minutes'=>$minutes));
          
              // Send the command to the API
              curl_setopt($ch, CURLOPT_URL, $unifiServer.'/api/cmd/stamgr');
              curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.$data);
              curl_exec ($ch);
          
              // Logout of the UniFi Controller
              curl_setopt($ch, CURLOPT_URL, $unifiServer.'/logout');
              curl_exec ($ch);
              curl_close ($ch);
              unset($ch);
          }
          
          
          
          ?>
          

          现在这是它停止工作的时候。我需要与控制器对话,所以我需要执行另一个函数,使用cURL将两个元素usermac与sesh_length一起传递。然而,在从服务器接收JSON后,PHP方面的东西正在停止执行另一个cURL函数。

          现在有关于运行多个cURL脚本的信息,但我需要根据初始调用的结果执行调用CALL。

          任何帮助都非常感谢,我不会在这方面工作,所以我对PHP和cURL知之甚少。

0 个答案:

没有答案