JSON完整循环(PHP)
我已经简化到最简单的形式,但仍然步履蹒跚……我花了30多个小时进行研究和测试。根据所有从不显示整个圆圈超过15°的帖子,这应该是非常容易的 我想: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应用程序,我
<?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');"