简体   繁体   English

转换cURL-从CLI到PHP

[英]Converting cURL - from CLI to PHP

Here is what I have to replicate: 这是我必须复制的内容:

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

Here is what I have so far: 这是我到目前为止的内容:

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

Discounting the fact that the API isn't returning an error, that the call to the API doesn't seem to be doing anything, is my CURL translation good? 撇开API未返回错误,对API的调用似乎没有任何作用的事实,我的CURL转换是否还不错? I'm just trying to remove things from the equation. 我只是想从方程式中删除内容。

Edit #1 : While sanitizing my post (and removing debug output), I had removed the $buffer = curl.exec($curl_handle) line... 编辑#1 :在清理我的帖子(并删除调试输出)时,我删除了$ buffer = curl.exec($ curl_handle)行...

Edit #2 : Per Fanis' suggestion, I replaced the file calls 编辑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);

(Posting comment as answer as per TekiusFanatikus' suggestion) (根据TekiusFanatikus的建议发布评论作为答案)

The "@" operator from curl will not work like that in PHP's curl functions to include a file's contents. curl的“ @”运算符将无法像PHP的curl函数中那样包含文件内容。 You'll need to get the file's contents into a variable as a string and pass that into CURLOPT_POSTFIELDS. 您需要将文件的内容作为字符串获取到变量中,然后将其传递给CURLOPT_POSTFIELDS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM