如何在没有cURL的情况下使用PHP HTTP发布XML文件? [英] How to HTTP POST a XML File using PHP without cURL?
问题描述
我有一个从Table In MySql创建的XML,我需要创建一个HTTP Post来将XML插入到Web服务中。 Web Service只接受SOAP,HTTP POST和HTTP GET方法。我试图以不同的方式发出HTTP POST请求而根本没有运气。我之前从未使用过SOAP。如何进行HTTP POST或SOAP请求?
I have a XML created from a Table In MySql, I need to make a HTTP Post to insert the XML into a Web Service. The Web Service just accepts SOAP, HTTP POST and HTTP GET methods. I tried to make the HTTP POST request in different ways with no luck at all. I never worked with SOAP before. How can I make the HTTP POST or SOAP Request?
post_xml.xml:
post_xml.xml:
<?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>
client1.php(HTTP POST CODE)
client1.php (HTTP POST CODE)
$xml = file_get_contents('post_xml.xml');
$url = 'http://stg.sa.com/post.asmx/';
$post_data = array ("XML" => $xml);
$stream_options = array(
$url => 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);
Web服务的HTTP POST规范:
HTTP POST specs of the Web Service:
以下是HTTP POST请求和响应示例。
The following is a sample HTTP POST request and response.
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阵列是错误
I think your POST array is wrong
尝试:
$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);
这篇关于如何在没有cURL的情况下使用PHP HTTP发布XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!