PHP SoapClient: Object of class stdClass could not be converted to string - php

I'm making what's likely to be a really simple mistake but can't seem to work out. I'm attempting to send a SOAP request to a web service using the PHP SoapClient library. The following error occurs when attempting to print:
"Object of class stdClass could not be converted to string"
Here is the code, taken primarily from the PHP SoapClient Manual.
<?php
try {
$options = array(
'soap_version'=>SOAP_1_2,
'exceptions'=>true,
'trace'=>1,
'cache_wsdl'=>WSDL_CACHE_NONE
);
$client = new SoapClient('http://www.webservicex.net/ConvertTemperature.asmx?WSDL', $options);
$results = $client->ConvertTemp(array('Temperature'=>'100', 'FromUnit' => 'degreeCelsius',
'ToUnit' => 'degreeFahrenheit'));
}
catch (Exception $e)
{
echo "<h2>Exception Error!</h2>";
echo $e->getMessage();
}
$results = $client->ConvertTemp(array('Temperature'=>'100', 'FromUnit' => 'degreeCelsius',
'ToUnit' => 'degreeFahrenheit'));
print $results;
?>
I understand that the message is telling me that I'm trying to print an entire object rather than a member of that object. What I don't understand is that I'm expecting a call to ConvertTemp to return a string. Why is an object being return? Thanks in advance for any help.

Well, your expectations may be wrong. A var_dump or print_r can shed light on what $results actually is, re-examing the wsdl could tell you why:
Hint: __getTypes():
struct ConvertTempResponse {
double ConvertTempResult;
}

Related

PHP Cannot Run Soap Function from wsdl file

Hi am trying to run a function from wsdl. How to run this function?
When i am running get functions i am getting this
array(3) { [0]=> string(797) "ClientSoap createClient(string $client_name, string $client_password, string $first_name, string $last_name)" }
I want to run the createClient function .
I already tried using this code
$client = new SoapClient('mywsdl.wsdl');
$data = $client->createClient("samplecustomer", "samplepass", "first", "last");
print_r($data);
I am getting an error that createClient does not exist. Any ides?
Thanks in advance
I don't know if that can help you but you can add a try catch surround the declaration.
try {
$soapClient = new \SoapClient($url. '?wsdl', $options);
} catch (\SoapFault $e) {
print_r($e->getMessage();
}

SOAP Request from PHP is not working

I have a web service available # http://www.xxxxx/zzzzzzzz/service.asmx and I am trying to send a SOAP request for method - some_function with both the parameters but still not able to get the connection through.
This is my code:
<?php
$param = array('cedula'=>'XXXX','contrasena'=>'YYYYYY');
$client = new SoapClient("http://www.xxxxx/zzzzzzzz/service.asmx?wsdl");
$result = $client->__soapCall('some_function', $param);
print $result;
?>
Error that I'm getting is:
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/zzzz/XXXXXXXXXX.com/uni/index.php:6 Stack trace: #0 /home/zzzz/XXXXXXXXXX.com/uni/index.php(6): SoapClient->__soapCall('some_function' , Array) #1 {main} thrown in /home/zzzz/XXXXXXXXXX.com/uni/index.php on line 6
Please suggest the corrections. Many thanks in advance :)
Thanks #dootzky & #lulco. I have solved this. Code below works perfectly fine for me:
<?php
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$wsdl_path = "http://www.xxxxxxx/zzzzzzzzzz/service.asmx?WSDL";
$login_id = 'XXXX';
$password = 'YYYYYY';
$client = new SoapClient($wsdl_path, array('trace' => 1));
try {
echo "<pre>\n";
$result = $client->SOME_FUNCTION(array("request" => array("cedula" => $login_id, "contrasena" => $password)))
print_r($result);
echo "\n";
}
catch (SoapFault $exception) {
echo $exception;
}
?>
I think you are very close to getting this code to work. I would also give credit to this answer on StackOverflow, looks very similar to what you are asking:
"Object reference not set to an instance of an object" error connecting to SOAP server from PHP
So maybe you should just shoot the method directly, like so:
$client->SOME_FUNCTION(array("request" => array('cedula'=>'XXXX','contrasena'=>'YYYYYY'));
Hope that helps! :)
I think it could be problem in wsdl for service SOME_FUNCTION.
Here is the list of services:
http://www.xxxxxx/zzzzzzzzzz/service.asmx
All of them work, but SOME_FUNCTION doesn't. Go to url http://www.xxxxxx/zzzzzzzzzz/service.asmx?op=SOME_FUNCTION and try to set parameters and click Invoke. It will not work and throw exception "Object reference not set to an instance of an object.".
Then try another service, it will work and return some result.
Example for OTHER_FUNCTION service works:
$param = array('estatus'=>'XXXX');
$client = new SoapClient("http://www.xxxxxx/zzzzzzzzzz/service.asmx?wsdl");
$result = $client->__soapCall('OTHER_FUNCTION', $param);
print_r($result);

PHP accessing webservices

I am trying to access this web service and it is working fine but when i come to diplay the results using the TopGoalScorersResult it is giving me this error "Catchable fatal error: Object of class stdClass could not be converted to string". Can anyone please help me with this. The $results variable is filled with the correct answers just want to display them using the method TopGoalScorersResult
<?php
try {
$client = new SoapClient(
'http://footballpool.dataaccess.eu/data/info.wso?wsdl');
var_dump($client->__getFunctions());
var_dump($client->__getTypes());
$results = $client->TopGoalScorers(array("iTopN"=>"20"));
var_dump($results);
echo $results->TopGoalScorersResult;
} catch (SoapFault$e) {
echo "<pre>" . $e->getMessage() . "</pre>";
}
?>
web services in php4 you can use SOAP on the WSDL to call the web-service functions exmaple link is hereenter link description here
$client = new SoapClient("youridSome.wsdl");
and now $client is now an object . There was a method called getTime() in the WSDL then you use
$result = $client->getTime();

Soap is not working in PHP

I am using SoapClient but I am not able to get the result. I get this error:
The server was unable to process the request due to an internal error.
For more information about the error, either turn on
IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute
or from the configuration behavior) on the server in order to send the
exception information back to the client, or turn on tracing as per
the Microsoft .NET Framework 3.0 SDK documentation and inspect the
server trace logs.
<?php
$silverpop = new SoapClient($my_url, array('trace' => 1));
/*$client = new stdClass();
$client->LoginID = 'mylogin-id';
$client->LicenceKey = 'mylicense-key';*/
$clientobj = (object) array("LoginID" => "mylogin-id", "LicenceKey" => "mylicense-key");
try {
//$var = $silverpop->__soapCall("GetServicesforPincode",array('P_Pincode'=>'110014','P_ClientObject'=>$clientobj));
//$var = $silverpop->GetServicesforPincode('110014',$clientobj);
$var = $silverpop -> __soapCall("GetServicesforPincode", array('110014', $clientobj));
} catch (SoapFault $exception) {
echo $exception -> getMessage();
}
echo '<pre>';
print_r($var);
?>
What am I doing wrong?
Either you have send your data not according to the specifications or your SoapServer is not working. I think the first one as Soap isn't always as clear as it should. As it looks like the error message is actually generated by the SoapServer, I recommend checking the schema for allowed parameters/calls and their format. If that's all correct, check if you are missing headers etc.
If all of the above is correct, fix your SoapServer. If you haven't gotten access to it, poke the owner.
try{
$client = new SoapClient($my_url,array('trace' => 1));
$object = new stdClass();
$object->LoginID = 'mylogin-id';
$object->LicenceKey = 'mylicense-key';
$xml = simplexml_load_string($client->GetServicesforPincode($object));
$json = json_encode($xml);
print_r($json);
}
catch (SoapFault $exception) { echo $exception; }

WSO2AS Soapcall of service returns null

I'm developing a web-app, which needs data from a database.
For the communication with the db I use WSO2AS.
I made a database and linked that to the data service created, when i test the service in the admin panel of WSO2, I get the data needed from the database.
The data service I created is called TestService.
Now I want to have the same response in my php template as well, using this code.
<?php
try {
$client = new SoapClient('http://192.168.178.12:9763/services/TestService?wsdl');
$result = $client->__soapCall('greet');
printf("Result = %s\r\n", $result->return);
} catch (Exception $e) {
printf("Message = %s\r\n",$e->__toString());
}
?>
But this gives NULL, when I try to dump the $result.
When I try to execute an example WSO2 created, I do get the right result. While the soapcall code is the same, only the service name is different.
<?php
try {
$client = new SoapClient('http://192.168.178.12:9763/services/HelloService?wsdl');
$result = $client->__soapCall('greet', array(array('name' => 'Sam')));
printf("Result = %s\r\n", $result->return);
} catch (Exception $e) {
printf("Message = %s\r\n",$e->__toString());
}
?>
This code returns "Hello Sam !!!".
So I wonder what I did wrong, I personally think I made a mistake in implementing the service itself, but can't find it.
If any more information is needed feel free to ask, hope someone can help me with this.
Thanks in advance!
Apparently you need to have arguments in your soapcall.
After calling this __soapCall('greet', array());
It gave the proper response.

Categories