반응형
PHP에서 cURL을 사용한 RAW POST
cURL을 사용하여 PHP에서 RAW POST를 어떻게 수행 할 수 있습니까?
인코딩없이 원시 게시물과 내 데이터가 문자열에 저장됩니다. 데이터는 다음과 같이 형식화되어야합니다.
... usual HTTP header ...
Content-Length: 1039
Content-Type: text/plain
89c5fdataasdhf kajshfd akjshfksa hfdkjsa falkjshfsa
ajshd fkjsahfd lkjsahflksahfdlkashfhsadkjfsalhfd
ajshdfhsafiahfiuwhflsf this is just data from a string
more data kjahfdhsakjfhsalkjfdhalksfd
하나의 옵션은 전송되는 전체 HTTP 헤더를 수동으로 작성하는 것이지만 최적이 아닌 것 같습니다.
어쨌든, POST를 사용하고 text / plain을 사용하고 원시 데이터를에서 보내는 curl_setopt ()에 옵션을 전달할 수 $variable
있습니까?
방금 다른 사람이 그것을 우연히 발견 할 경우를 대비하여 해결책을 찾았습니다.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://url/url/url" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, "body goes here" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result=curl_exec ($ch);
Guzzle 라이브러리를 사용한 구현 :
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
$httpClient = new Client();
$response = $httpClient->post(
'https://postman-echo.com/post',
[
RequestOptions::BODY => 'POST raw request content',
RequestOptions::HEADERS => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
]
);
echo(
$response->getBody()->getContents()
);
PHP CURL 확장 :
$curlHandler = curl_init();
curl_setopt_array($curlHandler, [
CURLOPT_URL => 'https://postman-echo.com/post',
CURLOPT_RETURNTRANSFER => true,
/**
* Specify POST method
*/
CURLOPT_POST => true,
/**
* Specify request content
*/
CURLOPT_POSTFIELDS => 'POST raw request content',
]);
$response = curl_exec($curlHandler);
curl_close($curlHandler);
echo($response);
참고 URL : https://stackoverflow.com/questions/871431/raw-post-using-curl-in-php
반응형
'IT박스' 카테고리의 다른 글
모든 것을 한 눈에 보는 방법? (0) | 2020.07.27 |
---|---|
새로운 구문 중 하나 대신 일반 오래된 Thread 객체를 사용하는 것이 더 좋은 경우가 있습니까? (0) | 2020.07.27 |
Android 용 Eclipse에서 ProGuard 활성화 (0) | 2020.07.27 |
매개 변수를 통한 캐시 버스 팅 (0) | 2020.07.27 |
명령 행 출력 억제 (0) | 2020.07.27 |