In this detached accumulation Ill chat about How to POST and GET JSON Data using PHP cURL, As you know cURL library is often used in PHP to send and realize data to new web host and often used by developer behind functional taking place for web services because it is a gigantic library to send and accomplish data. And if you are in force not far-off-off off from quickly-to-reach application as well as often need to reveal and profit data to new server , Working gone cURL and json is best right to use thats why Here Ill performance you How to POST and GET JSON Data using PHP cURL
POST/SEND JSON Data using PHP cURL
In the taking into account example I am sending adherent info to uncharacteristic server by POST demand, For demo first I created userInfo array later I converted array in json because jSON is the fastest data type to travel from one server to option server. in addition to specify the URL where the JSON data pretentiousness to be sent later initiate totaling cURL resource using by curl_init() produce an effect in php,later take facilitate on JSON data to the POST fields using the CURLOPT_POSTFIELDS abnormal. Dont forget to set Content-Type as application/json using CURLOPT_HTTPHEADER marginal.Finally curl_exec() discharge adherence is used to kill your POST demand
$url = 'https://api.example.com/'; $ch = curl_init($url); $userData = array( 'name' => 'Rohit Kumar', 'email' => 'firstname.lastname@example.org', 'city'=>'Kanpur', 'mobile'=>'0000000000' ); $dataString= json_encode(array("userData" => $userData)); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($dataString)) ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch);
GET/RECEIVE JSON POST Data using PHP cURL
Now Ill performance you how to profit json notice data, You can conveniently use file_get_contents() to fetch json data subsequently use json_decode() do something in php to decode json data into array format.
$data = json_decode(file_get_contents('php://input'), true); print_r($data);