I looked around a lot before posting this question so my apologies if it is on another post and this is only my second quesiton on here so apologies if I don't format this question correctly.

I have a really simple web service that I have created that needs to take post values and return a JSON encoded array. That all worked fine until I was told I would need to post the form data with a content-type of application/json. Since then I cannot return any values from the web service and it is definitely something to do with how I am filtering their post values.

Basically in my local setup I have created a test page that does the following -

$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);

On the webservice I have this (I have stripped out some of the functions) -

<?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);

?>

Basically I know that it is due to the $_POST values not being set but I can't find what I need to put instead of the $_POST. I tried json_decode($_POST), file_get_contents("php://input") and a number of other ways but I was shooting in the dark a bit.

Any help would be greatly appreciated.

Thanks, Steve

Thanks Michael for the help, that was a definite step forward I now have at least got a repsonse when I echo the post....even if it is null

updated 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));

updated php on the page that the data is posted to -

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

print_r(json_encode($input));

As I say at least I see a response now wheras prior it was returning a blank page

推荐答案

You have empty $_POST. If your web-server wants see data in json-format you need to read the raw input and then parse it with JSON decode.

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

Your php code for test page should be like that:

$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相关问答推荐

JSON:将项';S键/名称移动到属性中,并使用JQ将其转换为数组

将数组中的值作为键连接到另一个数组中的值(Jolt)

Jolt变换将字段移动到数组的每个元素中

在Zig中解析JSON失败

PostgreSQL:删除 JSONB 数组中每个元素的特定键

在 PostgreSQL 中 Select 分层 JSON 作为表

添加到数组时出错:找不到Add的重载和参数计数:1

阅读 JSON 正文 Firebase 云函数

使用 jq 将键值行转换为 json

如何使用 LINQ 在 C# 中重构对象?

将 YAML 文件转换为 Python JSON 对象

现代浏览器一次可以处理多少个 HTML 元素?

JSON.NET 中特定对象的自定义转换

可以通过 POST 使用 EventSource 传递参数的服务器发送事件 (SSE)

Json.Net:用于自定义命名的 JsonSerializer-Attribute

我应该如何处理 JSON 中的 HATEOAS 链接和引用?

JSON Schema:验证数字或空值

AJAX 将 JavaScript 字符串数组发布到 JsonResult 作为 List 总是返回 Null?

如何将 mysqli 结果转换为 JSON?

使用 jQuery 和 JSON 填充表单?