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

2
  • Have a look at stackoverflow.com/a/36696768/1213708, you probably need to set the Content-Type in the headers. Commented Jun 5, 2020 at 6: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 Commented Jun 5, 2020 at 6:35

0

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.