转换cURL - 从CLI转换为PHP

时间:2011-10-19 17:34:44

标签: php curl

以下是我要复制的内容:

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翻译好吗?我只是想从等式中删除一些东西。

编辑#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);

1 个答案:

答案 0 :(得分:1)

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

来自curl的“@”运算符不会像PHP的curl函数那样包含文件的内容。您需要将文件的内容作为字符串输入变量并将其传递给CURLOPT_POSTFIELDS。