English 中文(简体)
Fogbugz XML_API PHP CURL File upload
原标题:

I have built a php script which receives values in $_POST and $_FILES

I m catching those values, and then trying to use CURL to make posts to FogBugz.

I can get text fields to work, but not files.

    $request_url = "http://fogbugz.icarusstudios.com/fogbugz/api.php";

            $newTicket = array();
            $newTicket[ cmd ] =  new ;
            $newTicket[ token ] = $token;
            $newTicket[ sPersonAssignedTo ] =  autobugz ;

            $text = "
";
            foreach( $form as $pair ) {
                $text .= $pair[2] . ": " . $pair[0] . "
";
            }
            $text = htmlentities( $text );
            $newTicket[ sEvent ] = $text;

            $f = 0;
            foreach ($_FILES as $fk => $v) {
                if ($_FILES[$fk][ tmp_name ] !=   ) {
                    $extension = pathinfo( $_FILES[$fk][ name ], PATHINFO_EXTENSION);
                    //only take the files we have specified above
                    if (in_array( array( $fk, $extension ) , $uploads)) {
                        $newTicket[ File .$f] = $_FILES[$fk][ tmp_name ];
                        //echo ( $_FILES[$fk][ name ] );
                        //echo ( $_FILES[$fk][ tmp_name ] );
                        //print $fk;
                        //print  <br/> ;
                        //print_r( $v );
                    }
                }
            }

            $ch = curl_init( $request_url );
            $timeout = 5;
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $newTicket );
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($ch);
            curl_close($ch);
最佳回答

To upload files with CURL you should prepend a @ to the path, see this example:

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file_box">
$post = array(
    "file_box"=>"@/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$response = curl_exec($ch);

Taken from http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html.

问题回答

The other answer -- for FogBugz reasons only --

$f cannot be set to 0 initially. It must be 1, so the files go through as File1, File2, etc.

The @ symbol is also key.





相关问题
fogbugz vs. OTRS

I m looking for a simple solution for support case management. The final nominees are Fogbugz and the open source OTRS.

Fogbugz XML_API PHP CURL File upload

I have built a php script which receives values in $_POST and $_FILES I m catching those values, and then trying to use CURL to make posts to FogBugz. I can get text fields to work, but not files. ...

VS 2010 addin for Fogbugz?

I m starting a project in Visual Studio 2010 and I d like to use FogBugz in conjunction with it, but the addin supplied appears to be for VS 2008. Is there a version that works with 2010? [crosses ...

热门标签