Home  >  Article  >  Backend Development  >  How to use curl to submit json format data in PHP

How to use curl to submit json format data in PHP

小云云
小云云Original
2018-03-27 16:46:563151browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn