duande1985 2019-03-29 07:18
浏览 92
已采纳

一个cURL执行另一个cURL

Hey guys I'm working with PHP, cURL and JSON data.

Architecture:

 FRONT <-> MIDDLE <-> BACK

Problem location:

  MIDDLE <-> BACK

Description:

In my MIDDLE file (PHP server) I call a function which make a cURL to my BACK file (PHP server).

The issue I'm having is that it will execute one of the calls (HTTP cURL requests) but not the other, overthought the one not executing is the call first.

Can you guys guide me on the issue, how can I solve this?

What I've try:

  • I research (articles, videos) info on curl thinking that maybe I had to handle it mysql-like, in which I had to create multiple cURL connection if I needed to request different items from the same server (BACK).
  • I've implemented dummy print outs to see whats executed and came about the conclusion I describe in the description part.

Note:

Following you will find the code for MIDDLE, BACK and two images:

  • Image 1 is the code with dummy print out showing how one cURL executes and the other does not. Please noticed the text "what back gets".
  • Image 2 is how cURL is not executed.

MIDDLE FILE:

Note: Look how under case "EXAMSOLVED" I call two functions. That is related to Image 1.

If I comment the second function that is related to Image 2.

<?php

/*------------------------------------------------------------------*/

include "exam_grading_system.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);
    curl_setopt($obj, CURLOPT_RETURNTRANSFER, true);

    $ans = curl_exec($obj);

    curl_close($obj);

    return $ans;
}

//--------------------------------------------------------------------

function get_question_info($ulr, $id_question)
{
    //Set UP Request Packet
    $askDB->case = "GradingInfo";
    $askDB->id_question = $id_question;

    //Convert Packet to JSON format
    $askDB = json_encode($askDB);

    //echo $askDB;

    //Ask Back Server
    $BK_Srv_Ans = http_post_back_server($url,$askDB);

    return $BK_Srv_Ans;
}

//--------------------------------------------------------------------

/*URL TO BACK SERVER*/
$url_myserver = "https: MY URL ";

/*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 "_EXAMSOLVED_":
        $questions_Info = get_question_info($url_myserver, $data["id_question"]);
        //$indata = Grading($data, $questions_Info);
        //$back_ans = http_post_back_server($url_myserver,$indata);
        break;
    default:
        $back_ans = http_post_back_server($url_myserver,$indata);
        break;
}

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

?>

BACK FILE:

Note: Look the dummy echo statement I have at the begging of the file

<?php

/*------------------------------------------------------------------*/
include "Prof_backend_tools.php";
include "Student_backend_tools.php";
/*------------------------------------------------------------------*/


echo "what back gets: ";

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

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

/*CHECKING DATABASE CONNECTIVITY */

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

switch($indata["case"])
{
  case "_EXAMSOLVED_":
    echo store_exam_slutions($indata,DB_s());
    break;
  case "GradingInfo":
    echo "--IN BACK--";
    //echo Needed_data_for_grading($indata);
    break;
  default:
    echo "NADA";
    break;
}

?>

Image 1:

enter image description here

Image 2: enter image description here

Any guide would be great guys. Thank you.

  • 写回答

1条回答 默认 最新

  • dongshubang7816 2019-04-01 04:33
    关注

    OK,I've found what was happening!!!

    Debugging:

    • Check MIDDLE file.
    • Check the parameters in function definition get_question_info
    • Now check arguments given to function call http_post_back_server
    • There lies the problem.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog