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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助