PHP SOAP client help! - php

I have to create a PHP SOAP client that sends leads, but I have never worked with SOAP before, and my XML isn't that great, here is the code I have managed to write so far. . .
<?php
try {
$client = new SoapClient(null, array(
'location' => "https://wwa.website.co.za/CallmasterTes/LeadService.asmx",
'uri' => "urn:Website:Callmaster:InTuch/CreateLead",
'login' => "username",
'password' => "password"
));
$sysName = ' ';
$clientCode = ' ';
$expTimestamp = ' ';
$schedTimestamp = ' ';
$client->CreateLead("ExternalLead",
new SoapParam($sysName, "BusinessSystemName"),
new SoapParam($clientCode, "BusinessSystemClientCode"),
new SoapParam($_POST['Title'], "Title"),
new SoapParam($_POST['FirstName'], "FirstName"),
new SoapParam($_POST['Surname'], "Surname"),
new SoapParam($_POST['IdNumber'], "IdNumber"),
new SoapParam($_POST['Gender'], "Gender"),
new SoapParam($_POST['DateOfBirth'], "DateOfBirth"),
new SoapParam($_POST['Language'], "Language"),
new SoapParam($_POST['EmailAddress'], "EmailAddress"),
new SoapParam($_POST['HomeTelNumber'], "HomeTelNumber"),
new SoapParam($_POST['BusinessTelNumber'], "BusinessTelNumber"),
new SoapParam($_POST['MobileTelNumber'], "MobileTelNumber"),
new SoapParam($_POST['OtherTelNumber'], "OtherTelNumber"),
new SoapParam($_POST['PreferredTelNumberCode'], "PreferredTelNumberCode"),
new SoapParam($_POST['CampaignName'], "CampaignName"),
new SoapParam($_POST['ProductName'], "ProductName"),
new SoapParam($_POST['Comments'], "Comments"),
new SoapParam($expTimestamp, "ExpiryTimestamp"),
new SoapParam($schedTimestamp, "ScheduledTimestamp"),
);
}
catch (SoapFault $fault) {
trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
}
?>
The following is a sample SOAP 1.2 request and response that I was given by the developers on the server side of things.
POST /CallmasterTest/LeadService.asmx HTTP/1.1
Host: wwa.website.co.za
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<CreateLead xmlns="urn:Website:Callmaster:InTuch">
<Lead>
<BusinessSystemName>string</BusinessSystemName>
<BusinessSystemClientCode>int</BusinessSystemClientCode>
<Title>Mr or Ms or Mrs or Miss...etc</Title>
<FirstName>string</FirstName>
<Surname>string</Surname>
<IdNumber>string</IdNumber>
<Gender>Male or Female</Gender>
<DateOfBirth>date</DateOfBirth>
<Language>string</Language>
<EmailAddress>string</EmailAddress>
<HomeTelNumber>string</HomeTelNumber>
<BusinessTelNumber>string</BusinessTelNumber>
<MobileTelNumber>string</MobileTelNumber>
<OtherTelNumber>string</OtherTelNumber>
<PreferredTelNumberCode>Business or Home or Mobile or Other</PreferredTelNumberCode>
<CampaignName>string</CampaignName>
<ProductName>string</ProductName>
<Comments>string</Comments>
<ExpiryTimestamp>dateTime</ExpiryTimestamp>
<ScheduledTimestamp>dateTime</ScheduledTimestamp>
</Lead>
</CreateLead>
</soap12:Body>
</soap12:Envelope>
The response:
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<CreateLeadResponse xmlns="urn:Website:Callmaster:InTuch">
<CreateLeadResult>
<Success>boolean</Success>
<StatusMessage>string</StatusMessage>
<LeadGuid>guid</LeadGuid>
<Errors>
<LeadError ErrorLevel="Information or Warning or Error">
<Message>string</Message>
<Field>string</Field>
</LeadError>
<LeadError ErrorLevel="Information or Warning or Error">
<Message>string</Message>
<Field>string</Field>
</LeadError>
</Errors>
</CreateLeadResult>
</CreateLeadResponse>
</soap12:Body>
</soap12:Envelope>
I have really tried everything, and googled into high heaven, but I just can't get it to work, I am sure that it's probably something small that I am missing, I would really appreciate the help, thanx!

I'm not sure if this is a copy/paste error or not, but all the _POSTs should be $_POSTs in your first code example.

Related

PHP function SOAP 1.2

I need some help to execute a SOAP request with PHP.
For example, a method for search a publisher:
POST /webservice/search.asmx HTTP/1.1
Host: www.host.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://host.com/HostNET/" xmlns:types="http://host.com/HostNET/encodedTypes" xmlns:rpc="http://www.w3.org/2003/05/soap-rpc" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body soap12:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<tns:findPublishers>
<sessionToken xsi:type="xsd:string">string</sessionToken>
<expression soapenc:id="id0" xsi:type="tns:SearchExpression">
<expression xsi:type="xsd:string">string</expression>
<phonetic xsi:type="xsd:boolean">boolean</phonetic>
<inflections xsi:type="xsd:boolean">boolean</inflections>
</expression>
<distributeurs xsi:type="xsd:boolean">boolean</distributeurs>
</tns:findPublishers>
</soap12:Body>
</soap12:Envelope>
I have the sessionToken but I don't understand what I have to do with the line <expression soapenc:id="id0" xsi:type="tns:SearchExpression"> and it's contents.
try{
$search = new SoapClient("http://www.host.com/WebService/search.asmx?wsdl", array("soap_version" => SOAP_1_2));
$search->findPublishers($sessionToken,???????,FALSE);
}catch (SoapFault $e){
print_r($e);
}

PHP SoapClient - action in the wrong place

I'm using PHP SoapClient, and am running into an issue with getting the request format just as a third party wants it.
They want it like this:
POST /service.asmx HTTP/1.1
Host: service.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<Order xmlns="http://someservice">
<json>string</json>
</Order>
</soap12:Body>
</soap12:Envelope>
However, the closest I can seem to get it, using SoapClient, is like this:
POST /service.asmx HTTP/1.1
Host: service.com
Content-Type: application/soap+xml; charset=utf-8; action="http://someservice"
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<Order>
<json>string</json>
</Order>
</soap12:Body>
</soap12:Envelope>
Notice how the action in my request is in the http header, and the action in their ideal format is in the Order tag. The thing is, it's SoapClient that is generating where that action is being placed - it gets that specific url from the WSDL, it's not even in my code.
How do I tell SoapClient to put it in the right spot? For my part, trying to only include what's necessary, this is essentially the code:
$this->client = new SoapClient($this->wsdl, array(
'soap_version' => SOAP_1_2,
'encoding' => 'UTF-8',
'stream_context' => stream_context_create($context),
'trace' => true,
'exceptions' => true,
)
);
$json = json_encode($request);
// Prepare the xml
$xml = array();
$xml[] = new SoapVar($json, XSD_STRING, 'string', 'http://www.w3.org/2001/XMLSchema', 'json');
$this->finalXML = new SoapVar($xml, SOAP_ENC_OBJECT, null, null, 'Order');
$this->response = $this->client->CreateOrder($this->finalXML);
After days of banging my head, I found this and ended up making the accepted, hacky solution work for me:
Change SOAP request format
Apparently, when you're working with a poorly written Soap API, there's not a lot you can do.

php soap client request

How to send PHP SOAP Request to .NET Web Services, I have already tried soapclient(); class but something wrong..... any body please help me.
POST /KH/TestKH/WebServices.asmx HTTP/1.1
Host: www.host.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/SearchMethod"
<?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="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<SearchMethod xmlns="http://tempuri.org/">
<_hap>string</_hap>
<_hapType>string</_hapType>
<UserID>string</UserID>
<Password>string</Password>
<Origin>string</Origin>
<Destination>string</Destination>
<FromDate>string</FromDate>
<ToDate>string</ToDate>
<Class>string</Class>
<Airlines>string</Airlines>
<Adults>int</Adults>
<Childs>int</Childs>
<Infants>int</Infants>
<isReturn>boolean</isReturn>
<CompanyCode>string</CompanyCode>
<sessionId>string</sessionId>
</SearchMethod>
</soap:Body>
</soap:Envelope>
OUR CODE:
$options["soap_version"]=SOAP_1_1;
$options["trace"]=true;
$options["exceptions"]=0;
$options["UserID"]="xxxxx";
$options["Password"]="xxxxx";
$client = new SoapClient("http://www.HOST.com/KH/TestKH/WebServices.asmx?WSDL",$options);
//var_dump($client->__getTypes());
$parameters= new stdClass();
$parameters->_hap="xxxxxxxxx";
$parameters->_hapType="TEST";
//$parameters->UserID="";
//$parameters->Password="";
$parameters->Origin="AUS";
$parameters->Destination="LHR";
$parameters->FromDate="01/01/2016";
$parameters->ToDate="27/02/2016";
$parameters->Class="X";
$parameters->Airlines="";
$parameters->Adults="1";
$parameters->Childs="0";
$parameters->Infants="0";
$parameters->isReturn=true;
$parameters->CompanyCode="xxxxx";
$parameters->sessionId="xxxxxx";
$result=$client->SearchMethod($parameters);
var_dump($result);
var_dump($client->__getLastRequest());
$status=$result->SearchMethodResult;
var_dump($status);
}
catch (Exception $e){
$e -> getMessage();
var_dump($e);
}
Our request process but don't know what is the wrong with these code because show server side error "Unknown error occured!"

How to create SOAP 1.2 request

I need help with creating SOAP 1.2 request. All i have is this:
SAMPLE REQUEST:
POST /WS/PriceList.asmx HTTP/1.1
Host: gateway.systemb2b.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetProducts xmlns="http://gateway.systemb2b.com/schemas/Product" />
</soap12:Body>
</soap12:Envelope>
SAMPLE RESPONSE:
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetProductsResponse xmlns="http://gateway.systemb2b.com/schemas/Product">
<GetProductsResult>xml</GetProductsResult>
</GetProductsResponse>
</soap12:Body>
</soap12:Envelope>
Thanks a lot.
<?php
$client = new SoapClient("URL/OF/YOUR/WSDL", array('soap_version' => SOAP_1_2));
$result = $client('GetProducts');
?>
I hope this example would help you to get your issue solved. All the best!
// SOAP 1.2 client
$options = array('soap_version'=>SOAP_1_2, 'exceptions'=>true, 'trace'=>1);
$client = new SoapClient('https://www.example.com/example.svc/SSL?wsdl', $options);
$actionHeader = new SoapHeader('http://www.w3.org/2005/08/addressing','Action','http://tempuri.org/xxxx/GetResult');
$toActionHeader = new SoapHeader('http://www.w3.org/2005/08/addressing','To','https://www.example.com/example.svc/SSL?wsdl');
$headerbody = array('Action' => $actionHeader,'To' => $toActionHeader);
$client->__setSoapHeaders($headerbody);
$client->__setLocation('https://www.example.com/example.svc/SSL');
$params = array( "param1"=>"abc", "param2"=>"ab123",c"param3"=>"ab1111" );
$client->__soapCall("GetResult", array($params));

PHP Soap header

I am trying to set Soap header for the WSDL and wonder if the following code is correct.
I try to pass all 3 variables into Soap header.
Any pointer would be much appreciated.
Thanks
PHP CODE:
try{
$soap_client = new SoapClient('https://mywebservice.com/ws.php?wsdl');
//Body of the Soap Header.
$soapheader = $this->getMRSoapHeader("PortalOnlineInventorySearch");
$headerbody = array('Timestamp' => $soapheader['Timestamp'],
'Signature' => $soapheader['Signature'],
'AccessID' => $soapheader['AccessID']);
//Create Soap Header.
$header = new SOAPHeader($this->ns, 'WebServiceSoapHeader', $headerbody);
//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header);
$searchResult = $soap_client->PortalOnlineInventorySearch($meetingRoomOnlineSearchCriteria);
}catch (Exception $e){
error_log("Error getting Search Results from Titan" + $e->getMessage());
var_dump("Error performing criteria search" . $e->getMessage());
}
WSDL:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Header>
<WebServiceSoapHeader xmlns="https://mywebservice.com/ws.php?wsdl">
<Signature>string</Signature>
<AccessID>string</AccessID>
<Timestamp>string</Timestamp>
</WebServiceSoapHeader>
</soap12:Header>
<soap12:Body>
<PortalOnlineInventorySearch xmlns="https://mywebservice.com/ws.php">
<searchCriteria>
<keyword>int</keyword>
<ReturnServices>boolean</ReturnServices>
</searchCriteria>
</PortalOnlineInventorySearch>
</soap12:Body>
</soap12:Envelope>

Categories