加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

如何在没有cURL的情况下使用PHP HTTP发布XML文件?

发布时间:2020-12-14 01:18:44 所属栏目:Linux 来源:网络整理
导读:参见英文答案 How to post data in PHP using file_get_contents?????????????????????????????????????3个 我有一个从Table In MySql创建的XML,我需要创建一个HTTP Post来将XML插入到Web服务中. Web Service只接受SOAP,HTTP POST和HTTP GET方法.我尝试以不
参见英文答案 > How to post data in PHP using file_get_contents?????????????????????????????????????3个
我有一个从Table In MySql创建的XML,我需要创建一个HTTP Post来将XML插入到Web服务中. Web Service只接受SOAP,HTTP POST和HTTP GET方法.我尝试以不同的方式发出HTTP POST请求而根本没有运气.我之前从未使用过SOAP.如何进行HTTP POST或SOAP请求?

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代码)

$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' . "rn",'content' => http_build_query($post_data)
    )
);
$context = stream_context_create($stream_options);    
$response = file_get_contents($url,null,$context);

Web Service的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阵列是错误的

尝试:

$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-urlencodedrn",'content' => http_build_query($post_data),),);

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读