我正在将Perl代码转换为php。
该Perl代码将Heredoc用于多部分/表单数据,我想在php中实现它。
但是,我的php代码无法达到相同的结果(空)。
我的php代码有问题吗?
另外,我应该在php中使用Heredoc与Perl代码做相同的事情吗?
我不知道怎么了。
谢谢您的帮助!
* Perl代码
if($csrf_access_token ne ''){
my $req2 = HTTP::Request->new(POST => $search_api);
my $shinaban1 = 'g3717380';
open(my $out, '>out_search.tab');
open(my $in, 'g3717380.jpg');
my $image;
binmode($in);
read($in, $image, -s 'g3717380.jpg');
close($in);
my $boundary= 'test';
$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"); #Heredoc start
--$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 #Heredoc end
my $res = $ua->request($req2);
if($res->is_success) {
print $out $res->content;
print $res->content, "\n";
}
else {
print $res->headers_as_string;
print $res->status_line, "\n";
print $res->content, "\n";
}
}
*我的PHP代码
...
$image_data=file_get_contents('g3717380.jpg');
$boundary = "test";
$data =
<<<EOF
--$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_data
--$boundary--
EOF;
$ch2 = curl_init($search_url);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_POST, true);
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch2, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data;boundary='.$boundary)
);
$result= curl_exec($ch2);
curl_close($ch2);
?>