程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

perl中的“<<”,“--”和php中的实现是什么意思?

发布于2022-06-23 18:46     阅读(986)     评论(0)     点赞(0)     收藏(3)


我正在将一些 Perl 代码转换为 PHP。
不过我对Perl了解不多,所以只好用粗略的意思来编码。

而且,我不明白下面的 Perl 代码是什么意思......

$req2->content(<<"POST_DATA")的含义是--$boundary什么?
我已经搜索了 Perl文档,但很难找到。

PHP代码:

...
$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代码:

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

解决方案


实际上与 PHP 没有什么不同。

  • <<Heredoc,也存在于PHP中,但略有不同:

    echo (<<<"POST_DATA"
    First line
    Second line
    POST_DATA
    );
    
  • --可变减少,如<?php $a=2; echo --$a;

笔记:

当然,Heredoc 内部--只是文本。


建议:

如果您不完全理解 Perl,请尝试运行它(它不是恶意代码)。

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

将产生:

 
--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--


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.phpheidong.com/blog/article/349983/1e1c2b2fb369c17ec470/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

0 0
收藏该文
已收藏

评论内容:(最多支持255个字符)