开发者

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['开发者_StackOverflow社区token'] = $token;
            $newTicket['sPersonAssignedTo'] = 'autobugz';

            $text = "\n";
            foreach( $form as $pair ) {
                $text .= $pair[2] . ": " . $pair[0] . "\n";
            }
            $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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜