php – Curl generating request body

Question:

Hello ! Can you please tell me if you can somehow see what POST request curl generates by the passed parameters?

Answer:

No, unfortunately, the curl extension does not have such a native feature. But you can send a request somewhere to a program under your control and see what exactly came. For example, the simplest thing is to open a port via nc :

nc -l -p 12345

Then, in your code, replace the URL with this local address:

$ch = curl_init('http://127.0.0.1:12345');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => ['foo' => 1],
]);
$response = curl_exec($ch);

Running this code on the console with nc will show the exact request sent:

melkij@melkij:~$ nc -l -p 12345
POST / HTTP/1.1
Host: 127.0.0.1:12345
Accept: */*
Content-Length: 139
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------05be5d432625f809

--------------------------05be5d432625f809
Content-Disposition: form-data; name="foo"

1
--------------------------05be5d432625f809--
Scroll to Top