如何在没有cURL的情况下在PHP中向API服务发送和接收XML数据

如何在没有cURL的情况下在PHP中向API服务发送和接收XML数据

问题描述:

I'm working a project to request data from api service by sending XML data. The programming language used is PHP. I have done so much research on the internet and nothing turned up except for using cURL. Is there any other way using PHP to achieve this.

<?php
$xml_data = '<mainservice>
               <customer>
                  <appln_id>myid</appln_id>
                  <password>mypasss</password>
                  <cust_id>1234</cust_id>
               </customer>
             </mainservice>';

This is the data that needs to be send. The id and password is for authenticating the API service and cust_id for retrieving data of that particular customer.

The Result data is also in XML format.

NOTE The service accepts only POST data.

我正在通过发送XML数据来从api服务请求数据。 使用的编程语言是 PHP code>。 我在互联网上做了很多研究,除了使用cURL之外什么也没发现。 有没有其他方法使用PHP来实现这一目标。 p>

 &lt;?php 
 $ xml_data ='&lt; mainservice&gt; 
&lt; customer&gt; 
&lt; appln_id&gt; myid&lt; / appln_id&gt; 
&lt; password&gt  ; mypasss&lt; / password&gt; 
&lt; cust_id&gt; 1234&lt; / cust_id&gt; 
&lt; / customer&gt; 
&lt; / mainservice&gt;'; 
  code>  pre> 
 
 

这是需要发送的数据。 id code>和 password code>用于验证API服务, cust_id code>用于检索特定客户的数据。 p> 结果数据也是XML格式。 p>

注意 strong> 该服务仅接受POST数据。 p> div>

<head>
    <meta charset="UTF-8">
    <title>test handle response</title>
    <script>
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST","URL to Request");
        var xmlDoc;
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            xmlDoc = xmlhttp.responseXML;
            console.log(xmlDoc);
            }
        };
        xmlhttp.setRequestHeader('Content-Type', 'text/xml');
        var xml = "<?xml version='1.0'?><query><author>John Steinbeck</author></query>";
        xmlhttp.send(xml);
    </script>
</head>
<body>

</body>
</html>

You must see

https://davidwalsh.name/web-service-php-mysql-xml-json

Hope you will find your solution.

You can also use this

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

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

Post data using file_get_contents() funtion

 $xml_data = '<mainservice>
               <customer>
                  <appln_id>myid</appln_id>
                  <password>mypasss</password>
                  <cust_id>1234</cust_id>
               </customer>
             </mainservice>';


$postdata = http_build_query(
    array(
        'xml_data' => $xml_data,
        'var2' => 'abc'
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://example.net/submit.php', false, $context);

Get data using file_get_contents() function

<?php 
$json_url = "http://api.example.com/test.php?type=menu";
$json = file_get_contents($json_url);
$data = json_decode($json);

echo "<pre>";
print_r($data);
echo "</pre>";