简体   繁体   English

如何在PHP中发出HTTP POST XML请求实际上将XML数据发送到供应商的服务器?

[英]How to make my HTTP POST XML request in PHP actually send XML data to my vendor's Server?

I'm working on some PHP code, that will generate an dynamic XML output that I want to HTTP POST to my Vendor's Server. 我正在处理一些PHP代码,该代码将生成一个动态XML输出,我希望将该输出通过HTTP POST到供应商的服务器。

My main goal is to get my item stock level synchronized with my Vendor's stock level, and when I POST the request as input, I should get an output back from their server. 我的主要目标是使我的商品库存水平与供应商的库存水平同步,并且当我将请求发布为输入时,我应该从他们的服务器获取输出。

I am creating the xml string with DOMDocument, and so far this is no problem, it works fine. 我正在使用DOMDocument创建xml字符串,到目前为止,这是没有问题的,它可以正常工作。

But when I am trying to HTTP POST using the "cUrl" method or the "file_get_contents()" method. 但是,当我尝试使用“ cUrl”方法或“ file_get_contents()”方法进行HTTP POST时。 The connection is made, but the server response is an error saying "No XML was recieved in HTTP POST". 建立了连接,但是服务器响应是一个错误,提示“ HTTP POST中未接收到XML”。

Im just using the standard code, as explained in this article Sending XML data using HTTP POST with PHP Like many other articles explaning the two methods. 我只是使用标准代码,如本文所述, 使用HTTP POST和PHP发送XML数据就像许多其他文章介绍了这两种方法一样。

The cUrl Method: cUrl方法:

$post_data = //the raw xml string or XML file from the DOMDocument.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://websiteURL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$content=curl_exec($ch);

or The file_get_contents() method: 或file_get_contents()方法:

$stream_options = array(
'http' => array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded' . "\r\n",
    'content' =>  $post_data));

$context  = stream_context_create($stream_options);
$response = file_get_contents($url, null, $context);

Both methods gives me a response from the server, that "No XML was recieved in HTTP POST". 两种方法都给我一个服务器响应,即“ HTTP POST中未接收到XML”。

If I print_r($post_data); 如果我print_r($ post_data); it shows the xml string that i want to send. 它显示了我要发送的xml字符串。 Like: 喜欢:

1. <?xml version="1.0" encoding="UTF-8"?> 
2. <pnarequest><customer.nr>some no.</customer_nr><password>some no.</password><Item><vare_nr>some sku no.</vare_nr>

New Info: 新资讯:

My vendor says they recieve my HTTP POST, but the content in the post is the variable "$post_data" as plain text, not the content inside the variable. 我的供应商说他们收到了我的HTTP POST,但是帖子中的内容是纯文本形式的变量“ $ post_data”,而不是变量中的内容。

Can someone please help me understanding why the XML data is not being Posted? 有人可以帮我理解为什么不发布XML数据吗?

Thanks. 谢谢。

The answer to my question: I needed to create the key "xml=" within the post, with my xml ($Post_data) as variable. 我的问题的答案:我需要在帖子中创建键“ xml =“,并将我的xml($ Post_data)作为变量。 Also i needed to get the correct user information from my vendor, to put into my xml variable. 另外,我还需要从供应商那里获取正确的用户信息,并将其放入xml变量中。 Now it works! 现在可以了! - Thanks to everyone who has helped to comment on this issue. -感谢所有对此问题发表评论的人。

It looks like you're not encoding the post data and you haven't defined a variable name for the XML field. 似乎您没有对发布数据进行编码,并且尚未为XML字段定义变量名。 The HTTP request here, since you're using post, should look like a request sent from a form with a single textarea. 由于您正在使用post,因此此处的HTTP请求应看起来像是从具有单个textarea的表单发送的请求。

<?php

$post_data = rawurlencode($post_data);   
curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml=' . $post_data);

?>

If you need to include more variables with the POST data. 如果需要在POST数据中包含更多变量。

<?php

$key = '12345';

$post_data =
    'xml=' . rawurlencode($post_data) .
    '&key=' . rawurlencode($key);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

?>

This is my last example code for you. 这是我为您提供的最后一个示例代码。 You're not posting correctly using keys and values. 您没有使用键和值正确发布。 You said you've used the example curl code from the other question, except you aren't, because they clearly used keys and values in the accepted answer and you did not. 您说您已经使用了另一个问题中的示例curl代码,只是您没有使用它,因为它们清楚地在接受的答案中使用了键和值,而您没有使用。 Also, I just checked the form that they have available on the site. 另外,我只是检查了网站上可用的表格。 The key name should be 'xml', just as I expected and the post should look like a form with a single textarea, just as I expected. 正如我所期望的那样,键名应该是“ xml”,并且正如我所期望的那样,帖子看起来应该像是具有单个textarea的表单。

<?php

$xml = '<?xml version="1.0"...';

$post_data =
    'xml=' . rawurlencode($post_data) .
    '&key=' . rawurlencode($key);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://websiteURL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$content=curl_exec($ch);

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

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