Search code examples
phpjqueryhtmlcurlmeta

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.

The curl is working beautifully, but when we comment out the if statement that handles the redirection to an XML document. The redirect occurs and the Curl request is not executed. We need to understand why this is happening, when we remove the Redirects it runs the curl request. When the Redirects are in place the curl request is not executed.

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.

<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>

Solution

  • 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?

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