perhaps? 2019-03-03 22:41 采纳率: 100%
浏览 98

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条回答 默认 最新

  • weixin_33682719 2019-03-04 00:01
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?