If you need to POST json to a url with CURL and PHP, in this case making a POST to https://www.example.com this sample code should help you.

<?php				
$json_data = array(
    'field1'=>'data1',
    'field2' => 'data2');
$url = 'https://www.example.com';
$json_string = json_encode($json_data);
//curl php library
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_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);
?>

Leave a Reply

Your email address will not be published. Required fields are marked *