cURL to PHP cURL (html2pdf.raph.site) cURL to PHP cURL (html2pdf.raph.site) curl curl

cURL to PHP cURL (html2pdf.raph.site)


You can use curl library in PHP.

Understanding the command. From man curl:

-F, --form <name=content>

(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit button.

This enables uploading of binary files etc. To force the 'content' part to be a file, prefix the file name with an @ sign...

The @ makes a file get attached in the post as a file upload.

In PHP version earlier than 5.5 you could simply post the params like:

curl_set_opt($ch, CURLOPT_POSTFIELDS, [    'file' => '@/path/to/file.html',]);

But this is no longer supported in newer versions. You will have to use curl_file_create instead.

Here is my code:

<?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,]);

Example of my output:

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)

Hope this helps.