PHP Cannot Run Soap Function from wsdl file - php

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();
}

Related

Laravel not running try catch block

Ive run into a little issue.
Im calling a method using the following
$this->testConnection($request->all());
The method looks like so
private function testConnection($data)
{
try {
$conn = ftp_connect($data['host']);
if (false === $conn) {
throw new Exception('Cant connect');
}
} catch (\Exception $e) {
return redirect()->route('create')->withInput()->withErrors($e->getMessage());
}
}
Update: It seems the ftp_connect PHP function isn't working and its not returning any errors
Im using Laravel 5.3
Any help would be grand.
Cheers,
The solution to this was that i was missing
use Exception;

Using additional data in php exceptions

I have php code that execute python cgi and I want to pass python trace (returned from cgi) as extra data to php exception how can I do this and how can I get that value from catch(Exception e) { (It should check if that extra value exesit or not).
I have code like this:
$response = json_decode(curl_exec($ch));
if (isset($response->error)) {
// how to send $response->trace with exception.
throw new Exception($response->error);
}
return $response->result;
and I use json-rpc library that should return that data to the user:
} catch (Exception $e) {
//catch all exeption from user code
$msg = $e->getMessage();
echo response(null, $id, array("code"=>200, "message"=>$msg));
}
Do I need to write new type of exception or can I do this with normal Exception? I would like to send everything that was thrown in "data" =>
You need to extend Exception class:
<?php
class ResponseException extends Exception
{
private $_data = '';
public function __construct($message, $data)
{
$this->_data = $data;
parent::__construct($message);
}
public function getData()
{
return $this->_data;
}
}
When throwing:
<?php
...
throw new ResponseException($response->error, $someData);
...
And when catching:
catch(ResponseException $e) {
...
$data = $e->getData();
...
}
Dynamic Property (not recommended)
Please note that this will cause deprecation error in PHP 8.2 and will stop working in PHP 9 according to one of the PHP RFC https://wiki.php.net/rfc/deprecate_dynamic_properties
As the OP asking about doing this task without extending Exception class, you can totally skip ResponseException class declaration. I really not recommend do it this way, unless you've got really strong reason (see this topic for more details: https://softwareengineering.stackexchange.com/questions/186439/is-declaring-fields-on-classes-actually-harmful-in-php)
In throwing section:
...
$e = new Exception('Exception message');
$e->data = $customData; // we're creating object property on the fly
throw $e;
...
and when catching:
catch(Exception $e) {
$data = $e->data; // Access data property
}
September 2018 edit:
As some of readers found this answer useful, I have added a link to another Stack Overflow question which explains the downsides of using dynamically declared properties.
Currently, your code converts the response text directly into an object without any intermediate step. Instead, you could always just keep the serialized (via JSON) text it and append it to the end of the Exception message.
$responseText = curl_exec($ch);
$response = json_decode($responseText);
if (isset($response->error)) {
throw new Exception('Error when fetching resource. Response:'.$responseText);
}
return $response->result;
Then you could just recover everything after "Response:" in your error log and optionally de-serialize it or just read it.
As an aside, I would also not count on the server sending JSON, you should verify that the response text was actually parseable as JSON and return a separate error for that if it isn't.

Symfony 2 SOAP CLIENT

Please tell me why i must put "\" before \SoapClient. When i delete "\", then i have a error.
public function indexAction($name)
{
try {
$client = new \SoapClient('some WSDL', array('trace' => 1));
$a = array('Login'=>'1', 'Password'=>'1', 'LetterNo'=>'1');
$response = $client->__soapCall('GetTracking', array($a));
ladybug_dump($response->GetTrackingResult->Status);
} catch (\SoapFault $e) {
var_dump($e->getMessage(), $client->__getLastResponse()); die();
}
return array('response' => $response);
}
Thanks for help
...because your controller is in a namespace, so your call to instantiate SoapClient without the root slash tries to load that object from the current namespace.
App\Controller\SoapClient instead of SoapClient.
You can use a use statement at the top of your controller to bring it into scope. use SoapClient; and you'll be good to go.

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

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;
}

Soap Function Not Found

So I am trying to connect using SoapClient but am running into issues.
$this->client = new SoapClient(self::parser_url . '/SovrenConvertAndParse/ConvertAndParse.asmx?WSDL',array('soap_version' => SOAP_1_2
));
var_dump($this->client);
var_dump($this->client->__getFunctions());
try { var_dump($this->client->GetVersionInfo());}
catch (Exception $e){echo $e->getMessage();}
And here are the results I get:
object(SoapClient)#32 (2) {
["_soap_version"]=>
int(2)
["sdl"]=>
resource(42) of type (Unknown)
}
array(30) {
...
[18]=>
string(65) "GetVersionInfoResponse GetVersionInfo(GetVersionInfo $parameters)"
...
}
Not Found
Seems like if the function shows up in getFunctions() then I should be able to call it, and it should be findable. No? What could cause something like this?
So I also tried it without the try/catch and I get the following error:
Fatal error: Uncaught SoapFault exception: [HTTP] Not Found in /path/to/file:29
Anyone have any ideas as to what could be going on and how to fix this?
I'm from Sovren and I just stumbled across this post. Since you didn't contact us for support I'll assume you've already resolved this problem. But for other readers, I've run the following file with PHP 5.2.12 and the call to GetVersionInfo responded correctly with both SOAP_1_1 and SOAP_1_2:
<?php
$client = new SoapClient(
'http://localhost/SovrenConvertAndParse/ConvertAndParse.asmx?WSDL',
array('soap_version' => SOAP_1_2));
var_dump($client);
var_dump($client->__getFunctions());
try { var_dump($client->GetVersionInfo());}
catch (Exception $e){echo $e->getMessage();}
?>
this should work ..
$this->client = new SoapClient(self::parser_url . '/SovrenConvertAndParse/ConvertAndParse.asmx?WSDL',array("soap_version" => SOAP_1_1
));
var_dump($this->client);
var_dump($this->client->__getFunctions());
try { var_dump($this->client->GetVersionInfo(GetVersionInfoResult=>"version"));} //this is where i am editing your question, you need to have a parameter passed
catch (Exception $e){echo $e->getMessage();}

Categories