php 进行不同的API调用

3vpjnl9f  于 2024-01-05  发布在  PHP
关注(0)|答案(1)|浏览(222)

我想做两个Fetch API调用来将数据从我的JS文件传输到我的PHP文件。第一个调用是有条件的,并传输一个数据数组(在我的PHP文件中得到了正确的处理)第二个API调用是一种计时器(使用setInterval函数),我想用它来检查客户端是否仍在浏览器上(是的,这是最终的目标!)。2个电话工作正常(没有得到错误,可以从第一个数组中正确获取数据)。问题是,我不知道如何在PHP文件中的变量中单独获取计时器数据(我下一步会把它推到数组中)!你能告诉我怎么做吗(我还是个初学者。)
下面是我的JS文件中的代码
第一个电话

  1. if(...Button pressed...){
  2. fetch("cart.php",{
  3. "method":"POST",
  4. "Headers":{
  5. "content-type":"application/json; charset=utf-8"
  6. },
  7. "body":JSON.stringify(arrayOrderedProducts)
  8. })
  9. .then(function(response){
  10. return response.text()
  11. })
  12. .then(function(data){
  13. console.log(data)
  14. })
  15. }

字符串
二呼叫

  1. setInterval(()=>{
  2. timer+=1
  3. },1000)
  4. function checkConnection(){
  5. fetch ("cart.php",{
  6. method:"Post",
  7. body :timer
  8. })
  9. .then(
  10. response=>{
  11. return response.text()})
  12. .then(
  13. response=>{
  14. console.log(timer)
  15. })
  16. .catch(
  17. error=>{
  18. console.log(error)
  19. })
  20. }
  21. let intervalConCheck = setInterval(()=>checkConnection(),1000)


下面是我的PHP文件中的代码

  1. <?php
  2. if(isset($_POST)){
  3. $data = file_get_contents("php://input");
  4. $orders = json_decode($data);
  5. ...rest of code that retrieve data in arrayOrderedProducts...
  6. }
  7. ?>

hxzsmxv2

hxzsmxv21#

为了实现你想要的,你需要修改你的JavaScript代码,在第一次调用的时候单独发送计时器值。下面是一个如何构造你的JavaScript代码的例子:

  1. let arrayOrderedProducts = [...] // Your array of ordered products
  2. let timer = 0; // Initialize timer
  3. // First Call
  4. if (... /* Button pressed condition */) {
  5. fetch("cart.php", {
  6. method: "POST",
  7. headers: {
  8. "Content-Type": "application/json; charset=utf-8"
  9. },
  10. body: JSON.stringify({ orders: arrayOrderedProducts, timer: timer })
  11. })
  12. .then(response => response.text())
  13. .then(data => {
  14. console.log(data);
  15. })
  16. .catch(error => {
  17. console.log(error);
  18. });
  19. }
  20. // Second Call (Timer)
  21. setInterval(() => {
  22. timer += 1;
  23. // Update the timer value in the PHP file
  24. fetch("cart.php", {
  25. method: "POST",
  26. headers: {
  27. "Content-Type": "text/plain"
  28. },
  29. body: timer.toString()
  30. })
  31. .then(response => response.text())
  32. .then(data => {
  33. console.log(data);
  34. })
  35. .catch(error => {
  36. console.log(error);
  37. });
  38. }, 1000);

字符串
在PHP文件中,您可以分别处理从两个调用接收的数据:

  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  3. $data = file_get_contents("php://input");
  4. // Check if the data is JSON (from the first call)
  5. $jsonData = json_decode($data, true);
  6. if ($jsonData !== null && isset($jsonData['orders'])) {
  7. $orders = $jsonData['orders'];
  8. // Process the $orders array here
  9. // ...
  10. // Example: Send a response back to the client
  11. echo "First call data processed successfully";
  12. }
  13. // Check if the data is plain text (from the second call)
  14. else {
  15. $timer = intval($data);
  16. // Process the $timer value here
  17. // ...
  18. // Example: Send a response back to the client
  19. echo "Second call data processed successfully";
  20. }
  21. }
  22. ?>


通过这种方式,您可以区分第一次调用中发送的数据(包含订购产品的数组)和第二次调用中发送的数据(包含计时器值)。根据您的特定需求调整PHP文件中的处理逻辑。

展开查看全部

相关问题