Home > Article > Backend Development > How to use curl to submit json format data in PHP
This article mainly shares with you how to use curl to submit numbers in json format in PHP. I hope it can help you.
$data = array("name" => "Hagrid", "age" => "36"); $data_string = json_encode($data); $ch = curl_init('http://api.local/rest/users'); 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);
Related recommendations:
PHPAPI interface output json format data sample code
The above is the detailed content of How to use curl to submit json format data in PHP. For more information, please follow other related articles on the PHP Chinese website!