Trying to use PHP-s own SoapClient, but I have come across an obstacle.
I'm using __soapCall for making requests, but the query fails, because encodingStyle attribute is set:
$client = new SoapClient(NULL, array(
'location' => 'http://myUri.com',
'uri' => 'http://namespace.com/producer'
));
$result = $client->__soapCall('GET_ALL');
Now doing $client->__getLastRequest(); shows Envolope node like this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
How to remove SOAP-ENV:encodingStyle attribute and still use __soapCall(); ?
Apperantly I was missing "use" parameter in SoapClient construct. Solution:
$client = new SoapClient(NULL, array(
'location' => 'http://myUri.com',
'uri' => 'http://namespace.com/producer',
'use' => SOAP_LITERAL,
));
Related
So I am using Soap client in PHP and I'm trying to make a request on some WSDL. I try to have a specific output but it seems a bit far from what I try to do. Maybe I don't do this right. I'm a bit new to WSDL.
Here is the result I want:
<SOAP-ENV:Body>
<loc:sendSms>
<loc:addresses>9000000000</loc:addresses>
<loc:senderName>9000000</loc:senderName>
<loc:message>test SMS</loc:message>
</loc:sendSms>
</SOAP-ENV:Body>
This is what I try to fit my requirement, but it ends with a server error:
$MCIResp = (array) $_soapClient->__soapCall('sendSms',
'sendMessage' => array(
'addresses' => '9000000000',
'senderName' => '9000000',
'message' => 'test SMS '
)
);
And this is the error I get from this request:
"SoapFault exception: [Client] SOAP-ERROR: Encoding: object has no
'sendmessage' property";
EDIT: Made some improvements on that by getting inspiration on some posts
Here is my code now:
$_soapClient = new SoapClient(
null,
array(
'location' => 'http://someIP/parlayxsmsgw/services/SendSmsService?wsdl',
'uri' => 'http://someIP/parlayxsmsgw/services/SendSmsService?wsdl',
'trace' => 1,
'cache_wsdl' => WSDL_CACHE_NONE,
'use' => SOAP_LITERAL,
'style' => SOAP_DOCUMENT,
'stream_context' => stream_context_create(array(
'http' => array(
'header' => 'servicekey: someservicekey'
),
)),
)
);
$params = new \SoapVar("<sendSms><addresses>90000000000</addresses><senderName>90000000</senderName><message>test SMS</message></sendSms>",XSD_ANYXML);
And then I do a
$_soapClient->whatEverNameHereWillBeIgnored($params);
This is now the XML I generate:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<sendSms>
<addresses>989127184955</addresses>
<senderName>98307066</senderName>
<message>test SMS</message>
</sendSms>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
But I receive this as a response:
Unexpected subelement sendSms
do the "loc:" are mandatory to solve this ? Because when I add it in the raw xml it says that
Undeclared namespace prefix "loc"
Thanks,
I strongly advise you to use a WSDL to PHP generator such as PackageGenerator, it will ease you the request construction so you won't have to wonder how to construct the request, it will be cristal clear.
I'm trying to make a request with the PHP SoapClient in non-WSDL mode. I'm passing parameters as a multi-dimensional object as shown in the below code snippet:
$params = new stdClass;
$params->Characteristic = new stdClass;
$params->Characteristic->Name = 'PRODUCT_TYPE';
$params->Characteristic->CharacteristicValue = new stdClass;
$params->Characteristic->CharacteristicValue->Value = $type;
$params->Characteristic->CharacteristicValue->Type = 'STRING';
$client = new SoapClient(NULL, array( 'trace' => true, 'exceptions' => true, 'uri' => $uri, 'location' => $location,
'connection_timeout'=>9999,
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
'soap_version' => SOAP_1_1, 'encoding' => 'ISO-8859-1',
'use' => SOAP_LITERAL
));
$response = $client->thisIsTheFunction($params);
The generated XML is almost right apart from being wrapped in a tag:
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://[removed]">
<soap-env:body>
<ns1:thisisthefunction>
<param0>
<characteristic>
<name>PRODUCT_TYPE</name>
<characteristicvalue>
<value>Adhoc</value>
<type>STRING</type>
</characteristicvalue>
</characteristic>
</param0>
</ns1:thisisthefunction>
</soap-env:body>
</soap-env:envelope>
The problem is this is being detected as malformed by the service. Is there any way we can remove this extra tag?
I think if you want remove param0 and put characteristicValue in this place, you need to use a SoapParam (http://www.php.net/manual/en/class.soapparam.php).
In fact, your call must proceed like that :
$response = $client->thisIsTheFunction(new SoapParam($params->Characteristic, 'ns1:Characteristic'));
Now, your generated XML looks like that :
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://[removed]">
<soap-env:body>
<ns1:thisisthefunction>
<ns1:characteristic>
<name>PRODUCT_TYPE</name>
<characteristicvalue>
<value>Adhoc</value>
<type>STRING</type>
</characteristicvalue>
</ns1:characteristic>
</ns1:thisisthefunction>
</soap-env:body>
Good luck !
I need to use cookies in my PHP calls, currently i am using the below request:
$publish_Data ='<?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>
<publishData xmlns="http://www.birst.com/">
<token>'.$login_token.'</token>
<spaceID>'.$space_id.'</spaceID>
<date>'.$date.'</date>
</publishData>
</soap:Body>
</soap:Envelope>';
$location_URL = "https://app2104.bws.birst.com/CommandWebService.asmx?wsdl";
$action_URL = "http://www.birst.com/publishData";
$client = new SoapClient(null, array(
'location' => $location_URL,
'uri' => "",
'trace' => 1,
));
try{
$publish_data_response = $client->__doRequest($publish_Data,$location_URL,$action_URL,1);
//Get response from here
//print_r($publish_data_response);
}catch (SoapFault $exception){
var_dump(get_class($exception));
var_dump($exception);
}
I need somehow to enable and use cookies in order in next calls that will follow.
i tried using
sessions_start()
but it didn't help - In Java, something called session_maintain_property is used which allows for the program to use cookies, how do i do the same in PHP?
Problem was solved.
the issue was with the SoapClient, when i was sending a request i created a new SoapClient for every call and that gave me a different cookies per each call - kind of a different session.
basically you need to define the soap call only once:
$client = new SoapClient(null, array(
'location' => $location_URL,
'uri' => "",
'trace' => 1,
));
and use the same client for the rest of the calls.
I need to have this node in my SOAP Request (using 1.1):
<CredentialsHeader xmlns="http://www.url.com/Services/P24ListingService11"
<EMail>ricky#email.net</EMail>
<Password>password</Password>
</CredentialsHeader>
So I have the following PHP:
$client = new SoapClient("https://exdev.www.example.com/Services/example.asmx?WSDL",
array(
"trace" => 1,
"exceptions" => 0,
"cache_wsdl" => 0,
'soap_version' => SOAP_1_1
)
);
$CredentialObject = new SoapVar(array('EMail' => 'ricky#email.net', 'Password' => 'password'), SOAP_ENC_OBJECT);
Which generates:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.example.com/Services/Example">
<SOAP-ENV:Header>
<ns1:CredentialsHeader>
<EMail>ricky#email.net</EMail>
<Password>password</Password>
</ns1:CredentialsHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:EchoAuthenticated/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
All I need to do is prevent it using ns1 and actually define the xmlns in the node like so:
<CredentialsHeader xmlns="http://www.example.com/Services/Example">
<EMail>ricky#email.net</EMail>
<Password>password</Password>
</CredentialsHeader>
I have tested that in Firefox Poster and know for a fact that change fixes the problem.
$CredentialObjectXML = '<CredentialsHeader xmlns="http://www.example.com/Services/Example">
<EMail>'.$UserName.'</EMail>
<Password>'.$Password.'</Password>
</CredentialsHeader>';
$CredentialObject = new SoapVar($CredentialObjectXML,XSD_ANYXML);
This way you can directly use the XML with Type XSD_ANYXML.
Hope this will resolve your problem.
http://www.php.net/manual/tr/soapvar.soapvar.php
Parameter "node_namespace" is what you've been looking for i guess.
I had the same problem and found out that if you map a dummy class to the credential complex type from your WSDL, PHP will output something like:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.example.com/Services/Example">
<SOAP-ENV:Header>
<ns1:CredentialsHeader>
<ns1:EMail>ricky#email.net</ns1:EMail>
<ns1:Password>password</ns1:Password>
</ns1:CredentialsHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:EchoAuthenticated/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
This is not exactly what was requested but although more verbose, it is equivalent.
The code goes like this:
$client = new SoapClient("https://exdev.www.example.com/Services/example.asmx?WSDL",
array(
"trace" => 1,
"exceptions" => 0,
"cache_wsdl" => 0,
"soap_version" => SOAP_1_1,
"classmap" => array(
'credential_complex_type' => 'CredentialObject',
),
)
);
class CredentialObject {}
$credentialObject = new CredentialObject();
$credentialObject->Email = 'ricky#email.net';
$credentialObject->Password = 'password';
I try to make a request to a .NET WSDL function called GetPeriodicValues. The function requires some params and the problem is that SoapClient creates an incorrect XML.
This PHP-code...
$client = new SoapClient(self::URL , array('trace' => 1, 'encoding' => 'UTF-8', 'soap_version' => SOAP_1_1));
$params = array('name' => 'myname', 'address' => 'myaddress');
$result = $client->__soapCall('GetPeriodicValues', array('parameters' => $params), array());
...genereates the following request-XML (I have excluded some irrelevant content):
<SOAP-ENV:Body>
<ns1:RequestOf_GetPeriodicValuesParameters/>
</SOAP-ENV:Body>
But I would expect it to create this
<SOAP-ENV:Body>
<ns1:RequestOf_GetPeriodicValuesParameters>
<ns1:name>myname</ns1:name>
<ns1:address>myaddress</ns1:address>
</ns1:RequestOf_GetPeriodicValuesParameters>
</SOAP-ENV:Body>
How should i include the params in the function-call?
Problem solved. What I learned was that the XML-output is dependent on the WSDL and in my case it wanted me to put params in an array with the key "Params". However, looking at the XML return by the WSDL I found no such information, instead it looked like the key "parameters" should be used. I'm not sure, but perhaps something was wrong with the service.