douchuituo3032
2014-04-03 05:23
浏览 55

使用PHP中的JSON字符串发送Http post请求

Here is my code,

$url= 'http://dummyhost:8080/admin/EditSubscriber?jsonString={"sub_Id":3,"sub_Fname":"messi","sub_Lname":"lionel"}';
$data_string="";
$request = new HTTPRequest($url, HTTP_METH_POST);
$request->setRawPostData($data_string);
$request->send();    
$response = $request->getResponseBody();
$response= json_decode($response, true);

at the end of url JSON string is concatenated according to server requirement but in response there is nothing i get in response variable. What is wrong with this as when i make this request using chrome extension it shows me the result updated. And when i use the $url= "http://dummyhost:8080/admin/ViewSubsriber?jsonString={"sub_Name":"messi","sub_Password":"password"}"; i get the desired result. i've used curl as well'

i've used Curl as well like this

$ch = curl_init($url);                                                                      
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);                            
curl_close($ch);
$json_result = json_decode($result, true);

but the same result i get that is nothing

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanfei8149 2014-04-09 11:54
    已采纳

    If you have created JSON string at your own keep following things in mind:
    Space in string might result in unnatural behavior from server so for each of the varible or atleast for strings use

    urlecode(yourvariable);
    

    then chek the string online wether the JSON string is valid or not like this http://json.parser.online.fr/

    Like Brant says use

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

    for raw data instead of using the empty $data_string="";

    已采纳该答案
    打赏 评论
  • doumin1897 2014-04-03 06:11

    Your posted variable, $data_string, is empty. You are using POST and sending empty data, but then also sending a query string. It seems you are mixing GET and POST methods here. You need to actually post your JSON string in the posted data.

    If you are posting raw JSON string using application/JSON content type, the post data will need to be read from raw input like this

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

    This is because $_POST is only automatically populated by PHP for form-encoded content types.

    I would also recommend sticking with curl for such usage.

    打赏 评论

相关推荐 更多相似问题