使用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>
douyun6781
douyun6781 Alker我同意这种方法比使用临时文件更加糟糕。太糟糕了...我真的想在记忆中做到这一切,但我找不到实现它的真正方法。我将它写入文件,发布,然后删除该文件。谢谢。
大约 8 年之前 回复
douhuan3448
douhuan3448 stackoverflow.com/questions/3085990/...是我见过的唯一解决方案。但我觉得将它写入文件仍然会更加清洁。
大约 8 年之前 回复

1个回答



结束必须写入临时文件,发布该文件,然后删除临时文件。 不像我希望的那样优雅,但它运作良好。 感谢Brenton Alker的链接...但是如其他帖子所述,这样做似乎不切实际。</ p>
</ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐