Fogbugz XML_API PHP CURL 文件上传

发布于 2024-08-19 01:45:52 字数 1706 浏览 11 评论 0原文

我已经构建了一个 php 脚本,它接收 $_POST 和 $_FILES 中的值,

我正在捕获这些值,然后尝试使用 CURL 向 FogBugz 发布帖子。

我可以让文本字段工作,但不能让文件工作。

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

            $newTicket = array();
            $newTicket['cmd'] = 'new';
            $newTicket['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);

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 = "\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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∝单色的世界 2024-08-26 01:45:52

要使用 CURL 上传文件,您应该在路径前添加一个 @,请参阅以下示例:

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

取自 http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php。 html.

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.

雨夜星沙 2024-08-26 01:45:52

另一个答案——仅出于 FogBugz 的原因——

$f 最初不能设置为 0。它必须是 1,因此文件会显示为 File1、File2 等。

@ 符号也是关键。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文