我在张贴这个问题之前看了很多,所以如果是在另一个帖子上,我很抱歉,这只是我在这里的第二个问题,如果我没有正确地表达这个问题,我很抱歉.

我创建了一个非常简单的Web服务,它需要接受POST值并返回JSON编码的array.在我被告知需要发布内容类型为application/json的表单数据之前,一切都运行得很好.从那时起,我不能从Web服务返回任何值,这肯定与我如何过滤它们的POST值有关.

基本上,在我的本地设置中,我已经创建了一个测试页面,它执行以下操作-

$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);                                                                  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data))                                                                       
);
curl_setopt($curl, CURLOPT_URL, 'http://webservice.local/');  // Set the url path we want to call
$result = curl_exec($curl);

//see the results
$json=json_decode($result,true);
curl_close($curl);
print_r($json);

在webservice上,我有这个(我已经go 掉了一些功能)-

<?php

header('Content-type: application/json');

/* connect to the db */
$link = mysql_connect('localhost','root','root') or die('Cannot connect to the DB');
mysql_select_db('webservice',$link) or die('Cannot select the DB');

if(isset($_POST['action']) && $_POST['action'] == 'login') {
    $statusCode = array('statusCode'=>1, 'statusDescription'=>'Login Process - Fail');
    $posts[] = array('status'=>$statusCode);
    header('Content-type: application/json');
    echo json_encode($posts);

    /* disconnect from the db */
}
@mysql_close($link);

?>

基本上,我知道这是因为没有设置$_POST值,但是我找不到需要放置的内容,而不是$_POST.我试过了 JSON_DECODE($_POST)、FILE_GET_CONTENTS("php://input")和许多其他方式,但我有点蒙在鼓里.

Any help would be greatly appreciated.

Thanks, Steve

谢谢迈克尔的帮助,这是一个明确的进步.现在,当我回复帖子时,我至少得到了回复....即使它是空的

更新curl -

  $curl = curl_init();
  curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
  curl_setopt($curl, CURLOPT_URL, 'http://webservice.local/');  
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));

在数据发布到的页面上更新了php-

$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON, TRUE ); //convert JSON into array

print_r(json_encode($input));

正如我所说,至少我现在看到了一个回复,而之前它返回的是一个空白页

推荐答案

你有空的$_POST.如果web服务器希望看到json格式的数据,则需要读取原始输入,然后使用json解码对其进行解析.

You need something like that:

$json = file_get_contents('php://input');
$obj = json_decode($json);

Also you have wrong code for testing JSON-communication...

CURLOPT_POSTFIELDS tells curl to encode your parameters as application/x-www-form-urlencoded. You need JSON-string here.

UPDATE

测试页面的php代码应该如下所示:

$data_string = json_encode($data);

$ch = curl_init('http://webservice.local/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
);

$result = curl_exec($ch);
$result = json_decode($result);
var_dump($result);

Also on your web-service page you should remove one of the lines header('Content-type: application/json');. It must be called only once.

Json相关问答推荐

由于无效的UTF-8开始字节0xa0,JSON被拒绝,但编码似乎有效

如何使用GoFr返回XML响应?

如何创建生成两个不同对象的JSON数组的SQL查询?

如何用JQ更改空/布尔/数字的 colored颜色 ?

当并非所有子对象都有 Select 器字段时 Select

Python 将 struct 化文本转换和筛选为对象

如何使用jq按键 Select 并获取整个json输出来更改json中的多个值

Jolt 不打印任何东西

如何使用 SQL Server 将 json 存储为字符串的列分解/规范化为行和列?

在 Bash 中访问 JSON 对象 - 关联数组/列表/另一个模型

Newtonsoft Json 将值 {null} 转换为类型System.Int32时出错

在 JSON 反序列化期间没有为System.String类型定义无参数构造函数

在 Postgres 中向 JSON 对象添加元素

字符串的 Gson 数组到 JsonArray

在 Jersey 服务中使用 JSON 对象

Spring MVC:不反序列化 JSON 请求正文

如何使用 SwiftyJSON 将字符串转换为 JSON

有什么方法可以在 elasticsearch 服务器中导入 json 文件(包含 100 个文档).?

如何使用 Serde 反序列化带有自定义函数的可选字段?

杰克逊在通用列表中读取 json