doubu7425 2012-10-09 03:58
浏览 35
已采纳

使用PHP通过cURL发布XML而无需先生成XML文件

I am trying to send an XML file via POST to a form on a remote server. I am generating the XML data on the fly, and storing it as a string in a variable called $XmlData. This is a string starting with my opening tag (see example below)...so no "headers" are in this string. I could easily write it to the server in a temp directory as an XML file...but ideally i want to just post this as if it were a file, but without the added step of creating the file (which i would then have to delete afterwords)... Below is the code that i THINK should allow me to upload my XML file if i actually created it first...

    $url  = 'http://www.mydomain.com/path/to/form/process.php';

    $ch = curl_init($url);
    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="xmlfileresource">
    $post = array(
        "xmlfileresource"=>"@/path/to/myfile.xml",
        "action"=>"incomingXml",
        "user"=>"JohnSmith",
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch);
    curl_close($ch);

My question is this: How can I stream/blob/whatever the content of my $XmlData string into this cURL execution such that the remote server thinks it was just a normal file being POSTed?

Not really relevant, but in case it helps...my $XmlData string might look something like this:

<clientversion>
    <component>
    <type>module</type>
    <name>resources</name>
    <version>2.7.3</version>
    <date>2012-09-01 02:18:33</date>
    </component>
    <component>
    <type>module</type>
    <name>staff</name>
    <version>3.1</version>
    <date>2011-04-01 07:12:48</date>
    </component>
</clientversion>
  • 写回答

1条回答

  • doupinyan0186 2012-10-17 16:50
    关注

    Ended up having to write temp file, post that, and then delete the temp file. Not as elegant as I had hoped, but it works fine. Thanks to Brenton Alker for the link...but it seems impractical to do it as outlined in that other post.

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

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧