01.
<?php
02.
$imageFile
= __DIR__ . DIRECTORY_SEPARATOR .
'image.jpg'
;
03.
$targetURL
= (
strtolower
(
$_SERVER
[
'REQUEST_SCHEME'
]) ===
'https'
?
'https://'
:
'http://'
)
04.
. (
$_SERVER
[
'HTTP_HOST'
] ??
'localhost'
)
05.
. (isset(
$_SERVER
[
'REQUEST_URI'
]) ? dirname(
$_SERVER
[
'REQUEST_URI'
]) :
''
)
06.
.
'/upload.php'
;
07.
08.
09.
if
(!
is_file
(
$imageFile
)) {
10.
http_response_code(404);
11.
echo
'<p>Image file was not found. Please prepare image file at this location: <strong>'
.
$imageFile
.
'</strong></p>'
;
12.
exit
();
13.
}
14.
15.
16.
$headers
= [];
17.
$allHeaders
= [];
18.
$postFields
= [];
19.
20.
21.
22.
$postFields
[
'hidden-input'
] =
'hidden value (from cURL).'
;
23.
24.
$Finfo
=
new
finfo();
25.
$fileMimeType
=
$Finfo
->file(
$imageFile
, FILEINFO_MIME_TYPE);
26.
unset(
$Finfo
);
27.
$CurlFile
=
new
CURLFile(
$imageFile
,
$fileMimeType
,
'curl-upload-'
.
basename
(
$imageFile
));
28.
$postFields
[
'image'
] =
$CurlFile
;
29.
unset(
$CurlFile
,
$fileMimeType
);
30.
31.
32.
33.
$ch
= curl_init(
$targetURL
);
34.
35.
curl_setopt(
$ch
, CURLOPT_RETURNTRANSFER, true);
36.
37.
curl_setopt(
$ch
, CURLOPT_POST, true);
38.
39.
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYPEER, false);
40.
curl_setopt(
$ch
, CURLOPT_SSL_VERIFYHOST, false);
41.
42.
curl_setopt(
$ch
, CURLOPT_HEADERFUNCTION,
'headerFunction'
);
43.
44.
curl_setopt(
$ch
, CURLOPT_POSTFIELDS,
$postFields
);
45.
46.
47.
$response
= curl_exec(
$ch
);
48.
$httpCode
= curl_getinfo(
$ch
, CURLINFO_HTTP_CODE);
49.
50.
if
(curl_errno(
$ch
)) {
51.
http_response_code(500);
52.
echo
'<p>cURL error: '
. curl_error(
$ch
) .
'</p>'
;
53.
curl_close(
$ch
);
54.
exit
();
55.
}
56.
57.
58.
curl_close(
$ch
);
59.
unset(
$ch
,
$postFields
);
60.
61.
62.
http_response_code(
$httpCode
);
63.
if
(isset(
$headers
[
'content-type'
])) {
64.
header(
'Content-type: '
.
$headers
[
'content-type'
]);
65.
}
66.
unset(
$headers
);
67.
68.
69.
70.
$responseJson
= json_decode(
$response
);
71.
unset(
$response
);
72.
$responseJson
->debug->allHeaders =
$allHeaders
;
73.
$responseJson
->debug->httpCode =
$httpCode
;
74.
75.
echo
json_encode(
$responseJson
);
76.
unset(
$responseJson
);
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
function
headerFunction(
$ch
,
$header
)
87.
{
88.
global
$allHeaders
,
$headers
;
89.
if
(!
empty
(trim(
$header
))) {
90.
$headerExp
=
explode
(
':'
,
$header
, 2);
91.
if
(
count
(
$headerExp
) === 2) {
92.
$headers
[
strtolower
(trim(
$headerExp
[0]))] = trim(
$headerExp
[1]);
93.
}
94.
$allHeaders
[] =
$header
;
95.
}
96.
return
(int) mb_strlen(
$header
);
97.
}