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
Release: 2024-12-03 03:29:09
Original
265 people have browsed it

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);
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template