I need to send the following SOAP request using PHP:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sup="url">
<soapenv:Header/>
<soapenv:Body>
<ControlledEnquiryRequest xmlns="url">
<Authentication>
<SubscriberDetails>
<Username>*****</Username>
<Password>***</Password>
</SubscriberDetails>
</Authentication>
<ControlledRequest>
<Asset>
<Name>Tranquility</Name>
</Asset>
<ParameterList/>
<PrimaryProduct>
<Code>Platinum</Code>
</PrimaryProduct>
</ControlledRequest>
</ControlledEnquiryRequest>
</soapenv:Body>
</soapenv:Envelope>
At the moment I've got:
$requestParams = array(
'CustomerCode' => '*****',
'Password' => '***'
);
$client = new SoapClient('url?wsdl');
$response = $client->ControlledEnquiryRequest($requestParams);
print_r($response);
However I'm getting a 500 error. I'm also unsure on how I pass the other Name and product codes through?
Obviously I'm using the actual URL instead of a 'url'. In Soap UI it's getting a result, but I can't get my head around adapting it to PHP.
Related
I have a working SOAP UI xml, and I have my SOAP request XML and they are almost identical. SOAP UI works, mine gets a null response. Here's the SOAPUI XML first
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:get="http://www.cornerstoneondemand.com/Webservices/GetDetailsWebService">
<soapenv:Header>
<get:AuthHeader>
<get:CorpName>corp</get:CorpName>
<get:UserId>1234</get:UserId>
<get:Signature>ABC123</get:Signature>
</get:AuthHeader>
</soapenv:Header>
<soapenv:Body>
<get:GetDetails xmlns:get="http://www.cornerstoneondemand.com/Webservices/GetDetailsWebService">
<get:object_id>qwerty-123</get:object_id>
</get:GetDetails>
</soapenv:Body>
</soapenv:Envelope>
and here is my PHP code and the request.
$client=new SoapClient($wsdl,array('trace' => 1, 'exception' => 0));
$auth = array(
'CorpName' => $CorpName,
'UserId' => $username,
'Signature' => $Signature
);
$header = new SoapHeader('http://www.cornerstoneondemand.com/Webservices/GetDetailsWebService','AuthHeader',$auth,false);
$client->__setSoapHeaders($header);
$parm[] = new SoapVar($LOid, XSD_STRING, null, null, 'object_id' );
var_dump($client->GetDetails( new SoapVar($parm, SOAP_ENC_OBJECT) )); //output is NULL
//and the PHP request:
print_r($client->__getLastRequest());
output is
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.cornerstoneondemand.com/Webservices/GetDetailsWebService">
<SOAP-ENV:Header>
<ns1:AuthHeader>
<ns1:CorpName>corp</ns1:CorpName>
<ns1:UserId>1234</ns1:UserId>
<ns1:Signature>ABC123</ns1:Signature>
</ns1:AuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:GetDetails>
<object_id>qwerty-123</object_id>
</ns1:GetDetails>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I can't tell if I'm close to creating a good request, or miles off. I'm working to make the PHP request match SOAPUI's since it works and mine doesn't.
The contain the nearly same information. Namespace prefixes for element nodes are exchangeable and optional. So all these 3 variants are resolved to and element node with the local name Envelope in the namespace http://schemas.xmlsoap.org/soap/envelope/.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"/>
You can read the element name as {http://schemas.xmlsoap.org/soap/envelope/}:Envelope.
The same goes for the get vs ns1 namespaces prefixes. They both resolve to the same actual namespace.
But the element object_id has no namespace in your XML. The sixth argument of the SoapVar constructor is the node namespace so you might want to try:
$namespace = 'http://www.cornerstoneondemand.com/Webservices/GetDetailsWebService';
$parm[] = new SoapVar($LOid, XSD_STRING, null, null, 'object_id', $namespace);
This is the first time I work with SoapClient on PHP. My task is to create a script to automatically send a Soap request to the server. The correct request in SOAP UI is:
Soap URL: http://example.com:8181/inventory/soap/inventory-api?wsdl
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:inv="http://example.com:8181/inventory-api/">
<soapenv:Header/>
<soapenv:Body>
<inv:searchStockItemRequest>
<inv:filter>
<inv:ItemID>100</inv:ItemID>
<inv:ItemID>101</inv:ItemID>
<inv:ItemID>102</inv:ItemID>
</inv:filter>
</inv:searchStockItemRequest>
</soapenv:Body>
</soapenv:Envelope>
It means: Search the StockItem with ID is 100 or 101 or 102.
This is my current code.
$xml = '<inv:filter>
<inv:ItemID>100</inv:ItemID>
<inv:ItemID>101</inv:ItemID>
<inv:ItemID>102</inv:ItemID>
</inv:filter>';
$client = new SoapClient(null, array(
'location' => 'http://example.com:8181/inventory/soap/inventory-api?wsdl',
'uri' => MCA_INVENTORY_WSDL)
);
$result = $client->searchItem(htmlspecialchars($xml));
And the result is fault with a message:
"Missing required element {http://example.com:8181/inventory-api/}filter"
I think that the server cannot detect the above filter element. Anyone please help!!!
Soap URL: http://example.com:8181/inventory/soap/inventory-api?wsdl
This is your wsdl address, not your service end point url.
Open above url in web browser. Find this tag <soap:address location> in your wsdl. It should be within <service> tag at end of wsdl. Now replace wsdl url with this end point url.
It should work.
$client = new SoapClient(null, array(
'location' => 'http://example.com:8181/inventory/soap/inventory-api?wsdl',
'uri' => MCA_INVENTORY_WSDL)
I think you have to use a correct data format instead of a XML string. Based on your correct request using SOAP UI, the code using SoapClient should be like the below:
$client = new SoapClient('http://example.com:8181/inventory/soap/inventory-api?wsdl');
$result = $client->searchStockItemRequest(array(
'filter' => array(
'ItemID' => array(100, 101, 102)
)
));
I am not sure the ItemID array is a correct format. You can print the request to check:
echo $client->__getLastRequest();
Don't forget to enable the tracing option:
$client = new SoapClient('...', array('trace' => true));
I have created a soap client in PHP that signs on, but for the second request I want to make I cannot seem to get the PHP to structure the request properly.
This is request that works in SoapUI
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://v1.productapi.gs1ca.org" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<v1:searchProducts>
<sessionId>f53c5450-392e-4ca4-b592-adbb436cfe1f</sessionId>
<searchCriteria>
<v1:AttributeValue>
<v1:attribute>dateupdated</v1:attribute>
<v1:value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">08/01/2013</v1:value>
</v1:AttributeValue>
<v1:AttributeValue>
<v1:attribute>dateupdatedcompare</v1:attribute>
<v1:value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">1</v1:value>
</v1:AttributeValue>
</searchCriteria>
<includeImageAttributes>0</includeImageAttributes>
<sortOrder>dateupdated</sortOrder>
</v1:searchProducts>
</soapenv:Body>
</soapenv:Envelope>
How would I use PHP to format the XML the same way as the working request?
Some progress has been made.
I have been able to recreate the xml up to a point now. The request looks like this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://v1.productapi.gs1ca.org">
<SOAP-ENV:Body>
<ns1:searchProducts>
<sessionId>2a7d0294-8d96-428d-abd8-08add9cfc427</sessionId>
<searchCriteria>
<ns1:AttributeValue>
<ns1:attribute>dateupdated</ns1:attribute>
<ns1:value>01/01/2013</ns1:value>
</ns1:AttributeValue>
<ns1:AttributeValue>
<ns1:attribute>dateupdatedcompare</ns1:attribute>
<ns1:value>1</ns1:value>
</ns1:AttributeValue>
</searchCriteria>
<includeImageAttributes>false</includeImageAttributes>
<sortOrder>dateupdated</sortOrder>
</ns1:searchProducts>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The PHP creating that request is:
$args0 = array(
'sessionid'=>$session,
'searchcriteria'=> array(array('attribute'=>'dateupdated','value'=>'01/01/2013'),array('attribute'=>'dateupdatedcompare','value'=>'1')),
'includeimageattributes'=>0,
'sortorder'=>'dateupdated');
$result = $client->__soapCall('searchProducts',$args0);
The error this throws is:
Error: SoapFault exception: [a:DeserializationFailed] The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://v1.productapi.gs1ca.org:searchCriteria. The InnerException message was 'Element value from namespace http://v1.productapi.gs1ca.org cannot have child contents to be deserialized as an object. Please use XmlNode[] to deserialize this pattern of XML.
I am still missing a portion of the envelope:
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
And I need the value tags to look like this:
<v1:value i:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">
Any ideas on how I can add those portions in?
Okay! I Finally figured this out. It is ugly but it works.
try {
$args = array(
'sessionid'=>$session,
'searchcriteria'=> new SoapVar('<searchCriteria><ns1:AttributeValue>
<ns1:attribute>dateupdated</ns1:attribute>
<ns1:value xsi:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">01/01/2013</ns1:value>
</ns1:AttributeValue>
<ns1:AttributeValue>
<ns1:attribute>dateupdatedcompare</ns1:attribute>
<ns1:value xsi:type="b:string" xmlns:b="http://www.w3.org/2001/XMLSchema">1</ns1:value>
</ns1:AttributeValue></searchCriteria>
', XSD_ANYXML, "http://www.w3.org/2001/XMLSchema-instance"),
'includeimageattributes'=>0,
'sortorder'=>'dateupdated');
$result = $client->__soapCall('searchProducts',$args);
} catch (SoapFault $e) {
echo "Error: {$e}";
}
i am new to XMl. i want to extract the status values in the following xml .i have no idea how to do that in php . this is a response that i ma getting from a API call.
<soapenv:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:body>
<savesalesorderresponse xmlns="http://www.smartturn.com/services/OccamService/sales-order">
<uploadresponse>
<ns1:externalrefid xmlns:ns1="http://www.smartturn.com/services/occamtypes">007</ns1:externalrefid>
<ns2:status xmlns:ns2="http://www.smartturn.com/services/occamtypes">SUCCESS</ns2:status>
<ns6:systemid xmlns:ns6="http://www.smartturn.com/services/occamtypes">SO-059241</ns6:systemid>
</uploadresponse>
</savesalesorderresponse>
</soapenv:body>
</soapenv:envelope>
solution code will be appreciated
thanks in advance
All you need to do is Register namespaces with registerXPathNamespace
$xml = new SimpleXMLElement($data);
$xml->registerXPathNamespace("ns", "http://www.smartturn.com/services/occamtypes");
$status = $xml->xpath('//ns:status');
$status = (string) $status[0];
print($status);
Output
SUCCESS
The easiest way is to use SimpleXML
I have the follwing string:
$string='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bac="http://backupbank.com/"> <soapenv:Header/> <soapenv:Body>
<bac:CreateUser>
<bac:request>
<!--Optional:-->
<bac:username>anandneema</bac:username>
<bac:product>Workstation</bac:product>
<bac:credentials>
<bac:server>serveripaddress</bac:server>
<bac:user>username</bac:user>
<bac:password>password</bac:password>
</bac:credentials>
</bac:request>
</bac:CreateUser> </soapenv:Body> </soapenv:Envelope>';
$xmldata=simplexml_load_string($string);
echo "<pre>"; print_r($xmldata);
It is not parsing the data.
But when I use:
$string='<soapenv:Envelope> <soapenv:Header/> <soapenv:Body>
<bac:CreateUser>
<bac:request>
<!--Optional:-->
<bac:username>anandneema</bac:username>
<bac:product>Workstation</bac:product>
<bac:credentials>
<bac:server>serveripaddress</bac:server>
<bac:user>username</bac:user>
<bac:password>password</bac:password>
</bac:credentials>
</bac:request>
</bac:CreateUser> </soapenv:Body> </soapenv:Envelope>';
$xmldata=simplexml_load_string($string); echo "<pre>"; print_r($xmldata);
Actually when I am removing the attributes: xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:bac="http://backupbank.com/"
Then it parses the data
Can any one suggest to me what the problem might be?
Don't have much idea on this but try calling the service by properly format the xml as string then use CURL may be something like in this quesion
Handling web service errors within PHP