要将json从android发布到php,我使用了Volley库StringRequest对象.
StringRequest sr = new StringRequest(Request.Method.POST,url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// some code
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//some code
}
}){
@Override
protected Map<String,String> getParams(){
Map<String, String> params = new HashMap<String, String>();
ArrayList<Command> commands = MyApplication.readFromPreferences(getActivity(), Constants.COMMAND);
String jsonCommands = new Gson().toJson(commands);
params.put("commands", jsonCommands);
return params;
}
};
Run Code Online (Sandbox Code Playgroud)
为了捕获php中的数据并验证它是否被正确发送,我使用了这个
echo $_POST["commands"];
Run Code Online (Sandbox Code Playgroud)
输出:
[{\"product\":{\"category_id\":1,\"created_at\":\"2015-06-13 17:49:58\",\"description\":\"CF77 COIN FINDER\",\"url_image\":\"IMG_76ECDC-707E7E-70AC81-0A1248-4675F3-F0F783.jpg\",\"name\":\"CF77 COIN FINDER\",\"pid\":12,\"price\":500.0},\"product_quantity\":3},{\"product\":{\"category_id\":1,\"created_at\":\"2015-06-13 17:49:58\",\"description\":\"JEOSONAR 3D DUAL SYSTEM\",\"url_image\":\"IMG_2D9DF0-2EB7E9-ED26C0-2C833B-B6A5C5-5C7C02.jpg\",\"name\":\"JEOSONAR 3D DUAL SYSTEM\",\"pid\":15,\"price\":500.0},\"product_quantity\":1},{\"product\":{\"category_id\":1,\"created_at\":\"2015-06-13 17:49:58\",\"description\":\"MAKRO …Run Code Online (Sandbox Code Playgroud) I am using react.js, axios, and PHP to post data to MySQL database
This is my react.js code
sendData(){
var data = new FormData();
data.append('name', 'jessie');
data.append('time', '12:00');
data.append('food', 'milk');
data.append('nutrition', 'vitaminA');
axios.post(
'./sendData.php',{
data: data
})
.then(response => {
console.log(response)
console.log(response.data)
this.filter = response.data
})
.catch(e => {
this.errors.push(e)
})
}
Run Code Online (Sandbox Code Playgroud)
这是我的PHP代码
<?php
$servername = "127.0.0.1";
$username = "root";
$password = "";
$database = "mydb";
$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何在PHP中获取POST的正文?
我收到一个包含JSON的POST,问题是当我收到$ _POST为空时.为了测试我何时收到POST,我创建了一个包含$ _POST,$ _GET和$ _REQUEST的文件,它们都是空的.
发送请求的客户端正在执行以下操作:
$itemJson = '{
"id": 00,
"value": "ok"
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: '. strlen($itemJson))
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $itemJson);
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
说实话,我不明白我是如何得到这些数据的,因为没有参数设置.
有任何想法吗?
我有这个cURL函数将json发送到REST API:
$url = "https://server.com/api.php";
$fields = array("method" => "mymethod", "email" => "myemail");
$result = sendTrigger($url, $fields);
function sendTrigger($url, $fields){
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=UTF-8"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curlResult["msg"] = curl_exec($ch);
$curlResult["http"] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $curlResult;
}
Run Code Online (Sandbox Code Playgroud)
在服务器上,我有这个代码:
$data = json_decode($_REQUEST);
var_dump($data);
exit();
Run Code Online (Sandbox Code Playgroud)
当我执行cURL命令时,它返回我:
Warning: json_decode() expects parameter 1 to be string, array given in
Run Code Online (Sandbox Code Playgroud)
怎么样?
谢谢.
假设我有下表:
<form action="bla.php" method=post>
<table class="pv-data">
<tr>
<td><input type="text" name="id" size="2" value=1 /></td>
<td><input type="text" name="longitude" size="7"/></td>
<td><input type="text" name="latitude" size="7"/></td>
</tr>
<tr>
<td><input type="text" name="id" size="2" value=2 /></td>
<td><input type="text" name="longitude" size="7"/></td>
<td><input type="text" name="latitude" size="7"/></td>
</tr>
</table>
<input type="submit" name="submit" value="SUBMIT">
</form>
Run Code Online (Sandbox Code Playgroud)
我想将此表中的值分配给php变量,所以在 bla.php
如果我$id = $_REQUEST['id'];只使用 了最后一个值,因为有两个具有相同名称的输入标记.
那么我可以在php中请求这些具有相同名称的标签的2个值吗?
ps不要告诉我更改输入标签的名称,因为表格比这更复杂,并且它有动态添加行:我的真实代码是:http://jsfiddle.net/CchES/9/
我找不到解决这个访问通过AJAX传递给我的PHP脚本的变量的简单问题的解决方案.我甚至尝试过isset($ _ POST),但仍然无法找到用户名和密码变量.
这是AJAX调用:
var u = $("#username", form).val();
var p = $("#password", form).val();
//testing
console.log('Username: '+u); // 'John'
console.log('Password: '+p); // 'test'
if(u !== '' && p!=='') {
$.ajax({url: 'http://www.domain.net/php/user-login.php',
data: {username:u,password:p},
type: 'post',
async: true,
dataType:'json',
beforeSend: function() {
// This callback function will trigger before data is sent
$.mobile.loading('show'); // This will show ajax spinner
},
complete: function() {
// This callback function will trigger on data sent/received complete
$.mobile.loading('hide'); // This will hide ajax spinner
}, …Run Code Online (Sandbox Code Playgroud) 我正在使用Retrofit Lib.
我的代码是
public class TestPostData {
final String username;
final String password;
TestPostData(String username, String password) {
this.username = username;
this.password = password;
}
}
Run Code Online (Sandbox Code Playgroud)
和界面是
interface Test {
@POST("/post.php")
void testMethod(@Body TestPostData postbody,@Query("qName") String qName,Callback<Response> callback);
}
Run Code Online (Sandbox Code Playgroud)
休息适配器
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(TEST_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
Run Code Online (Sandbox Code Playgroud)
并称之为
testObj.testMethod(new TestPostData("myusername", "mypassword"),"myname",new Callback<Response>() { ..............
Run Code Online (Sandbox Code Playgroud)
在服务器端,我得到$ _POST数组为空.如何在服务器端获取用户名和密码值.我得到了名字的价值.
我正在使用WebClient.UploadData()方法 (C#) 向我的网络服务器发送 POST 请求。发送到我的网络服务器的数据包如下所示:
POST / HTTP/1.1
Host: {ip}
Content-Length: {length}
Expect: 100-continue
Connection: Keep-Alive
{buffer_content}
Run Code Online (Sandbox Code Playgroud)
由于 {buffer_content} 未在 $_POST 数组中分配,我有以下问题...
问题:如何用 PHP 读取 {buffer_content}?
我偶然发现了file_get_contents('php://input'),但我不确定是否建议这样做。
我正在尝试通过AJAX调用在php文件中设置会话变量。但是我收到一个未定义的索引错误。
这是jQuery代码:
$('.selectcpno li').click(function(){
//Get the value
var value = $(this).data("value");
//Put the retrieved value into the hidden input
$('input[name=cpnoselected]').val(value);
$.ajax({
type: "post",
url: "../web/cpnoselected.php",
dataType: "text",
data:"{'cpno':'" +value+ "'}",
success: function( data ){
alert("hellosuccess");
document.getElementById("cpno").innerHTML=data;
},
error: function( jqXhr, textStatus, errorThrown ){
alert(value);
console.log( errorThrown );
console.log( jqXhr );
console.log(textStatus);
}
});
});
Run Code Online (Sandbox Code Playgroud)
这就是我在php文件中的内容:
<?php
include("../config/config.php");
include("../inc/functions.php");
$cpnoselected= $_POST['cpno'];
$_SESSION['cpno']=$cpnoselected;
echo $cpnoselected;
?>
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我得到未定义的索引:C:/...../ cpnoselected.php中的cpno
请协助解决
嗨,我在表单中有许多复选框
<p>Select the modules you take:<br/>
Business <input type="checkbox" name="modules" value="Business"/><br />
Accounting <input type="checkbox" name="modules" value="Accounting"/><br />
Marketing <input type="checkbox" name="modules" value="Marketing" /><br />
</p>
Run Code Online (Sandbox Code Playgroud)
我有一个响应页面,期望用户选择多个答案,那么我将如何使用foreach循环?我尝试过以下但没有希望
foreach($modules as $selected){
print "The modules were ".$modules;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢