0

I want to send a data using curl and return some data

I'am using the following code

    $data = array('type' => 'active');     
    $result = ''; 
    $url = 'https://example.com/'.$function_name;  
    $send_data = json_encode($data, TRUE);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 80);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $send_data);
    $result = curl_exec($ch);       
    curl_close($ch);
    print_r($result);exit;

On the receiving end returning the POST data

print_r($_POST);exit;

But I'am getting data in different structure as below

(
    [{"type":"active"}] => 
)

I think due to this $_POST['type'] is not getting in the receiving side

Akhil K A
  • 29
  • 3
  • Have a look at https://stackoverflow.com/a/36696768/1213708, you probably need to set the Content-Type in the headers. – Nigel Ren Jun 05 '20 at 06:25
  • As a general idea: if you want to avoid all such stuff, where you write all these cURL calls and struggle with different options, then use a library like Guzzle which hides all these nasty details from you – Nico Haase Jun 05 '20 at 06:35

0 Answers0