简体   繁体   English

PHP cURL:curl_exec到重定向的网络表单

[英]PHP cURL: curl_exec to a webform that redirects

I'm new to cURL and I am having a bit of an issue getting it to work. 我是cURL的新手,让它无法正常工作。 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. 有问题的Web表单将获取文件和输入参数,并将文件转换为另一种格式。 When using this webform directly, submit redirects to a new page that displays a progress bar showing the progress of the file conversion. 直接使用此Web表单时,提交重定向到一个新页面,该页面显示一个进度条,该进度条显示文件转换的进度。 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. 我需要做的是提交一个文件(带有参数),等待转换完成,下载文件,然后继续处理已下载文件的PHP代码。 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. 但是,我在使用cURL时遇到了麻烦,并且不确定是否与重定向到出现进度条的页面有关。 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. $ response的初始实例返回空的Web表单。 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. 我没有curl_error的错误,也没有从curl_getinfo看到错误,我可以正确连接到服务器,但是在curl过程在不到1秒的时间内完成文件转换过程大约需要5分钟。 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 这是CURLOPT_VERBOSE的输出

  • Adding handle: conn: 0x7fa754818000 添加句柄:conn:0x7fa754818000
  • Adding handle: send: 0 添加句柄:发送:0
  • Adding handle: recv: 0 添加句柄:recv:0
  • Curl_addHandleToPipeline: length: 1 Curl_addHandleToPipeline:长度:1
  • Conn 16 (0x7fa754818000) send_pipe: 1, recv_pipe: 0 Conn 16(0x7fa754818000)send_pipe:1,recv_pipe:0
  • About to connect() to example.com port 80 (#16) 即将connect()到example.com端口80(#16)
  • Trying XX.XXX.XXX.XXX... 正在尝试XX.XXX.XXX.XXX ...
  • Connected to example.com (XX.XXX.XXX.XXX) port 80 (#16) 已连接至example.com(XX.XXX.XXX.XXX)端口80(#16)
  • POST /Converter/index.pl HTTP/1.1 POST /Converter/index.pl HTTP / 1.1
  • Host: example.com 主持人:example.com
  • Accept: / 接受: /
  • Content-Length: 249010577 内容长度:249010577
  • Expect: 100-continue 期望:100-继续
  • Content-Type: multipart/form-data; 内容类型:多部分/表单数据; boundary=----------------------------52f3c644b6de 边界= ---------------------------- 52f3c644b6de

  • HTTP/1.1 100 Continue HTTP / 1.1 100继续

  • HTTP/1.1 200 OK HTTP / 1.1 200 OK
  • Date: Fri, 01 May 2015 14:01:17 GMT 日期:2015年5月1日,星期五,格林尼治标准时间14:01:17
  • Server Apache/2.2.22 (Win32) is not blacklisted 服务器Apache / 2.2.22(Win32)未列入黑名单
  • Server: Apache/2.2.22 (Win32) 伺服器:Apache / 2.2.22(Win32)
  • Transfer-Encoding: chunked 传输编码:分块
  • Content-Type: text/html; 内容类型:text / html; charset=ISO-8859-1 字符集= ISO-8859-1

  • Connection #16 to host example.com left intact 与主机example.com的连接#16保持不变

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: 我猜您可以使用header()将用户重定向到转换后的文件,也可以删除最后一个curl,并使用CURLOPT_FOLLOWLOCATION, true在第一个curl上为CURLOPT_FOLLOWLOCATION, true ,即:

$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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM