Check if soap connection working - php

Quick one,
How would you go about checking if your connection to a soap server is actually connecting?
I have this code:
$m_wsdl = "https://m2mconnect.orange.co.uk/orange-soap/services/MessageServiceByCountry?wsdl";
try {
$client = new SoapClient($m_wsdl);
$this->m_messages = $client->peekMessages('','',10,"");
} catch (Exception $e) {
echo "Exception: \n" . $e->getMessage() . "\n";
}
$this->do_parse_xml();
Obviously my username and password are in the peekmessages field where they should be, and they are both correct i am 100%.
For some reason its not returning any data at all and i dont know how to check to see if the connection is actually working??
Im getting no exceptions being echo'd
Thanks for any help

Use isSoapFault() http://php.net/manual/en/function.is-soap-fault.php
Also, the peekMessages method would probably return false or a SoapFault.
You can also set Exceptions to true on the SoapClient

Related

Where does my PHP code make connection to the database?

So I am very new to MongoDB and PHP and I am trying to make a connection to the database and perform an insert query on it. I have managed to make it work, but know I do not really understand my own code.
My code:
<?php
// PHP version 7.4 used here
try {
require 'mongophp/vendor/autoload.php';
//$mongo = new MongoDB\Driver\Manager('mongodb://localhost:27017');
echo "Connection to database successfully\n";
$collection = (new MongoDB\Client)->shinto->users;
$insertOneResult = $collection->insertOne([
"name" => "testuser",
"password" => "1234",
]);
printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
var_dump($insertOneResult->getInsertedId());
echo "so far so good";
}
catch (Throwable $e) {
// catch throwables when the connection is not a success
echo "Captured Throwable for connection : " . $e->getMessage() . PHP_EOL;
print("\ndoes not work\n");
}
?>
The thing that I do not understand is how the code knows where my database is since I do not have to use the connection string.
My guess is that the mongophp/vendor/autoload.php takes care of this part, but I have looked through those files and can't seem to find the answer to my question in there as well.
Can someone help me understand how my code knows where my database is without a connection string or provide me with a link that will explain this to me?
Thank you very much in advance!

Get bad WSDL from SoapFault?

I'm connecting to a 3rd party service with SoapClient. Most of the time it works fine, but every once in awhile, maybe once out of every 100-150 calls, I get the error
Soap Failed: SOAP-ERROR: Parsing Schema: unexpected in complexType
My code is in a try/catch with a retry, and it will work on the next round through. But I'd like to examine the WSDL to find out why that fails, partly for my own curiosity, and in case I need to pass it along to the company I'm connecting to. Can I get that information from the SoapFault? Or would I have to call the URL to get the string? I'm afraid if I get the WSDL after the fact, it may already be fixed.
$pass = FALSE;
$this->soap = NULL;
$this->session = NULL;
do {
try {
Doc::i("Starting session");
$this->soap = new SoapClient($this->wsdl_url, ['trace' => 1]);
$pass = TRUE;
} catch (\SoapFault $e) {
Doc::e('Soap Failed: ' . $e->getMessage());
if(str_contains($e->getMessage(),'Parsing Schema') && !empty($e->detail)) {
Doc::e($e->detail); // Something new I'm trying to see if it helps
}
} catch (FatalErrorException $e) {
Doc::e("Soap failed really bad: " . $e->getMessage());
} catch (\Exception $e) {
Doc::e("Soap failed bad: " . $e->getMessage());
}
} while (!$pass);
You should be able to use $this->soap->__getLastResponse() since you are passing 'trace' => 1 option to SoapClient.
You might also consider logging $this->soap->__getLastRequest as well as the headers versions of both of these to ensure you're capturing as much information as possible at run-time.
Refer to the SoapClient method list for the possible options. Just remember the trick here is the trace option: without that, these will not return anything useful!

dns_get_record(): A temporary server error occurred.

I'm querying a whole bunch of addresses, some are online and some are not. I can't seem to get around this error however, even catching the exception fails :(
dns_get_record(): A temporary server error occurred.
try {
$result = dns_get_record('_minecraft._tcp.' . $addr, DNS_SRV);
}
catch (Exception $e) {
return [$addr,$port];
}
If this error occurs, I want to continue the script, skipping the record, however currently the script just halts.
Any help appreciated!!
I can't catch this exception too. And how I understood it's a bug of php:
https://bugs.php.net/bug.php?id=73149
But I found another solution. You can use # when you call this function. This symbol kill all errors when you call this one. And it will looks like that:
$dns = #dns_get_record($domain, DNS_A);
if(!$dns){
return false;
}
I was able to get the IP (A record) for a host using the below PHP function
gethostbynamel(string $hostname): array|false
Reference: gethostbynamel — Get a list of IPv4 addresses corresponding to a given Internet host name
try this:
try {
$dns = dns_get_record($domain, DNS_A);
}
catch (Exception $e) {
if ($e->getMessage() !== 'dns_get_record(): A temporary server error occurred.') {
throw $e;
}
$dns = false;
}

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.

How to get a message in a variable if the page contains an error?

While we run an web application some page may contain error and some page maynot contain error,
I want to get a notification if the page contains some error ,If there is an error we can see the error in the page, but can we set any value to a variable if the page contains error.. such that we can get the notification that there is an error .
I want to get the notification since i want to create an error log,If we can set the variable with some value then we can use some condition to create a logfile
How can we do that?
There is several ways to do it. One is to setup a custom error handler. PHP will trap most errors raised during script execution and pass it to your custom handler then. What you do inside the handler is up to you. You can write to a log and then redirect to somewhere else or whatever you want.
If you are talking about Exceptions, then wrap code that can break in try/catch blocks. If an error occurs, handle the exception the catch block. What you put in there is again up to you.
Go through the linked pages to learn how this works. Catching an error, setting a variable and writing to a log are three distinct things. Isolate and solve them one by one.
You could also consider using a try { } catch { } block and writing exceptions to error log in catch { } part. Like this:
try {
$db = new MyDb('127.0.0.1', 'root', 'root');
if (false === $db) {
throw new Exception ('Could not connect to the database.');
}
$row = $db->getTable('table_name')->getRowByColumn('id', $_GET['id']);
if (null === $row) {
throw new Exception ('Row with id ' . $_GET['id'] . ' not found.')
}
// and so on
} catch (Exception $e) {
$fp = fopen('logs/error.txt', 'w');
fwrite($fp, date('l jS \of F Y h:i:s A') . ': ' . $e->getMessage() . "\n");
fclose($fp);
}
You get the idea.
Instead of just a date of error you could also append a login of signed in user if the script is in authentication protected area so you know which user got that error.

Categories