在PHP中发出soap请求。传递令牌

时间:2014-02-26 05:38:02

标签: php soap

我使用以下内容:http://www.ukverify.co.uk/webservice.asmx

我使用以下方法创建令牌:

<?php

$wsdl = "http://www.ukverify.co.uk/webservice.asmx?wsdl";

$client = new SoapClient($wsdl,
array(
    "trace"      => 1,  // enable trace to view what is happening
    "exceptions" => 1,  // disable exceptions
    "cache_wsdl" => 0)   // disable any caching on the wsdl, encase you alter the wsdl server
);

$username='myusername';
$password='mypassword';
$token='';
$ns = 'http://ukverify.co.uk/';

$client = new SoapClient("http://www.ukverify.co.uk/webservice.asmx?wsdl",
    array(
    "trace"      => 1,  // enable trace to view what is happening
    "exceptions" => 1,  // disable exceptions
    "cache_wsdl" => 0)   // disable any caching on the wsdl, encase you alter the wsdl server
);

$headerbody = array('Username'=>$username,'Password'=>$password);
$header = new SoapHeader($ns,'WebServiceHeader',$headerbody);
$client->__setSoapHeaders($header);

$apiToken = $client->AuthenticateUser()->AuthenticateUserResult;
?>

我现在正在尝试使用BSearch。我现在必须在标题中使用令牌,并在正文中使用pcode。尝试不同的语法猜测后,我能得到的最接近的是:

$wsdl = "http://www.ukverify.co.uk/webservice.asmx?wsdl";

$client = new SoapClient($wsdl,
array(
    "trace"      => 1,  // enable trace to view what is happening
    "exceptions" => 1,  // disable exceptions
    "cache_wsdl" => 0)   // disable any caching on the wsdl, encase you alter the wsdl server
);

$username='myusername';
$password='mypassword';
$token='';
$ns = 'http://ukverify.co.uk/';

$client = new SoapClient("http://www.ukverify.co.uk/webservice.asmx?wsdl");

$headerbody = array('Username'=>$username,'Password'=>$password);
$header = new SoapHeader($ns,'WebServiceHeader',$headerbody);
$client->__setSoapHeaders($header);

$apiToken = $client->AuthenticateUser()->AuthenticateUserResult;

//echo $apiToken;

// Setup the SOAP Header
$authHeader = array('AuthenticatedToken'=>$apiToken);
$header = new SoapHeader($ns,
    'WebServiceHeader',
    $authHeader, false
);

$client->__setSoapHeaders($header);

$params = array('pcode' => 'sw15 2au');

$response = $client->__soapCall('BSearch',array("parameters" => array('pcode'=> "sw15 2au")));
 // $response = $client->__soapCall('BSearch',array("parameters" => array('pcode'=> $params)));
//$response = $client->AddressFill($params);

var_dump($response);

class AuthHeader
{
    var $APIToken;

    function __construct()
    {

    }
}

错误是:

Fatal error: Uncaught SoapFault exception: [soap:Server] Server was unable to process request. ---> Object reference not set to an instance of an object. in /home/wilseayd/public_html/idChecka/hanson3.php:40 Stack trace: #0 /home/wilseayd/public_html/idChecka/hanson3.php(40): SoapClient->__soapCall('BSearch', Array) #1 {main} thrown in /home/wilseayd/public_html/idChecka/hanson3.php on line 40

任何人都可以正确地委托PHP代码吗?

获得结果:

<?php

$client = new SoapClient('http://www.ukverify.co.uk/webservice.asmx?wsdl');
$username='xxx';
$password='xxx';
$token='';
$ns = 'http://ukverify.co.uk/';

$headerbody = array('Username'=>$username,'Password'=>$password);
$header = new SoapHeader($ns,'WebServiceHeader',$headerbody);
$client->__setSoapHeaders($header);

$apiToken = $client->AuthenticateUser()->AuthenticateUserResult;
$_SESSION['apiToken']= $apiToken;


// Do the above once

// Reuse the below 

$headerbody = array('AuthenticatedToken' => $apiToken); 
$header = new SoapHeader($ns,'WebServiceHeader',$headerbody);

$params = array('firstname' => "",'surname' => "warburton",'pafbldg' =>"",'pafpremno' => "",
'pafstreet' => "",'pafarea' => "",'town' => "",'pcode' => 'OL6 9rs');

$client = new SoapClient('http://www.ukverify.co.uk/webservice.asmx?wsdl');

$client->__setSoapHeaders($header);


$results = $client->BSearch($params);
var_dump($results);



?>

现在的问题是如何使用返回的数据。理想情况下,我需要它以JSON格式,所以我可以使用移动应用程序中的数据。

结果是(查看输出页面的源代码):

object(stdClass)#1 (1) {
    ["BSearchResult"]=>
    object(stdClass)#2 (2) {
        ["schema"]=>
            string(1065) "<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="xyzzy"><xs:element name="xyzzy" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="xyzzy"><xs:complexType><xs:sequence><xs:element name="title" type="xs:string" minOccurs="0"/><xs:element name="firstname" type="xs:string" minOccurs="0"/><xs:element name="initial" type="xs:string" minOccurs="0"/><xs:element name="surname" type="xs:string" minOccurs="0"/><xs:element name="dob" type="xs:string" minOccurs="0"/><xs:element name="pafpremno" type="xs:string" minOccurs="0"/><xs:element name="pafbldg" type="xs:string" minOccurs="0"/><xs:element name="pafstreet" type="xs:string" minOccurs="0"/><xs:element name="town" type="xs:string" minOccurs="0"/><xs:element name="county" type="xs:string" minOccurs="0"/><xs:element name="pcode" type="xs:string" minOccurs="0"/></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema>"
        ["any"]=>
            string(1129) "<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><xyzzy xmlns=""><xyzzy diffgr:id="xyzzy1" msdata:rowOrder="0"><title>Ms</title><firstname>xxxx</firstname><initial xml:space="preserve"> </initial><surname>xxxxx</surname><dob>45 to 54</dob><pafpremno>29</pafpremno><pafbldg/><pafstreet>Mossley Road</pafstreet><town>Ashton-Under-Lyne</town><county>Lancashire</county><pcode>OL6 9RS</pcode></xyzzy><xyzzy diffgr:id="xyzzy2" msdata:rowOrder="1"><title>Mr</title><firstname>xxx</firstname><initial>J</initial><surname>xxx</surname><dob>45 to 54</dob><pafpremno>29</pafpremno><pafbldg/><pafstreet>Mossley Road</pafstreet><town>Ashton-Under-Lyne</town><county>Lancashire</county><pcode>OL6 9RS</pcode></xyzzy><xyzzy diffgr:id="xyzzy3" msdata:rowOrder="2"><title>Mr</title><firstname>xxxx</firstname><initial>D</initial><surname>xxxx</surname><dob>18 to 24</dob><pafpremno>29</pafpremno><pafstreet>Mossley Road</pafstreet><town>Ashton-Under-Lyne</town><county>Lancashire</county><pcode>OL6 9RS</pcode></xyzzy></xyzzy></diffgr:diffgram>"
    }
}

Heres Hopin。

Warby先生

0 个答案:

没有答案