简体   繁体   中英

How to make POST request with JSON data from PHP to Java?

I have an application run on PHP. Now, I got a requirement where I have to send a API request (POST method) to a server which runs on JAVA including a json data. I tried to use curl method of php but was not able to send the request. I tried the below code for the same.

$data = array("user_id"=>"6cdedfcc-ff55-449f-8362-af3ae0e04928");
$data_string = json_encode($data);                                                                               

$ch = curl_init('https://java-api-url/api');                                                                      
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);
print_r($result);

maybe you can try this:

curl_setopt($ch, CURLOPT_POST, 1);

instead of:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

$data = NULL;

$data['user_id'] = "6cdedfcc-ff55-449f-8362-af3ae0e04928";

$data_string = json_encode($data);                                                                               

$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL,'https://java-api-url/api');                                                     
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);



print_r($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM