Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP SOAP XML Post

Posted on 2014-03-24
12
Medium Priority
?
2,836 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39950286
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 111

Expert Comment

by:Ray Paseur
ID: 39950329
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
ID: 39956321
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
How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39956361
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
ID: 39958486
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
ID: 39958552
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
 
LVL 6

Author Comment

by:mark_06
ID: 39958644
Can't for the life of me seem to get the XML to Post to the server
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39958667
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
ID: 39958852
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 111

Expert Comment

by:Ray Paseur
ID: 39958973
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
ID: 39964704
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
ID: 39964849
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

670 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