سؤال

لقد قمت ببناء برنامج نصي 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);
هل كانت مفيدة؟

المحلول

لتحميل الملفات باستخدام حليقة ، يجب عليك إعداد أ @ إلى المسار ، انظر هذا المثال:

$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.

نصائح أخرى

الجواب الآخر -لأسباب Fogbugz فقط -

لا يمكن ضبط $ f على 0 في البداية. يجب أن يكون 1 ، لذلك تمر الملفات كملف ، ملف ، إلخ.

الرمز @ هو المفتاح أيضًا.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top