I need help with this webservice, it is returning this stdClass Object ( [GETOfertasAereasResult] => )
I need to return an array with all the values.
<?php
try {
$wsdl_url = 'http://portaldoagente.com.br/wsonlinetravel/funcoes.asmx?WSDL';
$client = new SOAPClient($wsdl_url);
$params = array(
'sLojaChave' => "Y2Y4ZGRkOWU=",
);
$return = $client->GETOfertasAereas($params);
print_r($return);
} catch (Exception $e) {
echo "Exception occured: " . $e;
}
?>
Assuming your data is like the following:
$return = '<?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>
<GETOfertasAereasResponse xmlns="http://tempuri.org/">
<GETOfertasAereasResult>Some result content</GETOfertasAereasResult>
</GETOfertasAereasResponse>
</soap12:Body>
</soap12:Envelope>';
Try this:
$obj = new SimpleXMLElement($return);
$body = $obj->children('soap12', true)->Body->children();
$content = (string) $body->GETOfertasAereasResponse->GETOfertasAereasResult;
var_dump($content);
Related
As I haven't found a solution for testing my SOAP integration, I have decided to make my own soap requests through POST method. The method sends an XML response which works fine, but I want to make my own array-to-xml parser. So far my code is:
private function parseData($xml, $data) {
foreach ($data as $key => $row) {
var_dump($key, $row);
if (is_array($row) && count($row)) {
return $this->parseData($xml, $row);
}
$xml->addChild($key, $row);
}
return $xml;
}
private function toWSDL($call, $data) {
$root = '<data/>';
$xml = new \SimpleXMLElement($root);
$xml = $this->parseData($xml, $data);
$xmlBody = $xml->asXML();
$open = '<ns1:' . $call . '>';
$close = '</ns1:' . $call . '>';
$xmlBody = str_replace('<data>', $open, $xmlBody);
$xmlBody = str_replace('</data>', $close, $xmlBody);
$xmlBody = str_replace('<?xml version="1.0"?>', '', $xmlBody);
$request_data = <<<EOF
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="https://api.hostedshop.io/service.php">
<SOAP-ENV:Body>
$xmlBody
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
EOF;
return $request_data;
}
but it does not work for a soap function called "Product_Update", which takes in an array like so:
$this->call("Product_Update", [
"ProductData" => [
"Id" => 1,
"Stock" => 2
]
]);
The "ProductData" key with an array does not work with my parseData function, which is why I am writing for help.
I have the following code and I have been working to try to get this working.
<?php declare(strict_types=1);
$session_token = '?'; $xml = '';
$result = '<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://ws.careerbuilder.com/resumes/"><Packet><Errors /><SessionToken>3msk323msd-3312-CQ-2</SessionToken></Packet></string>
';
if ($result) {
$xml = simplexml_load_string($result);
print_r($xml);
if ($xml !== false) {
$session_token = $xml->SessionToken;
echo PHP_EOL.'Session: '. $session_token;
} else {
echo 'Error: XML does NOT appear to be valid';
}
} else
echo 'Error: result does NOT appear be valid';
The problem is no matter what I'm not able to extract the <SessionToken> value from the XML. When I use print_r() I get the following:
SimpleXMLElement Object
(
[0] => <Packet><Errors /><SessionToken>3msk323msd-3312-CQ-2</SessionToken></Packet>
)
Your input is entity-encoded. If this is really what it looks like, you'll need to decode it first:
$xml = simplexml_load_string(html_entity_decode($result));
$token = (string) $xml->Packet->SessionToken[0];
You document contains nested XML. The text content of the string element is serialized XML. So you need to parse it after reading it.
$result = '<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://ws.careerbuilder.com/resumes/"><Packet><Errors /><SessionToken>3msk323msd-3312-CQ-2</SessionToken></Packet></string>
';
$string = new SimpleXMLElement($result);
$packet = new SimpleXMLElement((string)$string);
var_dump($packet);
Output:
object(SimpleXMLElement)#2 (2) {
["Errors"]=>
object(SimpleXMLElement)#3 (0) {
}
["SessionToken"]=>
string(20) "3msk323msd-3312-CQ-2"
}
I am trying to send a PHP SOAP request that has multiple stdclass objects. I cannot figure out how to send so the xml is properly formatted. The xml structure should look like the following (from SOAPui).
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ass="http://assignment.soap.assignshare"
xmlns:xsd="http://request.assignment.soap.assignshare/xsd"
xmlns:xsd1="http://datamodel.assignment.soap.assignshare/xsd">
<soapenv:Header/>
<soapenv:Body>
<ass:SetStaffOnDutyStatus>
<ass:SasPubSetStaffOnDutyStatus>
<xsd:Source>VendorC</xsd:Source>
<xsd:Dest>?</xsd:Dest>
<xsd:PubEventMsgTag>1</xsd:PubEventMsgTag>
<xsd:EventSetStaffOnDutyStatus>
<xsd:Timestamp>Thurs,03 Nov 2016,20:20:00 GMT</xsd:Timestamp>
<xsd:EventAction>Set</xsd:EventAction>
<xsd:Staff>
<xsd1:StaffID>10</xsd1:StaffID>
<xsd1:Last>Doe</xsd1:Last>
<xsd1:First>John</xsd1:First>
<xsd1:Middle>?</xsd1:Middle>
<xsd1:Role>?</xsd1:Role>
</xsd:Staff>
<xsd:Team>
<xsd1:TeamID>319</xsd1:TeamID>
<xsd1:TeamName>5 East</xsd1:TeamName>
</xsd:Team>
</xsd:EventSetStaffOnDutyStatus>
</ass:SasPubSetStaffOnDutyStatus>
</ass:SetStaffOnDutyStatus>
</soapenv:Body>
</soapenv:Envelope>
My PHP file is
<?php
$a = new StdClass();
$a->SasPubSetStaffOnDutyStatus = new StdClass();
$a->SasPubSetStaffOnDutyStatus->Source = 'Demo';
$a->SasPubSetStaffOnDutyStatus->Dest = 'http://192.168.50.3:26001/SAIWebService';
$a->SasPubSetStaffOnDutyStatus->PubEventMsgTag = 1;
$a->SasPubSetStaffOnDutyStatus->EventSetStaffOnDutyStatus = new StdClass($b);
$b = new StdClass();
$b->EventSetStaffOnDutyStatus = new StdClass();
$b->EventSetStaffOnDutyStatus->Timestamp = 'Thurs,03 Nov 2016,20:20:00 GMT';
$b->EventSetStaffOnDutyStatus->EventAction = 'Set';
$c = new StdClass();
$c->Staff = new StdClass();
$c->Staff->StaffID = 10;
$c->Staff->Last = 'Harry';
$c->Staff->First = 'Potter';
$c->Staff->Middle = '?';
$c->Staff->Role = '?';
$d = new StdClass();
$d->Team = new StdClass();
$d->Team->TeamID = 319;
$d->Team->TeamName = '5 East';
$d = (object)array_merge ((array)$b, (array)$c, (array)$d);
$e = (object)array_merge((array)$a, (array)$d);
$wsdl = "http://192.xxx.xx.x:26001/SAIWebservice?singlewsdl";
$client = new SoapClient($wsdl, array(
"trace"=>1,
"exceptions"=>0));
$values = $client->SetStaffOnDutyStatus($e);
echo "REQUEST HEADERS:\n" . $client->__getLastRequestHeaders() . "\n";
$xml = $values->SasPubRespSetStaffOnDutyStatus;
print "<pre>\n";
print_r($xml);
print "</pre>";
The output of the PHP file does not include the values of objects $b, $c & $d. Appreciate any guidance on what I'm doing wrong to merge the objects. Output is below.
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://request.assignment.soap.assignshare/xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns2="http://assignment.soap.assignshare">
<SOAP-ENV:Body>
<ns2:SetStaffOnDutyStatus>
<ns2:SasPubSetStaffOnDutyStatus>
<ns1:Source>Demo</ns1:Source>
<ns1:Dest>http://192.168.50.3:26001/SAIWebService</ns1:Dest>
<ns1:PubEventMsgTag>1</ns1:PubEventMsgTag>
<ns1:EventSetStaffOnDutyStatus>
<ns1:Timestamp xsi:nil="true"/>
<ns1:EventAction xsi:nil="true"/>
<ns1:Staff xsi:nil="true"/>
<ns1:Team xsi:nil="true"/>
</ns1:EventSetStaffOnDutyStatus>
</ns2:SasPubSetStaffOnDutyStatus>
</ns2:SetStaffOnDutyStatus>
</SOAP-ENV:Body></SOAP-ENV:Envelope>
Fatal error: Call to a member function registerXPathNamespace() on a non-object in /home/gateway/public_html/index.php on line 83
So the error I am getting is above, and on that line is the following
$host = "services.incard.com.au/telechoicetransservice.asmx";
$timestamp = getGMTtimestamp();
$vars =
"<?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>" .
"<ProcessPayment xmlns=\"https://services.incard.com.au\">".
"<auth>" .
"<AccountCode>". urlencode($_POST['AccountCode'])."</AccountCode>".
"<Username>". urlencode($_POST['AccountCode'])."</Username>".
"<Password>". urlencode($_POST['AccountCode'])."</Password>".
"</auth>" .
"<MerchantNumber>".urlencode($_POST['MerchantNumber'])."</MerchantNumber>" .
"<CustomerNumber>".urlencode($_POST['CustomerNumber'])."</CustomerNumber>".
"<Amount>".urlencode($_POST['Amount'])."</Amount>".
"<Description>".urlencode($_POST['Description'])."</Description>".
"</ProcessPayment>".
"</soap:Body></soap:Envelope>";
$response = openSocket($host, $vars);
$xmlres = array();
$xmlres = makeXMLTree ($response);
$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('soap:Envelope', 'http://schemas.xmlsoap.org/soap/envelope/');
foreach ($xml->xpath('//soap:Envelope:ResponseCode') as $item) {
echo (string) $item;
}
foreach ($xml->xpath('//soap:Envelope:ResponseDescription') as $item) {
echo (string) $item;
}
I can't figure out why it is not working.
The response we get back from the server that we are requesting is the following
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?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>
<ProcessPaymentResponse xmlns="https://services.incard.com.au">
<ProcessPaymentResult>
<ResponseCode>string</ResponseCode>
<ResponseDescription>string</ResponseDescription>
</ProcessPaymentResult>
</ProcessPaymentResponse>
</soap:Body>
</soap:Envelope>
Try replacing
<?php
$xml->registerXPathNamespace('soap:Envelope',
'http://schemas.xmlsoap.org/soap/envelope/');
foreach ($xml->xpath('//soap:Envelope:ResponseCode') as $item) {
echo (string) $item;
}
foreach ($xml->xpath('//soap:Envelope:ResponseDescription') as $item) {
echo (string) $item;
}
?>
with
<?php
$xml->registerXPathNamespace( 'soap',
'http://schemas.xmlsoap.org/soap/envelope/' );
$result = $xml->xpath('//soap:Body');
foreach ($result as $body) {
echo $body->ProcessPaymentResponse->ProcessPaymentResult->ResponseCode . "<br />";
echo $body->ProcessPaymentResponse->ProcessPaymentResult->ResponseDescription . "<br />";
}
?>
Hope this helps.
I need to access the response from a .NET web service which returns data in XML format.
How can I split the data returned? For example, I would like to parse the data into some PHP variables:
$name = "Dupont";
$email = "charles.dupont#societe.com";
I have been looking for how to do this for a long time without finding the correct way to do it.
My script is:
$result = $client->StudentGetInformation($params_4)->StudentGetInformationResult;
echo "<p><pre>" . print_r($result, true) . "</pre></p>";
The echo in my page is:
stdClass Object
(
[any] => 0Successful10371DupontCharlescharles.dupont#societe.com1234charles.dupont#societe.comfalsefr-FR1003FIRST FINANCE1778AAA Département
)
The webservice response format is:
<?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>
<StudentGetInformationResponse xmlns="http://tempuri.org/">
<StudentGetInformationResult>xml</StudentGetInformationResult>
</StudentGetInformationResponse>
</soap:Body>
</soap:Envelope>
I have tried your example. But it doesn't do what i need.
I need to split the value returned. I would like to get the data and put them into PHP variables:
$name = "Dupont";
$email = "charles.dupont#societe.com";
etc...
Unfortunately the echo of your example gives:
object(stdClass)#1 (1) { ["StudentGetInformationResult"]=> object(stdClass)#11 (1) { ["any"]=> string(561) "0Successful10371DupontCharlescharles.dupont#societe.com1234charles.dupont#societe.comfalsefr-FR1003FIRST FINANCE1778AAA Département" } }
The only class you need is SoapClient. There are a lot of examples you can use in the PHP Documentation.
Example:
try {
$client = new SoapClient ( "some.aspx?wsdl" );
$result = $client->StudentGetInformation ( $params_4 );
$xml = simplexml_load_string($result->StudentGetInformationResult->any);
echo "<pre>" ;
foreach ($xml as $key => $value)
{
foreach($value as $ekey => $eValue)
{
print($ekey . " = " . $eValue . PHP_EOL);
}
}
} catch ( SoapFault $fault ) {
trigger_error ( "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR );
}
Output
Code = 0
Message = Successful
stud_id = 10373
lname = Dupont
fname = Charles
loginid = charles.dupont#societe.com
password = 1234
email = charles.dupont#societe.com
extid =
fdisable = false
culture = fr-FR
comp_id = 1003
comp_name = FIRST FINANCE
dept_id = 1778
dept_name = Certification CMF (Test web service)
udtf1 =
udtf2 =
udtf3 =
udtf4 =
udtf5 =
udtf6 =
udtf7 =
udtf8 =
udtf9 =
udtf10 =
Audiences =