0

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);
2
  • 2
    What does mean 'unable to send request'? What response do you see after sending the POST-request? Commented Jun 20, 2016 at 12:06
  • Actually, when I checked the error with status code, I found that the issue was due to ssl certificate (https). The code was working as expected. Now, the issue is fixed. Thanks every one for your time Commented Jun 20, 2016 at 12:55

2 Answers 2

1

maybe you can try this:

curl_setopt($ch, CURLOPT_POST, 1);

instead of:

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

Sign up to request clarification or add additional context in comments.

Comments

0
$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.