简体   繁体   English

在curl完成执行之前php重定向

[英]php redirect before curl completes execution

So we have a system that can alter what you see on a display. 因此,我们拥有一个可以更改您在显示器上看到的内容的系统。 This code is meant to send the command being "argument" to that JQuery page. 该代码旨在将“参数”命令发送到该JQuery页面。

The curl is working beautifully, but when we comment out the if statement that handles the redirection to an XML document. curl的工作很漂亮,但是当我们注释掉处理重定向到XML文档的if语句时。 The redirect occurs and the Curl request is not executed. 发生重定向,并且不执行Curl请求。 We need to understand why this is happening, when we remove the Redirects it runs the curl request. 我们需要了解为什么会这样,当我们删除重定向时,它将运行curl请求。 When the Redirects are in place the curl request is not executed. 重定向到位后,不会执行curl请求。

Please help in finding the cause of this issue and a way to rectify the problem, we have to keep the redirects but if there is a better way of running the curl request, ie not using curl but something that would be great, any suggestion will be tested. 请帮助找出问题的原因和解决问题的方法,我们必须保留重定向,但是如果有更好的方式来运行curl请求,即不使用curl而是一种很棒的方法,那么任何建议都会被测试。

<html>
<head>
<?php
    ob_implicit_flush(false);
    $argument = $_GET["argument"];
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, "http://example.com/sub/page.html?api_key=**********&device_id=************&argument=".$argument."");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);

  if($argument == 1) {
       echo '<META http-equiv="refresh" content="0;URL=http://adloc8tor.com/ussd/service1.xml">';
  } else if($argument == 2) {
       echo '<META http-equiv="refresh" content="0;URL=http://adloc8tor.com/ussd/service2.xml">';
  } else if($argument == 3) {
       echo '<META http-equiv="refresh" content="0;URL=http://adloc8tor.com/ussd/service3.xml">';
  } else if($argument == 4) {
       echo '<META http-equiv="refresh" content="0;URL=http://adloc8tor.com/ussd/service4.xml">';
  }
?>
</head>
</html>

From what I can see the curl request probably hasn't finished doings it's thing before the page does the rediection. 从我可以看到的卷曲请求可能还没有完成,这是页面重新重定向之前的事情。 Why not get a return value from the curl request and use that as a prerequisite to the if statement that follows? 为什么不从curl请求中获取返回值,并将其用作随后的if语句的前提?

ie: 即:

$status=curl_getinfo( $ch, CURLINFO_HTTP_CODE );

if( $status==200 ){
  if($argument == 1) {
       echo '<META http-equiv="refresh" content="0;URL=http://adloc8tor.com/ussd/service1.xml">';
  } else if($argument == 2) {
       echo '<META http-equiv="refresh" content="0;URL=http://adloc8tor.com/ussd/service2.xml">';
  } else if($argument == 3) {
       echo '<META http-equiv="refresh" content="0;URL=http://adloc8tor.com/ussd/service3.xml">';
  } else if($argument == 4) {
       echo '<META http-equiv="refresh" content="0;URL=http://adloc8tor.com/ussd/service4.xml">';
  }
}

From your point I can derive the following points. 根据您的观点,我可以得出以下几点。

  1. PHP programs run in sequential execution, so obviously curl execution will run in first step then redirection happens to the XML document. PHP程序在顺序执行中运行,因此显然卷曲执行将在第一步中运行,然后重定向到XML文档。

this is because due to curl execution time will be long 这是因为由于curl的执行时间会很长

  1. Make sure you are redirecting the page after the curl success 确保卷曲成功后重定向页面

     $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); if($status == 200){ if($argument == 1) { echo '<META http-equiv="refresh" content="0;URL=http://adloc8tor.com/ussd/service1.xml">'; } else if($argument == 2) { echo '<META http-equiv="refresh" content="0;URL=http://adloc8tor.com/ussd/service2.xml">'; } else if($argument == 3) { echo '<META http-equiv="refresh" content="0;URL=http://adloc8tor.com/ussd/service3.xml">'; } else if($argument == 4) { echo '<META http-equiv="refresh" content="0;URL=http://adloc8tor.com/ussd/service4.xml">'; } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM