PHP cURL ile JSON Data Göndermek
Bazen API’ler sizden json data göndermenizi isteyebiliyor, bu gibi durumda cURL ile post işleminde ufak birkaç eklemek yapmak gerekiyor.

İlgili kod parçacağına aşağıdan bakabilirsiniz. Ayrıca post edilen yerde alma yöntemi de biraz farklı, onuda 2. kısımda bulabilirsiniz.
$data = array("name" => "noneserver", "age" => "30");
$data_string = json_encode($data);
$ch = curl_init('http://localhost/post.php');
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);
echo $result;
isteği yaptığımız post.php’de ise json data’yı $_POST ile almaktan biraz daha farklı alıyoruz.
$posts = file_get_contents('php://input');
$jsonData = json_decode($posts, true);
print_r($jsonData);
Tepkiniz Nedir?






