douchuang1861 2011-10-19 17:34
浏览 35
已采纳

转换cURL - 从CLI转换为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? 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...

Edit #2: Per Fanis' suggestion, I replaced the file calls

$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条回答 默认 最新

  • dtl4521 2011-10-20 09:55
    关注

    (Posting comment as answer as per TekiusFanatikus' suggestion)

    The "@" operator from curl will not work like that in PHP's curl functions to include a file's contents. You'll need to get the file's contents into a variable as a string and pass that into CURLOPT_POSTFIELDS.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?