转换cURL -从CLI到PHP


Converting cURL - from CLI to PHP

以下是我必须复制的内容:

curl --basic
     --user testuser:testuser
     --form xml=@newdoc.xml
     --form data1=@mynewfile.xml
     http://localhost:9263/repository/document

到目前为止我写的是:

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_USERPWD, "user:pass");
curl_setopt($curl_handle,CURLOPT_URL, "http://localhost:9263/repository/document");
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_POST, 1);
$post = array("xml"=>"@e:/path_to_file/old.xml","data1"=>"@e:/path_to_file/new.xml");
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

不考虑API没有返回错误的事实,对API的调用似乎没有做任何事情,我的CURL翻译好吗?我只是想从等式中去掉一些东西。

Edit #1:在清理我的帖子(并删除调试输出)时,我删除了$buffer = curl.exec($curl_handle)行…

编辑#2:根据Fanis的建议,我替换了文件调用

$old_xml_string = daisy_exec_query("http://localhost:9263/repository/document/8-Multimedia?branch=1&language=2");
//omited dom operations
$new_xml_string = $dom->saveXML();  
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_USERPWD, "user:pass");
curl_setopt($curl_handle,CURLOPT_URL, "http://localhost:9263/repository/document");
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_POST, 1);
$post = array("xml"=>$old_xml_string,"data1"=>$new_xml_string);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

(根据TekiusFanatikus的建议发表评论作为答案)

curl中的"@"操作符不能像PHP的curl函数那样包含文件的内容。您需要将文件的内容作为字符串转换为变量,并将其传递给CURLOPT_POSTFIELDS。