使用PHP讀取JSON帖子在發(fā)布這個問題之前,我看了很多遍,所以我很抱歉,如果它在另一個帖子上,這只是我在這里的第二個問題,所以如果我沒有正確地設(shè)置這個問題的格式,那么很抱歉。我已經(jīng)創(chuàng)建了一個非常簡單的Web服務(wù),需要獲取POST值并返回一個JSON編碼數(shù)組。這一切都很好,直到我被告知我需要用一個內(nèi)容類型的應(yīng)用程序/json發(fā)布表單數(shù)據(jù)。從那時(shí)起,我不能從Web服務(wù)返回任何值,這肯定與我如何過濾他們的POST值有關(guān)?;旧?,在我的本地設(shè)置中,我創(chuàng)建了一個測試頁面,它執(zhí)行以下操作-$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上我有這個(我已經(jīng)刪除了一些功能)-<?phpheader('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);?>基本上,我知道這是由于沒有設(shè)置$_post值,但是我找不到我需要放的東西,而不是$_post。我嘗試了json_decode($_post)、file_get_content(“php:/input”)和許多其他方式,但是我在黑暗中拍攝了一些東西。任何幫助都將不勝感激。
3 回答

撒科打諢
TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個贊
$post_string= 'json_param=' . json_encode($data);//open connection$ch = curl_init();//set the url, number of POST vars, POST datacu rl_setopt($ch,CURLOPT_POST, 1);curl_setopt($ch,CURLOPT_POSTFIELDS, $post_string);curl_setopt($curl, CURLOPT_URL, ' .local/'); // Set the url path we want to call//execute post$result = curl_exec($curl);//see the results$json=json_decode($result,tr ue);curl_close($curl);print_r($json);
$json_string = $_POST['json_param'];$obj = json_decode($json_string);
- 3 回答
- 0 關(guān)注
- 553 瀏覽
添加回答
舉報(bào)
0/150
提交
取消