<<BACK
PHP Data Stream POST , JSON
보내는 방법
curl -H "Content-Type: application/json" -X POST -d '{"newstitle":"AAA","newstext":"BBB","newsurl":"한글"}' http://192.168.1.17/sample_01.php
받는 방법
$data = file_get_contents('php://input');
JSON 변환
$jsondata = json_decode($data);
JSON 사용 방법
echo $jsondata["newstitle"]."<\ br>";
echo $jsondata["newstext"]."<\ br>";
echo $jsondata["newsurl"]."<\ br>";
JSON 뿌리기
// 그룹에 대한 정보를 저장
header("Content-Type: application/json");
$groupData = array();
$groupData["groupName"] = "서태지와 아이들";
$groupData["debutYear"] = "1992";
$groupData["memberCount"] = "3";
// 첫 번째 코드와 동일
$member1 = array("name" => "서태지", "height" => "173cm", "weight" => "55kg");
$member2 = array("name" => "양현석", "height" => "180cm", "weight" => "70kg");
$member3 = array("name" => "이주노", "height" => "172cm", "weight" => "53kg");
$memberData = array($member1, $member2, $member3);
// JSON Array를 연관 배열로 저장 키이름은 memberData
$groupData["memberData"] = $memberData;
// JSON Array가 포함된 Object를 문자열로 변환
$output = json_encode($groupData);
// 출력
echo urldecode($output);
JSON 보내고 결과받기
$url = "https://api.openai.com/v1/chat/completions";
$model = "gpt-3.5-turbo";
$header = array(
"Authorization: Bearer ".$API_KEY,
"Content-type: application/json",
);
$params = json_encode(
array(
"messages" => $data,
"model" => $model,
"temperature" => 1,
"max_tokens" => 1500,
"top_p" => 1,
"frequency_penalty" => 0,
"presence_penalty" => 0,
"stream" => True
)
);
// create a new cURL resource
$curl = curl_init();
// set URL and other appropriate options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
// enable streaming
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_WRITEFUNCTION, function($curl, $chunk) {
// do something with the streamed data
echo $chunk;
return strlen($chunk);
});
// execute the request
curl_exec($curl);
// close cURL resource
curl_close($curl);