PHP SOAP XML Post

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.
LVL 6
mark_06Asked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
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
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
mark_06Author Commented:
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
 
Ray PaseurCommented:
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
 
mark_06Author Commented:
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
 
mark_06Author Commented:
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
 
mark_06Author Commented:
Can't for the life of me seem to get the XML to Post to the server
0
 
mark_06Author Commented:
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
 
Ray PaseurCommented:
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
 
mark_06Author Commented:
Hi, I am using the 8.5(1) Cisco Unified CM AXL Schema Reference if that helps.
0
 
mark_06Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.