<?php
$url = "http://localhost:8000";
$body_data = array(
"data1" => "test1",
"data2" => "test2"
);
$body = json_encode(body_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
?>
json 형태로 body를 전송하기 위해서는반드시 header에 'Content-Type: application/json'을 추가해줘야합니다.
header를 추가하지 않고 query형태(?data1=test1&data2=test2)로도 전송이 가능합니다.
반응형
'PHP' 카테고리의 다른 글
[PHP] CURL를 통해 header 전송 (0) | 2021.08.06 |
---|---|
[PHP] 제너레이터(Generator) - yield, yield from (0) | 2020.09.06 |
[PHP] @(골뱅이) 의미 (0) | 2020.09.06 |
[PHP] 연산자 우선순위 (0) | 2020.09.06 |
[PHP] 유형 연산자 - instacnof (0) | 2020.09.06 |