views:

135

answers:

2

Hello,

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

A: 

Well, I can't say I've done this myself, but according to the PHP documentation regarding CURL and File Handling (PHP.net)

CURLOPT_INFILE | The file that the transfer should be read from when uploading.

value should be a stream resource (using fopen(), for example) for the following values of the option parameter:

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 call move_uploaded_file() to a controlled directory on the server before calling fopen()

For example:

$ch = curl_init();

$file = fopen($_FILES['file-field-name']['tmp_name'], 'r');

// OTHER CURL CONFIGURATION
curl_setopt($ch, CURLOPT_INFILE, $file);

curl_exec($ch);
Dominic Barnes
A: 

First, your script can access the uploaded file through the $_FILES array.

From http://www.tizag.com/phpT/fileupload.php

The $_FILES array is where PHP stores all the information about files. There are two elements of this array that we will need to understand for this example.

  • uploadedfile - uploadedfile is the reference we assigned in our HTML form. We will need this to tell the $_FILES array which file we want to play around with.
  • $_FILES['uploadedfile']['name'] - name contains the original path of the user uploaded file.
  • $_FILES['uploadedfile']['tmp_name'] - tmp_name contains the path to the temporary file that resides on the server. The file should exist on the server in a temporary directory with a temporary name.

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:

$post_fields = array (
    $key => $value,
    ...
    $file_key => "@" . $filename,
);

If you use the "@" symbol, libcurl will read the file and include it in the POST.

Then use

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
bmb
Now my question is. Do I loose the original name? I did a print_r($_FILES) and I see the original name, and the temp path for the file. When my other application posts the file will it be /tmp/kwsnWjwn instead of 'FriendlyName.xls' ?
Israel Lopez
Israel Lopez, you will lose the original name using libcurl. I do not believe you can set this value using libcurl, although other tools may allow it. However, you can use PHP's move_uploaded_file() function to move/rename the temp file to a friendly name on your system, and then use that file in your $post_fields array.
bmb