将上传的文件发布到另一个 PHP 页面?
这是我的问题。我有一个不太“用户友好”的票务跟踪系统,但我希望我的用户能够在不让他们看到的情况下向系统提交“票证”。
我只使用自定义 HTTP 表单并将其发布到票证跟踪系统。一个问题是“成功/完成”页面容易让人感到困惑。所以我想......好吧,因为我无法更改票证系统以使用不同的“成功”页面。我将仅使用 CURL 处理 HTTP Post 交换并报告自定义成功或问题页面。这是一些抽象代码。
文件:tickethelper.php
<?php
extract($_POST);
$url = 'TICKETSYSTEMURL';
$fields = array(
'fullname'=>urlencode($fullname),
/*many more fields*/
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
if((strlen(strstr($contents,'Your ticket has been submitted')))>0){
header("Location: http://THANKYOU");
}
else{
header("Location: http://OOPS");
}
?>
但是,我意识到我缺少文件上传。我见过的大多数 CURL 示例都与将本地文件上传到远程 HTTP POST 页面有关。
如何处理从用户 HTTP 表单接收文件、在“tickethelper”中处理该文件并将其发布到“TICKETSYSTEMURL”?
-以色列
Here is my problem. I have a ticket-tracking system that is not very 'user friendly' but I have a desire for my users to submit 'tickets' to the system without having them to see it.
I got as far as just using a custom HTTP Form and posting to the ticket tracking system. One problem is the 'success/completion' page has a tendency to confuse people. So I figured... well since I cant change the ticket system to use a different 'success' page. I will just handle the HTTP Post exchange with CURL and report a custom success or problem page. Heres some abstracted code.
File: tickethelper.php
<?php
extract($_POST);
$url = 'TICKETSYSTEMURL';
$fields = array(
'fullname'=>urlencode($fullname),
/*many more fields*/
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_MAXREDIRS, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
if((strlen(strstr($contents,'Your ticket has been submitted')))>0){
header("Location: http://THANKYOU");
}
else{
header("Location: http://OOPS");
}
?>
However, what I realized is that I am missing my file upload. Most of the CURL examples I have seen are to do with uploading local files to a remote HTTP POST page.
How do I handle receiving a file from my users HTTP form, process this in the 'tickethelper' and POST that to the 'TICKETSYSTEMURL' ?
-Israel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我不能说我自己做到了这一点,但根据有关 CURL 和文件处理的 PHP 文档 (PHP.net)
您应该能够对已上传的文件调用
fopen()
(请参阅有关处理文件上传的文档)我不确定,但您可能需要在调用fopen()
之前先调用move_uploaded_file()
到服务器上的受控目录,例如:
Well, I can't say I've done this myself, but according to the PHP documentation regarding CURL and File Handling (PHP.net)
You should be able to call
fopen()
on the file that has been uploaded (refer to documentation regarding Handling File Uploads) I'm not sure, but you may need to callmove_uploaded_file()
to a controlled directory on the server before callingfopen()
For example:
首先,您的脚本可以通过 $_FILES 数组访问上传的文件。
来自 http://www.tizag.com/phpT/fileupload.php
要将此文件包含在使用 libcurl 发送的 POST 中,请使用字段数组而不是字符串。将文件包含为字段之一:
如果使用“@”符号,libcurl 将读取该文件并将其包含在 POST 中。
然后使用
First, your script can access the uploaded file through the $_FILES array.
From http://www.tizag.com/phpT/fileupload.php
To include this file in the POST you send with libcurl, use an array of fields instead of a string. Include the file as one of the fields:
If you use the "@" symbol, libcurl will read the file and include it in the POST.
Then use