如何在不使用cURL的情况下使用PHP以HTTP方式发布XML文件?
我有一个从MySql中的表创建的XML,我需要制作一个HTTP Post来将XML插入到Web服务中。Web服务只接受SOAP、HTTPPOST和HTTPGET方法。我试图以不同的方式发出HTTPPOST请求,但一点运气都没有。我以前从未用过肥皂。如何发出HTTP POST或SOAP请求 post_xml.xml:如何在不使用cURL的情况下使用PHP以HTTP方式发布XML文件?,php,xml,http-post,Php,Xml,Http Post,我有一个从MySql中的表创建的XML,我需要制作一个HTTP Post来将XML插入到Web服务中。Web服务只接受SOAP、HTTPPOST和HTTPGET方法。我试图以不同的方式发出HTTPPOST请求,但一点运气都没有。我以前从未用过肥皂。如何发出HTTP POST或SOAP请求 post_xml.xml: <?xml version="1.0" encoding="utf-8"?> <?ADF version="1.0"?> <adf> <pr
<?xml version="1.0" encoding="utf-8"?>
<?ADF version="1.0"?>
<adf>
<prospect><id sequence="1" source="xxxs">37</id>
<requestdate>2013-07-10 06:10:42</requestdate>
<vehicle interest="buy" status="new">
<year>2013</year>
<make>12</make>
<model>21</model>
<trim>Sport</trim>
</vehicle>
<customer>
<contact>
<name part="first">Jay</name>
<name part="last">11z</name>
<email>test@gmail.com</email>
<phone time="morning" type="voice" preferredcontact="1">99999999</phone>
<address>
<street line="1">1130 E Test</street>
<city>sa</city>
<regioncode>Z</regioncode>
<postalcode>79924</postalcode>
<country>USA</country>
</address>
</contact>
</prospect>
</adf>
Web服务的HTTP POST规范:
下面是一个HTTP POST请求和响应示例
POST /st.asmx/Post HTTP/1.1
Host: stg.sa.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length
XML= string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0"?>
xml
POST/st.asmx/POST HTTP/1.1
主持人:stg.sa.com
内容类型:application/x-www-form-urlencoded
内容长度:长度
XML=字符串
HTTP/1.1200ok
内容类型:text/xml;字符集=utf-8
内容长度:长度
xml
我认为您的帖子数组是错误的
尝试:
您确定XML应该在
XML
参数中传递,而不是作为请求主体吗?是的,这是web服务发布示例:st.sa.com/ausapst.asmx/post?XML=8002010-03-18T07:50:53-05:002004MERCEDES-BENZE-ClassE320 Rwd 4dr轿车(3.2L 6cyl 5A)JohnDoevalid@email.com...I将数组中的$URL更改为“http”,但仍然没有与web服务的通信。谢谢Dan2600,您的代码是发布帖子的正确方法。我的提供商向我发送了一个错误的URL。请注意,如果另一端正在使用like读取此原始数据php://input,并立即将其解释为XML,而无需将其从关联的数组变量索引“XML”中拉出,然后应将http\u build\u query($post\u data)
替换为简单的$XML
。
POST /st.asmx/Post HTTP/1.1
Host: stg.sa.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length
XML= string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0"?>
xml
$xml = file_get_contents('post_xml.xml');
$url = 'http://stg.sa.com/post.asmx/';
$post_data = array(
"xml" => $xml,
);
$stream_options = array(
'http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query($post_data),
),
);
$context = stream_context_create($stream_options);
$response = file_get_contents($url, null, $context);