1

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.

3 Answers 3

1

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>";
Sign up to request clarification or add additional context in comments.

6 Comments

how does it work with XML? I need to send $xml_data = '<mainservice> <customer> <appln_id>myid</appln_id> <password>mypasss</password> <cust_id>1234</cust_id> </customer> </mainservice>';
Replace "$postdata" variable to your one, let me update answer
You can try above answer, you just need to change the URL here file_get_contents('http://example.net/submit.php', false, $context); to where you want to send the data
Nope. That didn't work. var_dump($result) is returning bool(false)
Please see the code here https://www.tehplayground.com/e0934u36dPj5mu4d . If the call is successful, it will return user authentication error.
|
1

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

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

3 Comments

thanks for the link. but it only shows reading and printing the XML data received from the web-service. How do I send data in XML format to the web-service using POST.
I think this is the exactly you want - link
Thank you. Can you make it as answer, so that i can close this question.
1
<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.