使用PHP读取JSON POST
在发布此问题之前,我环顾了四周,因此我很抱歉在其他问题上发表,这只是我的第二个问题,如果无法正确设置此问题的格式,我们深表歉意.
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.
我创建了一个非常简单的Web服务,该服务需要获取发布值并返回JSON编码的数组.一切工作正常,直到被告知我需要使用内容类型为application/json的表单数据来发布.从那时起,我无法从Web服务返回任何值,这绝对与我如何过滤其发布值有关.
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);
?>
基本上我知道这是由于未设置$ _POST值,但是我找不到需要放置的内容而不是$ _POST.我试过了 json_decode($ _ POST),file_get_contents("php://input")和许多其他方式,但是我在黑暗中开枪了.
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.
任何帮助将不胜感激.
谢谢,史蒂夫
感谢Michael的帮助,这是一个明确的进步,当我回应该帖子时,我现在至少有了一个回应....即使它为空
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
更新的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-
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
您有空的$_POST
.如果您的网络服务器想要查看json格式的数据,则需要读取原始输入,然后使用JSON解码对其进行解析.
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.
您需要类似的东西:
$json = file_get_contents('php://input');
$obj = json_decode($json);
另外,您使用错误的代码来测试JSON通讯...
Also you have wrong code for testing JSON-communication...
CURLOPT_POSTFIELDS
告诉curl
将您的参数编码为application/x-www-form-urlencoded
.您需要在这里使用JSON字符串.
CURLOPT_POSTFIELDS
tells curl
to encode your parameters as application/x-www-form-urlencoded
. You need JSON-string here.
更新
您用于测试页的php代码应如下所示:
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);
此外,在您的Web服务页面上,您还应删除header('Content-type: application/json');
行之一.它只能被调用一次.
Also on your web-service page you should remove one of the lines header('Content-type: application/json');
. It must be called only once.