I got a PHP web service that had been running great for a long time, but somewhere a long the way it some how stopped working, and I just can't get it to work again.
I got some php page in which all I do is define a class with functions, and in the end of it I create the SoapServer.
It looks like this -
class MyClassWS
{
function .....
function ....
}
ini_set("soap.wsdl_cache_enabled", "0");
error_log("Server after ini_set");
$soapserver = new SoapServer("MyWSDL.wsdl");
error_log("Server after new SoapServer");
$soapserver->setClass("MyClassWS");
error_log("Server after setClass");
//error_log(print_r($soapserver->getFunctions()));
try
{
$soapserver->handle();
}
catch(Exception $ex)
{
error_log("Exception!".$ex->getMessage());
}
error_log("Finished Handling",0);
Right after the $soapserver->handle(); the code terminates, and I get a vague "SOAP-ENV:ClientBad Request" result on my web page.
This happens when I "require_once" this page, from my index page, so I could call functions that are defined in this class, from my index page.
My guess is that maybe I've been fiddling too much with my WSDL and somehow it screwed up my while WebService, but I tried looking on what's wrong with it, but couldn't get onto anything. Especially because of this annoying vague error message, that doesn't really help.
Thanks!
I think I nicely fixed it by separating the WebService class that included the SoapServer into 2 different classes.
One that included only the class with the functions, and another that included the WebServer and only had a reference to the class.
This way I can import the class with the functions without also importing the SoapServer and triggering the handle() function.
Related
I'm trying to move a php/mysql web application to a new server. The application runs fine on multiple other servers, just not the one the client wants it on. Problem 1 is I can't get errors to display on screen or in an error log. (I've posted a separate question about this). I'm hoping if I can get error to display I'll have more to go on, but what I know so far is it fails when trying to extend a module. I stripped down the class to just this:
class Module_Organization extends LmsModule {
function Module_Organization($module_name = '') {
die('Made it into the function');
}
}
Nothing happens. But if I change it to:
class Module_Organization {
function Module_Organization($module_name = '') {
die('Made it into the function');
}
}
then it does execute the die statement. So it seems that the extend portion is tripping it up. But I don't think it's the code because I know this exact code works fine on other 5 other servers. So I'm wondering if there is any server configuration that could prevent php from extending a class.
Thank you in advance for your help.
I compared the php.ini file to a server where it is working to make them match and it is now working. I'm not sure which changed fixed it though. Thank you.
I am in the process of migrating a site from my personal dev server onto Windstream's business hosting server. I've already run into the issue of having developed using PHP 5.4 only to find out that my static functions won't work on WS's 5.1.4 installation. I've since fixed those issues and am not facing one that I can't seem to find any help for on the internet.
All of the static functions I was using have been rewritten as functions outside the class scope. Instead of having
class Product{
...
public static function myFunction(){}
...
}
I now have
function myFunction(){}
class Product{...}
in my included Product.php file.
However, when I try to call myFunction() from my code, nothing happens. I know the nothingness comes from WS's error handling, but the point is, the function isn't working. To verify this, I have taken the following steps:
Inserted the line echo "entered included"; immediately following the <?php in Product.php. This prints "entered included" on the index page, indicating that my include is working. I have done the same thing before the final ?> with the same results, so I don't think it's getting hung up inside the included file.
I have changed myFunction() in the included file to be simply
function myFunction(){echo "myFunction works";}
A call to myFunction() still makes nothing happen.
I have moved myFunction() to the including file (myFunction() now lives in index.php instead of Product.php; index includes Product.php). This time, myFunction() executes without issue.
To my 'hack it til it does what it should' sensibilities, this tells me that the server is having a problem with functions that are declared in files that are included; honestly, though, I have absolutely no clue what's going on, and any help would be appreciated. Their website is currently down, and they were expecting it to only be offline for a day, so I'll try pretty much anything short of sacrificing a fatted calf.
I know I should be posting more specific code, but since this is a customer's actual website, I'm trying to put as little of the actual code out here as is possible. I'm happy to append specific sections of code to this entry as they are requested for clarification.
Thanks in advance for your consideration.
#Rottingham: First, thanks for the 3v4l link. Second, my assumption about static methods in 5.4 vs 5.1.4 came from this line of php.net's page on static members and methods:
"As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static)."
src - http://www.php.net/manual/en/language.oop5.static.php
Since my version and the server version were on different sides of the 5.3 mark mentioned, I incorrectly assumed that this was my problem.
Third, when I get in from my day job, I'll update my code to show errors and update this post if a solution has not yet been found.
Ultimately, my problem isn't with using static methods (since I don't have them anymore) but with using any function that is declared in an included .php file.
I have a PHP daemon script running on the command line that can be connected to via telnet etc and be fed commands.
What it does with the command is based on what modules are loaded, which is currently done at the start. (psuedocode below for brevity)
$modules = LoadModules();
StartConnection();
while(true){
ListenForCommands();
}
function LoadModules(){
$modules = Array();
$dir = scandir("modules");
foreach($dir as $folder){
include("modules/".$folder."/".$folder.".php");
$modules[$folder] = new $folder;
}
}
function ListenForCommands(){
if(($command = GetData())!==false){
if(isset($modules[$command])){
$modules[$command]->run();
}
}
}
So, an example module called "bustimes" would be a class called bustimes, living in /modules/bustimes/bustimes.php
This works fine. However, I'd like to make it so modules can be updated on the fly, so as part of ListenForCommands it looks at the filemtime of the module, works out if it's changed, and if so, effectively reloads the class.
This is where the problem comes in, obviously if I include the class file again, it'll error as the class already exists.
All of the ideas I have of how to get around this problem so far are pretty sick and I'd like to avoid doing.
I have a few potential solutions so far, but I'm happy with none of them.
when a module updates, make it in a new namespace and point the reference there
I don't like this option, nor am I sure it can be done (as if I'm right, namespaces have to be defined at the top of the file? That's definitely workaroundable with a file_get_contents(), but I'd prefer to avoid it)
Parsing the PHP file then using runkit-method-redefine to redefine all of the methods.
Anything that involves that kind of parsing is a bad plan.
Instead of including the file, make a copy of the file with everything the same but str_replacing the class name to something with a rand() on the end or similar to make it unique.
Does anyone have any better ideas about how to either a) get around this problem or b) restructure the module system so this problem doesn't occur?
Any advice/ideas/constructive criticism would be extremely welcome!
You should probably load the files on demand in a forked process.
You receive a request
=> fork the main process, include the module and run it.
This will also allow you to run several commands at once, instead of having to wait for each one to run before launching the next.
Fork in php :
http://php.net/manual/en/function.pcntl-fork.php
Tricks with namespaces will fail if module uses external classes (with relative paths in namespace).
Trick with parsing is very dangerous - what if module should keep state? What if not only methods changed, but, for example, name of implemented interface? How it will affect other objects if they have link to instance of reloaded class?
I think #Kethryweryn is something you can try.
i have a question in PHP related to the parameters of a function and their scope.
I am building an own little template system and currently i keep using:
try {
$ret = include("file.php");
if(!$ret) {
throw new Exception();
}
// Go on here...
}
catch(Exception $e) {
// error handling
}
about 3-4 times in the mainfile index.php. This code first includes the needed file (set by GET parameters in the URL), trys to include it and if it fails it handles all errors. Since this is used 3-4 times in index.php i want to make a function out of it so i can easily call $Template->LoadFile("filename.php"); and this function handles everything for me: Including, Executing the code and error handling.
Not that hard i thought, but when doing so i get a lot of error messages because the variables needed by the included file for its execution are only avaiable in the scope of index.php (where the code was executed before) and not in the scope of the LoadFile() function. What can i do now?
Revert everything back and again use this above code 3-4 times stupidly?
Add all needed variables in LoadFile() as parameters so by using $Template->LoadFile($file,$vars); $vars would be available for use in LoadFile() for the included File? By doing so the given parameters would be huge in some cases. For example if i add an own board, the $vars would contain every board data and thats quite much. Wouldn't that make the template system really slow?
Add the needed variables via parameters as reference? (same as my 2nd suggest, but with less engine slowdown).
use globals? NO!
What options are there else? Thank's a lot :)
You can use option2 but in variety of other ways, have a look at how codeigniter and kohana render their views.
you can do something like
$Template->LoadFile($file, $vars)
where $vars is an array like array('thisIsVariableName' => 'thisIsVariableValue')
or
$Template->file($file)
->vars('thisIsVariableName', thisIsVariableValue)
->multi_var(array('thisIsVariableName2' => 'thisIsVariableValue2'))
->render();
In case of the template size, then you can always split a template into subtemplates.
How should I write error reporting modules in PHP?
Say, I want to write a function in PHP: 'bool isDuplicateEmail($email)'.
In that function, I want to check if the $email is already present in the database.
It will return 'true', if exists. Else 'false'.
Now, the query execution can also fail, In that time I want to report 'Internal Error' to the user.
The function should not die with typical mysql error: die(mysql_error(). My web app has two interfaces: browser and email(You can perform certain actions by sending an email).
In both cases it should report error in good aesthetic.
Do I really have to use exception handling for this?
Can anyone point me to some good PHP project where I can learn how to design robust PHP web-app?
In my PHP projects, I have tried several different tacts. I've come to the following solution which seems to work well for me:
First, any major PHP application I write has some sort of central singleton that manages application-level data and behaviors. The "Application" object. I mention that here because I use this object to collect generated feedback from every other module. The rendering module can query the application object for the feedback it deems should be displayed to the user.
On a lower-level, every class is derived from some base class that contains error management methods. For example an "AddError(code,string,global)" and "GetErrors()" and "ClearErrors". The "AddError" method does two things: stores a local copy of that error in an instance-specific array for that object and (optionally) notifies the application object of this error ("global" is a boolean) which then stores that error for future use in rendering.
So now here's how it works in practice:
Note that 'Object' defines the following methods: AddError ClearErrors GetErrorCodes GetErrorsAsStrings GetErrorCount and maybe HasError for convenience
// $GLOBALS['app'] = new Application();
class MyObject extends Object
{
/**
* #return bool Returns false if failed
*/
public function DoThing()
{
$this->ClearErrors();
if ([something succeeded])
{
return true;
}
else
{
$this->AddError(ERR_OP_FAILED,"Thing could not be done");
return false;
}
}
}
$ob = new MyObject();
if ($ob->DoThing())
{
echo 'Success.';
}
else
{
// Right now, i may not really care *why* it didn't work (the user
// may want to know about the problem, though (see below).
$ob->TrySomethingElse();
}
// ...LATER ON IN THE RENDERING MODULE
echo implode('<br/>',$GLOBALS['app']->GetErrorsAsStrings());
The reason I like this is because:
I hate exceptions because I personally believe they make code more convoluted that it needs to be
Sometimes you just need to know that a function succeeded or failed and not exactly what went wrong
A lot of times you don't need a specific error code but you need a specific error string and you don't want to create an error code for every single possible error condition. Sometimes you really just want to use an "opfailed" code but go into some detail for the user's sake in the string itself. This allows for that flexibility
Having two error collection locations (the local level for use by the calling algorithm and global level for use by rendering modules for telling the user about them) has really worked for me to give each functional area exactly what it needs to get things done.
Using MVC, i always use some sort of default error/exception handler, where actions with exceptions (and no own error-/exceptionhandling) will be caught.
There you could decide to answer via email or browser-response, and it will always have the same look :)
I'd use a framework like Zend Framework that has a thorough exception handling mechanism built all through it.
Look into exception handling and error handling in the php manual. Also read the comments at the bottom, very useful.
There's aslo a method explained in those page how to convert PHP errors into exceptions, so you only deal with exceptions (for the most part).