user3378617
user3378617

Reputation:

Http post request with JSON String in PHP

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

Upvotes: 1

Views: 818

Answers (2)

Zahid Khan
Zahid Khan

Reputation: 1260

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

Upvotes: 1

Mike Brant
Mike Brant

Reputation: 71384

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.

Upvotes: 0

Related Questions