Php Curl — how to find Content Lenth of request sent?

Send the file 1test.jpg (85,210 bytes) to the server using PUT. I need to know the exact Content Length, which gets the server. The problem is that the server does not report what was the Content Length of my request, but the next time it necessarily requires.
I tried to specify the Content Length to the size of file being sent. Close, but no. In the Content Length at least added the name of the file and something else don't know what.
To learn from what formed the Content Length I researched curl_getinfo($this->_ch), but anything useful did not find.
cErViI.jpg
CURLOPT_VERBOSE is also nothing useful.
Then tried to send a request to my test page and saw that in the HEADERS is information about Content Length
wjCogq.jpg
And it's the correct Content Length, which service for which I make the customer agrees. But I can not understand how it is formed exactly or where to find it
And as I wrote above, the file size + length of his name + ±200 characters.
The crux of the matter. If Curl sends the header Content Length of the request to the server, where I can find it before shipping?
April 7th 20 at 15:20
4 answers
April 7th 20 at 15:22
Solution
Razobralsya. To send files to the server using PUT need as in the code below
$fo = fopen ($filename, 'r');
 curl_setopt($this->_ch, CURLOPT_PUT, true);
 curl_setopt($this->_ch, CURLOPT_INFILE, $fo);
 curl_setopt($this->_ch, CURLOPT_INFILESIZE, filesize($filename));

Not correct:
$post_data = ["file" => curl_file_create($file_name)];
curl_setopt($this->_ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $post_data);


In this case, it is transmitted in plain text to be wrapped with additional information:
--------------------------5cff5c02b52f5c00
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: application/octet-stream
YOUR DATA
--------------------------5cff5c02b52f5c00--
April 7th 20 at 15:24
consider how many characters the string length plus the length of the file (filesize('A1.jpg')) add in the header Content Length
What characters count? I already wrote that in the Content Length is file size + length of his name + a variable number is not unknown characters. - Edyth.Blick8 commented on April 7th 20 at 15:27
all that passed in the request - Maxwell.Nad commented on April 7th 20 at 15:30
@Carolanne_Connelly, If you add up everything that goes in the headers, doesn't add up. Here's an example file name in the header is not transmitted but is counted by the server in the Content Length. And the query only file. - Edyth.Blick8 commented on April 7th 20 at 15:33
schaz test, just pass the file length and all. then match - Maxwell.Nad commented on April 7th 20 at 15:36
@Carolanne_Connelly, I do not roll - Edyth.Blick8 commented on April 7th 20 at 15:39
came code how to send request and response - Maxwell.Nad commented on April 7th 20 at 15:42
Client. Send the file to the server via PUT
<?php

$post_data = ["file" => curl_file_create($file_name)];
$this->_ch = curl_init();
curl_setopt($this->_ch, CURLOPT_URL, $url);
curl_setopt(
$this->_ch,
CURLOPT_USERAGENT,
'Mozilla/5.0'
);
curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->_ch, THIS, true);
curl_setopt($this->_ch, CURLOPT_COOKIEFILE, $this->_cookie);
curl_setopt($this->_ch, CURLOPT_COOKIEJAR, $this->_cookie);
curl_setopt($this->_ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $post_data);
$result_ = curl_exec($this->_ch);


============================================
Code test page to see the Content Length
the <pre><?php
echo "<b>SERVER</b>";
print_r($_SERVER);
echo '<hr>';
echo "<b>HEADERS</b>";
print_r(apache_request_headers());
echo '<hr>';
echo "<b>COOKIE</b>";
print_r($_COOKIE);
echo '<hr>';
echo "<b>REQUEST</b>";
print_r($_REQUEST);
?>
</pre>

============================
Answer test page about this
HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Server: nginx
Date: Sun, 16 Feb 2020 15:33:02 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 1666
Connection: keep-alive
Keep-Alive: timeout=60
Vary: Accept-Encoding

the <pre><b>SERVER</b>Array
(
 [HTTP_HOST] => example.net
 [HTTP_X_FORWARDED_FOR] => 91.241.220.200
 [HTTP_CONNECTION] => close
 [CONTENT_LENGTH] => 85403
 [HTTP_USER_AGENT] => Mozilla/5.0
 [HTTP_ACCEPT] => */*
 [HTTP_X_REQUESTED_WITH] => XMLHttpRequest
 [CONTENT_TYPE] => multipart/form-data; boundary=------------------------644b415d544c9b32
 [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
 [SERVER_SIGNATURE] => 
 [SERVER_SOFTWARE] => Apache/2.4.18 (Ubuntu) mod_fcgid/2.3.9 OpenSSL/1.0.2 g
 [SERVER_NAME] => example.net
 [SERVER_ADDR] => 95.216.144.70
 [SERVER_PORT] => 80
 [REMOTE_ADDR] => 91.241.220.200
 [DOCUMENT_ROOT] => /home/example/web/example.net/public_html
 [REQUEST_SCHEME] => http
 [CONTEXT_PREFIX] => 
 [CONTEXT_DOCUMENT_ROOT] => /home/example/web/example.net/public_html
 [SERVER_ADMIN] => info@example.net
 [SCRIPT_FILENAME] => /home/example/web/example.net/public_html/ip.php
 [REMOTE_PORT] => 58106
 [GATEWAY_INTERFACE] => CGI/1.1
 [SERVER_PROTOCOL] => HTTP/1.0
 [REQUEST_METHOD] => PUT
 [QUERY_STRING] => 
 [REQUEST_URI] => /ip.php
 [SCRIPT_NAME] => /ip.php
 [PHP_SELF] => /ip.php
 [REQUEST_TIME_FLOAT] => 1581867182.959
 [REQUEST_TIME] => 1581867182
)
<hr><b>HEADERS</b>Array
(
 [Host] => example.net
 [X-Forwarded-For] => 91.241.220.200
 [Connection] => close
 [Content-Length] => 85403
 [User-Agent] => Mozilla/5.0
 [Accept] => */*
 [X-Requested-With] => XMLHttpRequest
 [Content-Type] => multipart/form-data; boundary=------------------------644b415d544c9b32
)
<hr><b>COOKIE</b>Array
(
)
<hr><b>REQUEST</b>Array
(
)
</pre>
- Edyth.Blick8 commented on April 7th 20 at 15:45
$content_length = filesize($file_path_str) + strlen(serialize($post_data)) + 100; - Maxwell.Nad commented on April 7th 20 at 15:48
where does the +100 I don't know, but the numbers add up, checked the 5 files. - Maxwell.Nad commented on April 7th 20 at 15:51
@Carolanne_Connelly, $post_data is an array with an object inside CurlFile and cannot be serialized.
USHvP4.jpg
There's also the code shows:
$post_data = ["file" => curl_file_create($file_name)];
- Edyth.Blick8 commented on April 7th 20 at 15:54
@Carolanne_Connelly, Thanks for the tips which led me to right thoughts! - Edyth.Blick8 commented on April 7th 20 at 15:57
April 7th 20 at 15:26
I send no POST with the usual form and PUT with the file. Not my case. - Edyth.Blick8 commented on April 7th 20 at 15:29
April 7th 20 at 15:28
Look at the function curl_getinfo().
Probably useful CURLINFO_HEADER_OUT, well, or CURLINFO_REQUEST_SIZE.
curl_getinfo looked, there's nothing useful was not - Edyth.Blick8 commented on April 7th 20 at 15:31

Find more questions by tags PHP