IT박스

PHP에서 cURL을 사용한 RAW POST

itboxs 2020. 7. 27. 07:42
반응형

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

반응형