I'm new to cURL and I am having a bit of an issue getting it to work. I need to send some parameters, as well as upload a file to a webform. The webform in question will take the file and the input parameters and convert the file to another format. When using this webform directly, submit redirects to a new page that displays a progress bar showing the progress of the file conversion. On completion a download link appears.
What I need to do is submit a file (with parameters), wait for the conversion to complete, download the file and proceed with my PHP code that processes the downloaded file. However, I'm having a hard time with cURL and I'm not sure if it has something to do with the redirect to the page with the progress bar that occurs. Example code is below:
$full_file_path = realpath($inputfile);
$data = array('param1' => '1', 'param2' => '2', 'file' => '@'.$full_file_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/Converter/index.pl');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
#get converted file
$file_head=substr($inputfile, 0, strpos($inputfile, '.raw'));
$output_file=$file_head.".mzXML";
$url="http://example.com/Converter/temp/".$output_file;
$fp = fopen ($output_file, 'w+');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
curl_close($ch);
The initial instance of $response returns the empty webform. I get no errors with curl_error and from curl_getinfo I can see that I am correctly connecting to the server, however the file conversion process should take ~5minutes while the curl process completes in <1 sec. I'm not sure what's going wrong or how to get more info to tell me what's happening. My best guess is it has something to do with the redirect but I don't know for sure.
Any help is much appreciated.
EDIT
Here is the output from CURLOPT_VERBOSE
Content-Type: multipart/form-data; boundary=----------------------------52f3c644b6de
HTTP/1.1 100 Continue
Content-Type: text/html; charset=ISO-8859-1
Connection #16 to host example.com left intact
I guess you can use header() to redirect the user to the converted file, also, remove the last curl, and use CURLOPT_FOLLOWLOCATION, true
on the first curl ie:
$full_file_path = realpath($inputfile);
$data = array('param1' => '1', 'param2' => '2', 'file' => '@'.$full_file_path);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/Converter/index.pl');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
#get converted file
$file_head=substr($inputfile, 0, strpos($inputfile, '.raw'));
$output_file=$file_head.".mzXML";
$url="http://example.com/Converter/temp/".$output_file;
$fp = fopen ($output_file, 'w+');
header("Location: $url");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.