【问题标题】:php redirect before curl completes executioncurl完成执行之前的php重定向
【发布时间】:2015-05-07 12:08:03
【问题描述】:

所以我们有一个系统可以改变您在显示器上看到的内容。此代码旨在将作为“参数”的命令发送到该 JQuery 页面。

curl 运行良好,但是当我们注释掉处理重定向到 XML 文档的 if 语句时。发生重定向并且不执行 Curl 请求。我们需要了解为什么会发生这种情况,当我们删除重定向时,它会运行 curl 请求。当重定向到位时,不会执行 curl 请求。

请帮助找出导致此问题的原因和解决问题的方法,我们必须保留重定向,但如果有更好的方法来运行 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>

【问题讨论】:

  • 你的问题不清楚。

标签: php jquery html curl meta


【解决方案1】:

据我所知,curl 请求可能还没有完成,这就是页面进行重定向之前的事情。为什么不从 curl 请求中获取一个返回值并将其用作后面 if 语句的先决条件?

即:

$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">';
  }
}

【讨论】:

  • 好的,所以我将您的建议添加到文件中。运行编辑时它仍然做同样的事情,进入状态 if 语句并运行重定向而不完成 Curl 执行
  • 您期待 curl 请求的结果吗?该 curl 请求是否会导致一些可被监控的操作并返回一个返回值?
  • Curl 请求没有返回。它只是将信息推送/发布到预设的 Jquery html 文件,该文件可以完美运行。但是这个需要的文件在Curl可以发布到Jquery文档之前一直重定向。
  • 我在 META 重定向上放了 3 秒时间,现在它给了足够的时间来发帖,我也在使用您的状态检查来提供帮助。谢谢你的文章。 @RamRaider
【解决方案2】:

从你的观点我可以得出以下几点。

  1. PHP 程序按顺序执行,因此显然 curl 执行将在第一步中运行,然后重定向到 XML 文档。

这是因为curl的执行时间会很长

  1. 确保在 curl 成功后重定向页面

    $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">';
        }
    }
    

【讨论】:

  • 感谢您的回复,所以是的,我把它放在代码中,它仍然跳过发布/执行到 Jquery 页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 2016-10-09
  • 2012-06-26
  • 2019-02-22
  • 1970-01-01
  • 1970-01-01
  • 2016-10-07
相关资源
最近更新 更多