JSON完整循环(PHP)

JSON完整循环(PHP),php,mysql,json,apache,wampserver,Php,Mysql,Json,Apache,Wampserver,我已经简化到最简单的形式,但仍然步履蹒跚……我花了30多个小时进行研究和测试。根据所有从不显示整个圆圈超过15°的帖子,这应该是非常容易的 我想: 将查询参数(JSON格式)从Android手机发送到WAMP服务器……这可能相当于本地SQLite表的一个完整转储,因此查询字符串不会剪切它 让WAMP服务器读取JSON数据,制定SQL查询并提交到mySQL数据库 将响应打包为JSON数据(从简单的“OK”到完整的表转储) 将响应包返回到Android手机 这已经是一个功能齐全的WAMP应用程序,我

我已经简化到最简单的形式,但仍然步履蹒跚……我花了30多个小时进行研究和测试。根据所有从不显示整个圆圈超过15°的帖子,这应该是非常容易的

我想:

  • 将查询参数(JSON格式)从Android手机发送到WAMP服务器……这可能相当于本地SQLite表的一个完整转储,因此查询字符串不会剪切它
  • 让WAMP服务器读取JSON数据,制定SQL查询并提交到mySQL数据库
  • 将响应打包为JSON数据(从简单的“OK”到完整的表转储)
  • 将响应包返回到Android手机
  • 这已经是一个功能齐全的WAMP应用程序,我想集成Android access。出于这个原因,我真的希望避免使用AJAX,因为我希望保持与已有内容的一致性

    我已经将其简化为最简单的循环,并且遇到了障碍。我使用send.php将一些JSON数据发布到receive.php。此时,我只需要receive.php读取数据并将其发送回(稍微修改)send.php

    send.php正在正确读取从receive.php发送的股票JSON。我只是无法得到任何生命迹象,receive.php甚至无法识别发送给它的JSON

    请不要把我引向cURL……从我所发现的关于Android和JSON的所有信息来看,cURL是一个切线,它将把我送回一个完整的无功能状态

    Apache2.2.22,PHP5.4.3

    就像我说的,我把它简化成最简单的形式来演示一个完整的圆

    send.php:

    <?php
    $url = "http://192.168.0.102:808/networks/json/receive.php";
    $data = array(
            'param1'      => '12345',
            'param2'    => 'fghij'
    );
    $json_data = json_encode($data);
    
    $options = array(
            'http' => array(
                    'method'  => 'POST',
                    'content' => $json_data,
                    'header'=>  "Content-Type: application/json\r\n" .
                    "Accept: application/json\r\n" .
                    'Content-Length: ' . strlen($json_data) . "\r\n"
            )
    );
    
    $context  = stream_context_create( $options );
    $result = file_get_contents( $url, false, $context );
    
    $response = json_decode( $result , true);
    echo '[' . $response['param1'] . "]\n<br>";
    //THIS WORKS!  send.php displays "Initialized"
    ?>
    
    
    
    receive.php

    <?php
    $newparam = 'Initialized';
    //HERE I NEED TO read the JSON data and do something
    
    $data = array(
            'param1'      => $newparam,
            'param2'    => 'pqrst'
    );
    
    header('Content-type: application/json');
    echo json_encode($data);
    ?>
    
    <?php
    //K.I.S.S. - Retrieve the incoming JSON data, decode it and send one value 
    //back to send.php
    
    //Grab the incoming JSON data (want error correction)
    //THIS IS THE PART I WAS MISSING
    $data_from_send_php = file_get_contents('php://input');
    
    //Decode the JSON data
    $json_data = json_decode($data_from_send_php, true);
    
    //CAN DO:  read querystrings (can be used for user auth, specifying the 
    //requestor's intents, etc)
    
    //Retrieve a nugget from the JSON so it can be sent back to send.php
    $newparam = $json_data["param2"];
    
    //Prep the JSON to send back
    $data = array(
            'param1'      => $newparam,
            'param2'    => 'pqrst'
    );
    
    //Tell send.php what kind of data it is receiving
    header('Content-type: application/json');
    
    //Give send.php the JSON data
    echo json_encode($data);
    ?>
    

    正如所有不完整的解释中所述,这其实很容易……我终于完成了整个循环

    我选择简单来证明我可以周而复始,现在我已经做到了

    send.php

    <?php 
    //The URL of the page that will:
    //  1. Receive the incoming data
    //  2. Decode the data and do something with it
    //  3. Package the results into JSON
    //  4. Return the JSON to the originator
    $url = "http://192.168.0.102:808/networks/json/receive.php";
    
    //The JSON data to send to the page (above)
    $data = array(
            'param1'      => 'abcde',
            'param2'    => 'fghij'
    );
    $json_data = json_encode($data);
    
    //Prep the request to send to the web site
    $options = array(
            'http' => array(
                    'method'  => 'POST',
                    'content' => $json_data,
                    'header'=>  "Content-Type: application/json\r\n" .
                    "Accept: application/json\r\n"
            )
    );
    $context  = stream_context_create( $options );
    
    //Make the request and grab the results
    $result = file_get_contents( $url, false, $context );
    
    //Decode the results
    $response = json_decode( $result , true);
    
    //Do something with the results
    echo '[' . $response['param1'] . "]\n<br>";
    ?>
    
    
    
    receive.php

    <?php
    $newparam = 'Initialized';
    //HERE I NEED TO read the JSON data and do something
    
    $data = array(
            'param1'      => $newparam,
            'param2'    => 'pqrst'
    );
    
    header('Content-type: application/json');
    echo json_encode($data);
    ?>
    
    <?php
    //K.I.S.S. - Retrieve the incoming JSON data, decode it and send one value 
    //back to send.php
    
    //Grab the incoming JSON data (want error correction)
    //THIS IS THE PART I WAS MISSING
    $data_from_send_php = file_get_contents('php://input');
    
    //Decode the JSON data
    $json_data = json_decode($data_from_send_php, true);
    
    //CAN DO:  read querystrings (can be used for user auth, specifying the 
    //requestor's intents, etc)
    
    //Retrieve a nugget from the JSON so it can be sent back to send.php
    $newparam = $json_data["param2"];
    
    //Prep the JSON to send back
    $data = array(
            'param1'      => $newparam,
            'param2'    => 'pqrst'
    );
    
    //Tell send.php what kind of data it is receiving
    header('Content-type: application/json');
    
    //Give send.php the JSON data
    echo json_encode($data);
    ?>
    

    在receive.php内部使用
    error\u log()
    查找生命迹象。@user2147564在读取(在receive.php中)发送的json数据时尝试json\u decode($data)。isset($\u POST)返回true,但似乎不包含任何内容。如果我尝试访问$_POST['content'],从send.php发送的标题来看,这似乎是合乎逻辑的,那么这里什么都没有(错误)。我知道我需要对json进行解码($data_from_send_php),但是如何检索传入的json数据呢?在进一步测试后,receive.php以数组的形式接收$_POST,但count($_POST)=0…问题(开始)是send.phpOnly花了几百篇文章才找到它…send实际上还可以,它正确地将json数据发送到receive.php。JSON数据可通过“$data\u from\u send\u php=file\u get\u contents('php://input');"