SOAP Request with PHP

Discussion in 'PHP' started by bigmike7801, Aug 10, 2009.

  1. #1
    I am currently trying to work with a 3rd party vendor that requires the use of SOAP requests.

    The vendor's site says:

    I've looked all around at the SOAPClient manual on php.net and just find myself getting more and more confused. Their documentation really lacks commenting sometimes.

    If anybody can get me pointed in the right direction I would really appreciate it. I may not have supplied enough info, so if more info is needed, please let me know!
     
    bigmike7801, Aug 10, 2009 IP
  2. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #2
    you can send xml strings like: <food type="lemon" tasty="10/10"></food>
     
    astrazone, Aug 10, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Double check that you specifically need SOAP and not just an XML post.

    You can use CURL to post XML data to an API, and then parse the response using simplexml or domxml, or another parser, which is significantly easier that SOAP.
     
    jestep, Aug 10, 2009 IP
  4. bigmike7801

    bigmike7801 Well-Known Member

    Messages:
    277
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Here's what I've got so far but it keeps throwing the error of "Unable to handle request without a valid action parameter. Please supply a valid soap action."

    <?php
    
    error_reporting(E_ALL | E_STRICT | E_NOTICE);
    
    $requestString = file_get_contents('soap_data.xml');
    
    echo '<pre>';
    
    try
    {
    	$soap = new SoapClient( 'https://aissreports.acxiom.com/xmlbackgroundtests/xmlresults.asmx?wsdl',
    							array('login'    => 'user',
    								  'password' => 'pass') );
    	
    	echo htmlentities( $soap->__doRequest($requestString, 'https://aissreports.acxiom.com/XMLBackgroundTests/Request.asmx', 'https://aissreports.acxiom.com/XMLBackgroundTests/SubmitRequest', SOAP_1_2) );
    }
    catch (SoapFault $fault)
    {
    	var_dump( htmlentities($fault) );
    }
    catch (Exception $e)
    {
    	var_dump( htmlentities($e) );
    }
    
    echo '</pre>';
    
    ?>
    PHP:
    This is the full response that is output by the browser

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <soap:Body>
        <soap:Fault>
          <faultcode>soap:Client</faultcode>
          <faultstring>Unable to handle request without a valid action parameter. Please supply a valid soap action.</faultstring>
          <detail />
        </soap:Fault>
      </soap:Body>
    </soap:Envelope>
    Code (markup):
     
    bigmike7801, Aug 10, 2009 IP
  5. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #5
    shallowink, Aug 10, 2009 IP
  6. bigmike7801

    bigmike7801 Well-Known Member

    Messages:
    277
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Any chance you could give me an example on how to send the header first with my example code above?

    Thanks for everything!
     
    bigmike7801, Aug 10, 2009 IP
  7. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #7
    Um, best I could would be to pull one of those examples from php.net. but I ain't sure the code you got above is 100% right so odds are its going to be more confusing than helpful.

    First create the header class ....
    
    class authentication_header {
      private $username;
      private $password;
      public function __construct($username,$password) {
         $this->username=$username;
         $this->password=$password;
      }
    }
    Code (markup):
    Actually assign the user/pass to vars ...

    $auth=new authentication_header('myuser','secret');

    Apparently you have to encapsulate the variables (>?>)
    $authvalues=new SoapVar($auth,SOAP_ENC_OBJECT,'authenticate');

    Then you generate the header

    $header=new SoapHeader("myURN",
                               "authenticate",
                               $authvalues,
                               false,
                                "https://aissreports.acxiom.com/XMLBackgroundTests/Request.asmx");
    
    Code (markup):
    And I'm just guessing on the correct URI. The docs from the web service should tell you which is correct.


    $requestString = file_get_contents('soap_data.xml');

    This needs to be put into the parameters, should be array. check the docs for correct way to do it.
    $params = array("fileasstring" =>$requestString);


    $soap = new SoapClient( 'https://aissreports.acxiom.com/xmlbackgroundtests/xmlresults.asmx?wsdl');
    Final part is make the call :
    $soap->__call('SubmitRequest', $params, null, $header);


    Course I haven't used that method so its probably all wrong.
     
    shallowink, Aug 10, 2009 IP
  8. bigmike7801

    bigmike7801 Well-Known Member

    Messages:
    277
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #8
    I'm not sure if I did everything like you stated or in the correct order. Code is below.

    I'm getting the following error when I run the code

    I'm using myuname and mypass for username and password. (wasn't sure where you wanted me to plug them in at).

    <?php
    class authentication_header {
      private $username;
      private $password;
      public function __construct($username,$password) {
         $this->username=$username;
         $this->password=$password;
      }
    }
    $auth=new authentication_header('myuname','mypass');
    $authvalues=new SoapVar($auth,SOAP_ENC_OBJECT,'authenticate');
    
    $header=new SoapHeader("myURN",
    					   "authenticate",
    					   $authvalues,
    					   false,
    						"https://aissreports.acxiom.com/XMLBackgroundTests/Request.asmx");
    
    $params = array("fileasstring" =>$requestString);
    
    
    $soap = new SoapClient( 'https://aissreports.acxiom.com/xmlbackgroundtests/xmlresults.asmx?wsdl');
    
    $soap->__call('SubmitRequest', $params, null, $header);
    
    
    ?>
    PHP:
     
    bigmike7801, Aug 12, 2009 IP
  9. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #9
    Looks right, its just telling us that it doesn't accept the SubmitRequest as a viable function. the company running the webservice should have provided docs for their specific interface with the functions listed. You need to check that.
     
    shallowink, Aug 12, 2009 IP