我在张贴这个问题之前看了很多,所以如果是在另一个帖子上,我很抱歉,这只是我在这里的第二个问题,如果我没有正确地表达这个问题,我很抱歉.
我创建了一个非常简单的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));
正如我所说,至少我现在看到了一个回复,而之前它返回的是一个空白页