php - 如何在没有 cURL 的情况下使用 PHP HTTP POST XML 文件?

标签 php xml http-post

<分区>

我有一个从 MySql 中的表创建的 XML,我需要创建一个 HTTP Post 以将 XML 插入到 Web 服务中。 Web 服务只接受 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' . "\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 请求和响应示例。

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-urlencoded\r\n",
       'content' => http_build_query($post_data),
    ),
);

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

关于php - 如何在没有 cURL 的情况下使用 PHP HTTP POST XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17578799/

相关文章:

php - 登录后,所有页面都应该是https吗?

javascript - angularjs http post 到特定元素处的 json 数组

javascript - 如何在 javascript 中存储使用 api 获得的 XML 响应?

c# - 如何将字节数组转换为xml?

request - 对 Google Dialogflow 使用 HTTP 请求

php - Android HttpPost 中的 JSON _POST 为空

php - 使用 PHP 发送带有 PDF 文件附件的电子邮件

php - Apache ErrorDocument 指令不重定向

php - Wordpress 2015 主题 : Space on top of page. 找不到相关的 css

php - 使用 PHP SimpleXML 获取下一个元素