Fogbugz XML_API PHP CURL文件上传
我构建了一个php脚本,它接收$\u POST和$\u文件中的值 我正在捕捉这些值,然后尝试使用CURL向FogBugz发布帖子 我可以让文本字段工作,但不能让文件工作Fogbugz XML_API PHP CURL文件上传,php,api,curl,fogbugz,Php,Api,Curl,Fogbugz,我构建了一个php脚本,它接收$\u POST和$\u文件中的值 我正在捕捉这些值,然后尝试使用CURL向FogBugz发布帖子 我可以让文本字段工作,但不能让文件工作 $request_url = "http://fogbugz.icarusstudios.com/fogbugz/api.php"; $newTicket = array(); $newTicket['cmd'] = 'new'; $newTick
$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);
$request\u url=”http://fogbugz.icarusstudios.com/fogbugz/api.php";
$newTicket=array();
$newTicket['cmd']='new';
$newTicket['token']=$token;
$newTicket['sPersonAssignedTo']='autobugz';
$text=“\n”;
foreach($pair形式){
$text.=$pair[2]。“:”$pair[0]。“\n”;
}
$text=htmlentities($text);
$newTicket['sEvent']=$text;
$f=0;
foreach($\文件格式为$fk=>$v){
如果($\u文件[$fk]['tmp\u名称']!=''){
$extension=pathinfo($\u文件[$fk]['name'],pathinfo\u扩展名);
//只接受我们上面指定的文件
if(在_数组中(数组($fk,$extension),$uploads)){
$newTicket['File'.$f]=$\u文件[$fk]['tmp\u名称'];
//echo($_文件[$fk]['name']);
//echo($_文件[$fk]['tmp_名称']);
//打印$fk;
//打印“
”;
//印刷费($v);
}
}
}
$ch=curl\u init($request\u url);
$timeout=5;
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch,CURLOPT_POSTFIELDS,$newTicket);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data=curl\u exec($ch);
卷曲关闭($ch);
要使用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);
$ch=curl_init();
curl_setopt($ch,CURLOPT_头,0);
curl_setopt($ch,CURLOPT_VERBOSE,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERAGENT,“Mozilla/4.0(兼容;)”);
curl_setopt($ch,CURLOPT_URL,_VIRUS_SCAN_URL);
curl_setopt($ch,CURLOPT_POST,true);
//同
$post=数组(
“文件盒”=>“@/path/to/myfile.jpg”,
);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
$response=curl\u exec($ch);
摘自。另一个答案——仅出于FogBugz的原因--
$f最初不能设置为0。它必须是1,因此文件将作为File1、File2等进行处理
@符号也是关键。@John:没问题,很高兴我能帮上忙。另外,请确保在发送带有URL编码字符串的文件时始终使用post数组,否则将无法工作。