1

I am converting some Perl code to PHP.
However, I do not know much about Perl, so I have to code it with a rough meaning.

And, I do not understand what the below Perl code means...

What is the meaning of $req2->content(<<"POST_DATA") and --$boundary?
I've searched the Perl documentation, but it's too hard to find.

PHP code:

...
$boundary= 'Nobody-has-the-intention-to-erect-a-wall'; 

$req2 = curl_init($search_url); 
curl_setopt($req2, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($req2, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($req2, CURLOPT_POSTFIELDS, $data_string); 
curl_setopt($req2, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($req2, CURLOPT_COOKIE, $cookie); 
curl_setopt($req2, CURLOPT_HTTPHEADER, array( 
'Content-Type: multipart/form-data;boundary='.$boundary, 
'Content-Length: ' . strlen($data_string)) 
); 
$result= curl_exec($req2); 
...

Perl code:

...
my $boundary= 'Nobody-has-the-intention-to-erect-a-wall';
$req2->content_type('multipart/form-data;boundary='.$boundary);
$req2->header("Cookie"=>"access_token_cookie=$access_token_cookie; csrf_access_token=$csrf_access_token");

$req2->content(<<"POST_DATA"); #what means this?

--$boundary
Content-Disposition: form-data; name="num_result"
Content-Type: text/plain

$num_result
--$boundary
Content-Disposition: form-data; name="img"; filename="search.jpg"
Content-Type: image/jpeg

$image
--$boundary--
POST_DATA

my $res = $ua->request($req2);
...
simlev
  • 809
  • 2
  • 10
  • 23
JY Lee
  • 31
  • 3
  • This looks like [NOWDOC](http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc). They're also explained in PERL on the [Here document wikipedia page](https://en.wikipedia.org/wiki/Here_document) – h2ooooooo Sep 04 '18 at 07:46

2 Answers2

4
$req2->content(<<"POST_DATA"); #what means this?

The <<"POST_DATA" starts a HERE document, which is essentially a long string. The double quotes "" tell Perl to do string interpolation. That means that variables inside the string will be replaced with their content. The string ends when the parser encounters the delimiter, which in this case is the string POST_DATA.

The -- you are referring to is not an operator. It's used inside of the string. The program sends a multipart/formdata form over HTTP. Look at RFC 7578 if you are interested in the technical details. Essentially, each part of the request body represents one document. It can be multi-line and contain lots of information. The boundary can be set in the HTTP headers, and is typically a long, random string that would not appear in any of the body parts. See this answer for a more detailed explanation.

simbabque
  • 50,588
  • 8
  • 69
  • 121
3

Nothing very different from PHP actually.

  • << Heredoc, also present in PHP with slight differences:

    echo (<<<"POST_DATA"
    First line
    Second line
    POST_DATA
    );
    
  • -- Variable decrease, as in <?php $a=2; echo --$a;

Note:

Of course, inside a Heredoc -- is just text.


Suggestion:

If you don't fully understand the Perl, try to run it (it's not evil code).

my $boundary = 'Nobody-has-the-intention-to-erect-a-wall';
print(<<"POST_DATA");

--$boundary
Content-Disposition: form-data; name="num_result"
Content-Type: text/plain

$num_result
--$boundary
Content-Disposition: form-data; name="img"; filename="search.jpg"
Content-Type: image/jpeg

$image
--$boundary--
POST_DATA

Will yield:

 
--Nobody-has-the-intention-to-erect-a-wall
Content-Disposition: form-data; name="num_result"
Content-Type: text/plain


--Nobody-has-the-intention-to-erect-a-wall
Content-Disposition: form-data; name="img"; filename="search.jpg"
Content-Type: image/jpeg


--Nobody-has-the-intention-to-erect-a-wall--
simlev
  • 809
  • 2
  • 10
  • 23