how to send pre-recorded xml request

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

how to send pre-recorded xml request

Post by yacahuma »

I want to be able to send a bunch of xml request already recorded.
An example of the request

Code: Select all

 
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://ws.interfaces.sessions.APILink.amdocs" xmlns:ns2="http://datainfo.APILink.amdocs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns3="http://io.datainfo.APILink.amdocs" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns4="http://awsi.amdocs.com" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:createBan><in0 xsi:type="ns3:InCreateBanInfo"><mAccountTypeInfo xsi:type="ns3:AccountTypeInfo"><mAccountType xsi:type="xsd:string">I</mAccountType>
......
</in0></ns1:createBan></SOAP-ENV:Body></SOAP-ENV:Envelope>
 
I though I could use curl

Code: Select all

 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://SERVICE_HERE');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);
    curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('data.xml'));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
 
    $start = array_sum(explode(' ', microtime()));
    $buffer = curl_exec($ch);
 
 
The result I am getting

Code: Select all

 
<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <soapenv:Fault> <faultcode xmlns:ns1="http://xml.apache.org/axis/">ns1:Client.NoSOAPAction</faultcode> <faultstring>no SOAPAction header!</faultstring> <detail> <ns2:hostname xmlns:ns2="http://xml.apache.org/axis/">petapps</ns2:hostname> </detail> </soapenv:Fault> </soapenv:Body> </soapenv:Envelope>Total time for request: 1.0137739181519 HTTP ERROR -> 500
 
Any ideas? Thank you
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: how to send pre-recorded xml request

Post by yacahuma »

After analyzing the output from soapui using PacketAnalyzer(seem to be an execelent product) I notice the header

Code: Select all

 
POST /awsi/services/Ban HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
 
so I added the soap action into the header and now it works

Code: Select all

 
    function xml_post($post_xml,$url)
    {
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        $header = array();
        $header[] = 'Content-Type: text/xml;charset=UTF-8';
        $header[] = 'SOAPAction: ""';
        $header[] = 'Accept-Encoding: gzip,deflate';
 
 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
//        curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_xml);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
 
Post Reply