PHP和cURL —奇怪的输出

I'm mainly playing with PHP and cURL (the code includes some AJAX and HTML as well).

Architecture:

Front <--> Middle <--> Back <-->MySQL

Description:

  1. In my front section I'm creating an AJAX object and doing a POST request with some data (JSON format) obtain from a Form in the HTML.
  2. Data is sent to my middle server (PHP file). This file receives it and json_decodes it. Determines witch switch case to use and sends it to the back server using cURL.
  3. Back server (PHP file) gets data from POST request and it json_decodes the data. It then proceeds to create MySQL connection checks if passwords match. If the passwords match it echos back a string saying "GRATNED".
  4. Data is passed back to middle server and then to front section where AJAX receives it and displays the string.

So...

All of this works perfect. However, for some reason my data contains a 1 at the end. which messes up my Regular Expression in my JS file.

Can you guys please let me know what option do cURL (if that is the case) do I have to modify or what is it that I'm doing that I get that one(1) and how to remove it.

Thank you guys.

Attached is my code & and images of output...

Front

function whenSubmitt()
{
  //Get the data that I want to pass
  //JS Object
  var parameters = {"case":"login",
  "username":document.getElementById("username").value,
  "password":document.getElementById("password").value
  };

  //Make into JSON object
  parameters = JSON.stringify(parameters);

  //Create AJAX object
  var xobj = new XMLHttpRequest();
  var method = "POST";
  var url = "./front.php";

  //Open Connection
  xobj.open(method,url,true);
  xobj.setRequestHeader("content-type", "application/x-www-form-urlencoded");

  //When Submit button is pressed
  xobj.onreadystatechange = function()
  {
    if (xobj.readyState == 4 && xobj.status == 200)
    {
      var respuestas = xobj.responseText;
      document.getElementById("msrv_answer").innerHTML = respuestas;
      //window.location.replace(respuestas[0]); //REDIRECTS TO NEW PAGE
    }
  };


  xobj.send(parameters);
}

Front PHP

<?php

function contact_middle_man($parameters)
{
 $url = "https://myurl/middle/middle.php";

 $obj = curl_init();
 curl_setopt($obj, CURLOPT_URL, $url);
 curl_setopt($obj, CURLOPT_POST, strlen($parameters));
 curl_setopt($obj, CURLOPT_POSTFIELDS, $parameters);
 curl_setopt($obj, CURLOPT_RETURNTRANSFER, true); //ALLOWS TO GET ANSWER BACK IN STRING FORMAT, AND DOES NOT OUTPUT ANSWER DIRECTLY.

 $ans = curl_exec($obj);

 curl_close($obj);

 return $ans;
}


/*RECEIVE DATA FROM WEB INTERFACE, USER*/
$indata = file_get_contents("php://input");

/*CONTACT MIDDLE MAN, USE CURL*/
$middle_answ = contact_middle_man($indata);

echo $middle_answ;
?>

Middle PHP

<?php

function http_post_back_server($url, $data)
{
    $obj = curl_init();

    curl_setopt($obj, CURLOPT_URL, $url);
    curl_setopt($obj, CURLOPT_POST, strlen($data));
    curl_setopt($obj, CURLOPT_POSTFIELDS, $data);

    $ans = curl_exec($obj);

    curl_close($obj);

    return $ans;
}

/*URL TO BACK SERVER*/
$url_myserver = "https://myurl/loginquery_v2_.php";

/*GLOBAL VARS*/
$back_ans ="";

/*RECEIVE DATA FROM POST REQUEST*/
$indata = file_get_contents("php://input");
$data = json_decode($indata, true);


/*MAKE REQUEST TO SERVERS*/
switch($data["case"]){
    case "login":
        $back_ans = http_post_back_server($url_myserver,$indata);
        break;
    default:
        $back_ans="NADA";
        break;
}

/*ANSWER BACK TO FRON END*/
echo $back_ans;

?>

Back PHP

<?php

/*RECEIVING DATA FROM POST REQUEST */
$indata = file_get_contents("php://input");

/*DATA TO JSON OBJ*/
$indata = json_decode($indata, true);


/*CONNECTION TO DATABASE */
$conn=mysqli_connect(myusername, mypassword);

/*CHECKING DATABASE CONNECTIVITY */

if(mysqli_connect_error())
{ echo "Connection Error: ".mysqli_connect_error; }

/*GOOD CONNECTION ... CONTINUE */

$uname = $indata["username"];

$query="SELECT * FROM alpha WHERE username ='".$indata["username"]."'";

$db_output = mysqli_query($conn,$query);

/* CHECK QUERY RESULT */
if($db_output)
{

 /* FETCH RESULTS */
 while($result = mysqli_fetch_assoc($db_output))
 {

  /* COMPARE STORE PWD VS RECEIVED PWD */
  if($result["password"] == $indata["password"])
  {
    /*JSON OBJECT*/
    echo "ACCESS GRANTED";
  }

  /* PASSWORDS DOES NOT MATCH */
  else
  {
    /*JSON OBJECT*/
    echo "ACCESS DENY";
  }

 }
}

/*CLOSE DATABASE CONNECITON */

mysqli_close($conn);

?>

PAGE WITH OUTPUT

enter image description here

Thank you guys.

1个回答


之所以发生这种情况,是因为在您的中间PHP </ strong>中,您缺少curl调用中的 CURLOPT_RETURNTRANSFER </ code>选项。 结果,为 $ ans </ code>分配了值 true </ code>(因为curl调用成功)和curl调用的输出( ACCESS GRANTED </ code> )被回显到 Middle PHP </ strong>的输出中,然后是 $ back_ans </ code>,当被回显产生 1 </ code时,它为true >在输出中。 因此,返回到 Front PHP </ strong>的字符串是 ACCESS GRANTED1 </ code>。 您可以通过将其添加到 Middle PHP </ strong>:</ p>来解决此问题。

  curl_setopt($ obj,CURLOPT_RETURNTRANSFER,true);
</ code> </ pre>

然后将为 $ ans </ code>分配值 ACCESS GRANTED </ code>而不是 true </ code>,您的输出将达到预期的水平。</ p>
     </ div>

展开原文

原文

This is occurring because in your Middle PHP, you are missing the CURLOPT_RETURNTRANSFER option in your curl call. As a result, $ans is assigned the value true (because the curl call is successful) and the output from the curl call (ACCESS GRANTED) is echo'ed into the output from Middle PHP, followed by $back_ans, which being true, when it is echo'ed produces a 1 in the output. Thus the string returned to Front PHP is ACCESS GRANTED1. You can fix this by adding this to Middle PHP:

curl_setopt($obj, CURLOPT_RETURNTRANSFER, true);

Then $ans will be assigned the value ACCESS GRANTED instead of true and your output will be as expected.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问