English 中文(简体)
PHP SoapClient request
原标题:

I m trying to send a SOAP request to a newsletter service using this WSDL.

Here s my PHP:

$client = new SoapClient($wsdl_url, array(
     login  =>  myusername ,
     password  =>  mypassword ,
     trace  => true
));

$client->AddSubscriber(
    new SoapParam( MyFirstName ,  FirstName ),
    new SoapParam( MyLastName ,  LastName ),
    new SoapParam( myemail@someaddress.com ,  Email )
);

I m getting the exception:

End element  Body  from namespace  schemas.xmlsoap.org/soap/envelope/  expected. Found element  LastName  from namespace   . Line 2, position 156.

Here s what the service is expecting for AddSubscriber:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AuthHeader xmlns="admin.ekeryx.com">
      <Username>string</Username>
      <Password>string</Password>
      <AccountID>string</AccountID>
    </AuthHeader>
  </soap:Header>
  <soap:Body>
    <AddSubscriber xmlns="admin.ekeryx.com">
      <subscriber>
        <ID>string</ID>
        <FirstName>string</FirstName>
        <LastName>string</LastName>
        <Email>string</Email>
      </subscriber>
      <overwritable>boolean</overwritable>
    </AddSubscriber>
  </soap:Body>
</soap:Envelope>

Here s what s being sent:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="tempuri.org/">
    <SOAP-ENV:Body>
        <ns1:AddSubscriber/>
            <LastName>MyLastName</LastName>
            <Email>myemail@someaddress.com</Email>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I m not very familar with SOAP, and I ve been looking for documentation all over the place, but I can t seem to find a very good reference for what I m doing.

Any guidance would be very much appreciated!


Thanks. Could you give me an example? I m looking at the example on the PHP site that shows:

<?php
class SOAPStruct {
    function SOAPStruct($s, $i, $f) 
    {
        $this->varString = $s;
        $this->varInt = $i;
        $this->varFloat = $f;
    }
}
$client = new SoapClient(null, array( location  => "http://localhost/soap.php",
                                      uri       => "http://test-uri/"));
$struct = new SOAPStruct( arg , 34, 325.325);
$soapstruct = new SoapVar($struct, SOAP_ENC_OBJECT, "SOAPStruct", "http://soapinterop.org/xsd");
$client->echoStruct(new SoapParam($soapstruct, "inputStruct"));
?>

Are you saying I would have to create a Subscriber PHP class, assign all the vars $this->FirstName = $first_name, etc... and then put it in a SoapVar with encoding SOAP_ENC_OBJECT? How can I better represent the subscriber structure?

问题回答

There are two possible Params subscriber and overwriteable

<soap:Body>
<AddSubscriber xmlns="admin.ekeryx.com">
  <subscriber>
    <ID>string</ID>
    <FirstName>string</FirstName>
    <LastName>string</LastName>
    <Email>string</Email>
  </subscriber>
  <overwritable>boolean</overwritable>
</AddSubscriber>

So you need to do some more complex construction by using SoapVar to represent the subsrciber structure.

http://www.php.net/manual/en/soapvar.soapvar.php

Should look something like this i think, although youll want to check the XSD against the Soap:Body produced...

$subscriber = new StdClass();
$subscriber->ID =  myid ;
$subscriber->FirstName =  First ;
$subscriber->LastName =  Last ;

$subscriber = new SoapParam(new SoapVar($subscriber, SOAP_ENC_OBJECT, $type, $xsd),  subscriber );

$type should be the type in the XSD/WSDL defeinition for the api and $xsd is the URI for the XSD.

I think that should do it but Ive only used native PHP libs once for EBay (it was a nightmare haha) and that was almost 2 years ago so im a little rusty.

I came across this while looking for a similar answer myself. I believe the issue you are having is that the following code will pass the login and password in the HTTP header not the SOAP header

$client = new SoapClient($wsdl_url, array(
     login  =>  myusername ,
     password  =>  mypassword ,
     trace  => true
));

I believe this is what you are looking for

$headerbody = array( Token  => $someToken, 
                     Version  => $someVersion, 
                     MerchantID =>$someMerchantId, 
                       UserCredentials =>array( UserID =>$UserID, 
                                              Password =>$Pwd)); 




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签