1

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 Answers2

1

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="";

Zahid Khan
  • 1,250
  • 3
  • 15
  • 31
  • exactly the same issue with my Json i've cheked them online but is well structured, but now used urlencode with the varibles and works fine. thanks buddy –  Apr 09 '14 at 11:56
0

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.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • @X-Man That is why you are probably best just using POST. If you insist on using GET, then you will need to urlencode the JSON string that you pass in the query string. The cURL approach you show is basically almost there, you just need to make sure `$data_string` contains you JSON-serialized string and you need to read PHP raw input as noted above in my answer to read the JSON string into a variable in PHP. – Mike Brant Apr 04 '14 at 15:59