您可以在PHP中使用curl
庫。
瞭解命令。從man curl
:
-F,--form <名稱=含量>
(HTTP)這允許捲曲模擬填充的形式,其中用戶按下提交按鈕。
這使的二進制文件等,要強制「內容」部分是一個文件,一個@符號前綴的文件名上傳...
的@使得文件得到重視在崗爲一個文件上傳。
在PHP版本低於5.5,你可以簡單地張貼PARAMS,如:
curl_set_opt($ch, CURLOPT_POSTFIELDS, [
'file' => '@/path/to/file.html',
]);
但是,這已不再在新版本的支持。您將不得不使用curl_file_create。
這裏是我的代碼:
<?php
$ch = curl_init();
$api_url = 'http://html2pdf.raph.site';
$html_file = '/home/ally/Desktop/file.html';
$params = [
'file' => curl_file_create($html_file),
];
$options = [
CURLOPT_URL => $api_url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
];
curl_setopt_array($ch, $options);
$response_body = curl_exec($ch);
$curl_error_no = curl_errno($ch);
$curl_error_msg = curl_error($ch);
if ($curl_error_no) {
print_r([
'error_no' => $curl_error_no,
'error_msg' => $curl_error_msg,
]);
echo 'There was an error posting html to the api.' . PHP_EOL;
die();
}
$response_info = curl_getinfo($ch);
curl_close($ch);
print_r([
'request_url' => $api_url,
'request_params' => $params,
'response_code' => $response_info['http_code'],
'response_info' => $response_info,
'response_body' => $response_body,
]);
我輸出的例子:
Array
(
[request_url] => http://html2pdf.raph.site
[request_params] => Array
(
[file] => CURLFile Object
(
[name] => /home/ally/Desktop/file.html
[mime] =>
[postname] =>
)
)
[response_code] => 200
[response_info] => Array
(
[url] => http://html2pdf.raph.site/
[content_type] => text/plain
[http_code] => 200
[header_size] => 182
[request_size] => 187
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.906493
[namelookup_time] => 0.512727
[connect_time] => 0.746585
[pretransfer_time] => 0.747354
[size_upload] => 236
[size_download] => 74
[speed_download] => 81
[speed_upload] => 260
[download_content_length] => 74
[upload_content_length] => 236
[starttransfer_time] => 0.782345
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 46.101.147.77
[certinfo] => Array
(
)
[primary_port] => 80
[local_ip] => 192.168.0.10
[local_port] => 53286
)
[response_body] => http://html2pdf.raph.site/static/ffc8e3bb-c0d9-4129-b756-0d5298f51bce.pdf
)
希望這有助於。
你可以使用curl擴展名php – siddhesh
任何方式都有幾十個庫,它們是一樣的 – siddhesh