我正在使用curl对API发出http请求:
$data = '{"id": 1}';
$ch = curl_init('https://localhost:8888/products');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
$products = json_decode($result, true);
这段代码有效,但我正在重构我的代码,我想知道是否有任何库可以更清晰地制作相同的代码。
答案 0 :(得分:0)