Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Docker
- 로드밸런서
- 스프링 부트
- Kafka
- 스프링
- 클라우드
- 백준
- 카프카
- 자료구조
- Spring Data JPA
- 인천여행
- gcp
- 알고리즘
- DFS
- 백트래킹
- Spring
- 클라우드 컴퓨팅
- Elasticsearch
- Spring Boot
- 프로그래밍문제
- 오일러프로젝트
- Apache Kafka
- VPC
- aws
- JPA
- springboot
- 쿠버네티스
- 월미도
- 코드업
- 스프링부트
Archives
- Today
- Total
GW LABS
file_get_contents POST 전송 보내기 본문
PHP 구버전 혹은 curl 모듈이 설치되어 있지 않은 개발환경에서 POST 전송을 구현하려면 까다로운 작업을 거쳐야한다. HTTP header 및 body 정보를 직접 문자열로 만들어줘야 하기 때문이다. 매번 POST 전송을 위한 정보를 찾아가며 개발하는 것도 시간이 소요되는 일이기 때문에 여기에 소스를 정리한다. 파일의 경우에는 보내는 파일의 종류에 따라 meme type을 변경해주면 된다.
<?php
define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true));
define('FORM_FIELD', 'img[]');
function getFormDataString($name, $value) {
$formDataString = "--".MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"$name\"\r\n\r\n".
"$value\r\n";
return $formDataString;
}
function getStreamContext($header, $content) {
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $header,
'content' => $content,
),
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
)
));
return $context;
}
function createContext($filename) {
$file_contents = file_get_contents($filename);
$header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY;
// 이미지 파일 세팅
// 보내는 파일 정보에 따라 meme 정보를 변경하면 된다.
$content = "--".MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"".FORM_FIELD."\"; filename=\"".basename($filename)."\"\r\n".
"Content-Type: image/jpeg\r\n\r\n".
$file_contents."\r\n";
// Form 정보 세팅
$content .= getFormDataString("Form필드이름", "1234");
$content .= "--".MULTIPART_BOUNDARY."--\r\n";
$context = getStreamContext($header, $content);
return $context;
}
$context = createContext($filepath);
$result = file_get_contents("https://POST전송대상URL", false, $context);
'Programming > Php' 카테고리의 다른 글
PHP 클라이언트 IP 알아내기 (0) | 2019.12.10 |
---|
Comments