Solved

PHP SOAP XML Post

Posted on 2014-03-24
12
2,559 Views
Last Modified: 2016-02-26
Hi,

I am new to PHP and SOAP requests so please forgive the question...

I have an XML script (attached) that I want to be able to use a basic web form to change 2 variables. I want to create a PHP script that allows me to type in the Pattern and the Description and then submit it to my SOAP Request server. I know how to submit via CURL.

Just wondering if someone could help me write something in PHP that would accomplish this, nothing fancy, just a basic input asking for the Pattern and Description then Submit.


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/8.5">
   <soapenv:Header/>
   <soapenv:Body>
      <ns:addLine sequence="?">
         <line>
            <pattern>?</pattern>
            <description>?</description>
               </line>
      </ns:addLine>
   </soapenv:Body>
</soapenv:Envelope>

Open in new window


Any response back from the server would be good also if it can print on screen.
0
Comment
Question by:mark_06
  • 7
  • 5
12 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
PHP forms are described in the introductory tutorial here:
http://php.net/manual/en/tutorial.forms.php

Heredoc notation might be helpful here.  I'll try to give you an example in a moment.  If you're new to PHP, this article can help you find dependable learning resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Please see http://www.iconoun.com/demo/mark_06.php

You would make the cURL call to the web service near lines 28-30.  The existing code there is just for my proof of concept to see that the string variable substitution worked.

<?php // demo/mark_06.php
error_reporting(E_ALL);

// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28395751.html

// IF THERE IS A POST-METHOD REQUEST
if (!empty($_POST))
{
    // RETRIEVE VALUES OR ACCEPTABLE DEFAULTS
    $p = !empty($_POST['p']) ? $_POST['p'] : NULL;
    $d = !empty($_POST['d']) ? $_POST['d'] : NULL;

   // CONSTRUCT THE XML DOCUMENT USING HEREDOC SYNTAX
	$xml = <<<ENDXML
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/8.5">
   <soapenv:Header/>
   <soapenv:Body>
      <ns:addLine sequence="?">
         <line>
            <pattern>$p</pattern>
            <description>$d</description>
         </line>
      </ns:addLine>
   </soapenv:Body>
</soapenv:Envelope>
ENDXML;

    // MAKE THE cURL CALL HERE
    echo '<pre>';
    echo htmlentities($xml);
}

// IF THERE IS NO POST METHOD REQUEST, CREATE THE FORM
else
{
    $form = <<<ENDFORM
<form method="post">
Pattern: <input name="p" />
Description: <input name="d" />
<input type="submit" value="Go!" />
</form>
ENDFORM;

    echo $form;
}

Open in new window

Please post back if you have any questions, ~Ray
0
 
LVL 6

Author Comment

by:mark_06
Comment Utility
thanks for that. sorry to be a pest, however where exactly do i place my CURL call in the code? also can i phave it primt out "completed request" instead of the XML string?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
where exactly do i place my CURL call in the code?
Line 28, where it says "MAKE THE cURL CALL HERE"
i phave it primt out "completed request"
Yes. remove lines 29 and 30, and instead insert this line.

echo 'completed request';

Open in new window

0
 
LVL 6

Author Comment

by:mark_06
Comment Utility
Thanks for that, you have been very helpful however I am having trouble getting the CURL commands correct.

In CLI this is the CURL commands I use....

curl -k -u cups:password1! -H "Content-type: text/xml;" -H "SOAPAction: CUCM:DB ver=8.5"  -d  https://192.168.16.245:8443/axl/

im having trouble conversting that into the CURL for PHP
0
 
LVL 6

Author Comment

by:mark_06
Comment Utility
This is the code I am currently trying

<?php // demo/mark_06.php
error_reporting(E_ALL);

// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28395751.html

// IF THERE IS A POST-METHOD REQUEST
if (!empty($_POST))
{
    // RETRIEVE VALUES OR ACCEPTABLE DEFAULTS
    $p = !empty($_POST['p']) ? $_POST['p'] : NULL;
    $d = !empty($_POST['d']) ? $_POST['d'] : NULL;

   // CONSTRUCT THE XML DOCUMENT USING HEREDOC SYNTAX
	$xml = <<<ENDXML
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/8.5">
   <soapenv:Header/>
   <soapenv:Body>
      <ns:addLine sequence="?">
         <line>
            <pattern>$p</pattern>
            <description>$d</description>
         </line>
      </ns:addLine>
   </soapenv:Body>
</soapenv:Envelope>
ENDXML;

$username = 'cups';
$password = 'Password1!';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://192.168.16.245:8443/axl');
curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
curl_exec($ch);
curl_close ($ch);



	echo '<pre>';
    echo htmlentities($xml);
}

// IF THERE IS NO POST METHOD REQUEST, CREATE THE FORM
else
{
    $form = <<<ENDFORM
<form method="post">
Pattern: <input name="p" />
Description: <input name="d" />
<input type="submit" value="Go!" />
</form>
ENDFORM;

    echo $form;
}

Open in new window

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 6

Author Comment

by:mark_06
Comment Utility
Can't for the life of me seem to get the XML to Post to the server
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
For some reason, PHP programmers seem unique among software developers in the matter of ignoring return values from functions and error conditions.  There's often a reason for a problem that is obvious if only the PHP script tests for it.  Of course, with SOAP, everything is obscure.  If you can find a RESTful API instead of SOAP it's often much easier to get things right.

This should give us a little better diagnostic ability.  Let's see what happens.  When I try to test it, it just times out, so I can't tell what is happening at that URL.

<?php // demo/mark_06.php
error_reporting(E_ALL);

// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28395751.html

// IF THERE IS A POST-METHOD REQUEST
if (!empty($_POST))
{
    // RETRIEVE VALUES OR ACCEPTABLE DEFAULTS
    $p = !empty($_POST['p']) ? $_POST['p'] : NULL;
    $d = !empty($_POST['d']) ? $_POST['d'] : NULL;

   // CONSTRUCT THE XML DOCUMENT USING HEREDOC SYNTAX
	$xml = <<<ENDXML
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/8.5">
   <soapenv:Header/>
   <soapenv:Body>
      <ns:addLine sequence="?">
         <line>
            <pattern>$p</pattern>
            <description>$d</description>
         </line>
      </ns:addLine>
   </soapenv:Body>
</soapenv:Envelope>
ENDXML;

    // MAKE THE cURL CALL HERE
    $url = 'https://192.168.16.245:8443/axl';
    $unm = 'cups';
    $pwd = 'password1';
    $xml = urlencode($xml);
    $res = curl_post($url, $unm, $pwd, $xml, 4);
    echo '<pre>';
    var_dump($res);
}

// IF THERE IS NO POST METHOD REQUEST, CREATE THE FORM
else
{
    $form = <<<ENDFORM
<form method="post">
Pattern: <input name="p" />
Description: <input name="d" />
<input type="submit" value="Go!" />
</form>
ENDFORM;

    echo $form;
}


function curl_post
( $url
, $u=NULL
, $p=NULL
, $post_string=NULL
, $timeout=2
, $error_report=TRUE
){
    // PREPARE THE CURL CALL
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL,            $url           );
    curl_setopt( $curl, CURLOPT_USERPWD,        $u . ':' . $p  );

    curl_setopt( $curl, CURLOPT_HEADER,         FALSE          );
    curl_setopt( $curl, CURLOPT_POST,           TRUE           );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string   );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate' );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout       );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE           );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE           );

    // EXECUTE THE CURL CALL
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            echo PHP_EOL . '<pre>' . PHP_EOL;
            var_dump($inf);
            echo PHP_EOL . '</pre>' . PHP_EOL;
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS
    curl_close($curl);
    return $htm;
}

Open in new window

0
 
LVL 6

Author Comment

by:mark_06
Comment Utility
Thanks for that. I can get it to show the basic HTML page but its still not posting the XML. If I use the CLI with

curl -k -u cups:Nielbazza1! -H "Content-type: text/xml;" -H "SOAPAction: CUCM:DB ver=8.5"  -d @addline.xml https://192.168.16.245:8443/axl/  

Then it works, where @addline.xml contains exactly the XML in it as above it works. Just trying to workout why this won't convert into PHP.

I have attached the message I am getting...
error-xml.png
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Sorry, I don't know the CLI well enough to translate it directly into PHP.  Can you tell me if you're using one of the APIs listed on this page?
https://developer.cisco.com/site/devnet/home/index.gsp#comboFilters[contenttype]=.apis
0
 
LVL 6

Author Comment

by:mark_06
Comment Utility
Hi, I am using the 8.5(1) Cisco Unified CM AXL Schema Reference if that helps.
0
 
LVL 6

Author Comment

by:mark_06
Comment Utility
This is what I can see is happening on Tomcat traces

2014-03-30 23:52:37,804 INFO  [http-8443-26] servletRouters.AXLAlpha - SOAPAction : CUCM:DB ver=8.5 addLine
2014-03-30 23:52:38,988 INFO  [http-8443-26] servletRouters.AXLAlpha - Going to axis--->
2014-03-30 23:52:38,990 INFO  [http-8443-26] servletRouters.AXLAlpha - Request processed by AXIS
2014-03-30 23:52:38,990 INFO  [http-8443-26] servletRouters.AXLFilter - <?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Action>http://www.w3.org/2005/08/addressing/soap/fault</wsa:Action></soapenv:Header><soapenv:Body><soapenv:Fault><faultcode></faultcode><faultstring>com.ctc.wstx.exc.WstxEOFException: Unexpected EOF in prolog
 at [row,col {unknown-source}]: [1,0]</faultstring><detail /></soapenv:Fault></soapenv:Body></soapenv:Envelope>
2014-03-30 23:52:38,991 INFO  [http-8443-26] servletRouters.AXLFilter - Request 1394096940771 was process in 1194ms

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now