Monday, 24 September 2018

POSTing json data with php cURL

<?php

$params = array(

    "id" => 1,

    "name" => "Pritom Kumar Mondal"

);

$param = json_encode($params);

$ch   = curl_init("http://localhost/api_server/user");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

curl_setopt($ch, CURLOPT_POSTFIELDS, $param);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(

    'Content-Type: application/json',

    'Content-Length: ' . strlen($param))

);

$response = curl_exec($ch);

print_r($response);

?>

0 comments:

Post a Comment