Home >Backend Development >PHP Tutorial >How to Send Raw POST Data Using cURL in PHP?

How to Send Raw POST Data Using cURL in PHP?

Barbara Streisand
Barbara StreisandOriginal
2024-12-03 03:29:09257browse

How to Send Raw POST Data Using cURL in PHP?

Send Raw POST Data via cURL in PHP

In certain use cases, it's necessary to perform a raw POST operation using cURL in PHP. Instead of encoding, you can send unprocessed data stored in a string. The data should adhere to the following format:

<br>... usual HTTP header ...<br>Content-Length: 1039<br>Content-Type: text/plain</p>
<p>89c5fdataasdhf kajshfd akjshfksa hfdkjsa falkjshfsa<br>ajshd fkjsahfd lkjsahflksahfdlkashfhsadkjfsalhfd<br>ajshdfhsafiahfiuwhflsf this is just data from a string<br>more data kjahfdhsakjfhsalkjfdhalksfd<br>

Solution:

To perform a raw POST without manually writing the HTTP header, you can use the following cURL options:

$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);

This code initializes a cURL session, sets the target URL, enables response retrieval, specifies the POST method, supplies the raw data as POST fields, and sets the Content-Type header accordingly. Executing this code will send the raw POST data without any encoding.

The above is the detailed content of How to Send Raw POST Data Using cURL in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn