What do to when encountering the shell_exec() error in Windows environment

Well, I’m back with this week, but with a different tutorial to address an overlooked error.  We’re going to answer a few questions for those of you that use a Windows development environment.

I’m surprise this issue has not surfaced before now.  Nevertheless, I had the pleasure of engaging a Pastor from the CCB Tutorials community (that’s right, this community) about a few tutorials *not working* (oh no!).

He and I hopped on a quick GoToMeeting, so that I could size up the issue and be certain that it was not a simple fix.

And what do you know! Using Windows 10 and XAMP, he executed code from both Part II and Part III of how to search for an individual, and was presented the following error:

error: shell_exec() curl command failed

Honestly, this error never registered with me because I’m a Mac user 90% of the time. In fact, it took me roughly 3 hours to think long and hard about the error encountered in our GoToMeeting session.

Again, he stated that he could get Part I to execute, but not Part II and III. The light bulb should have gone off for me at that moment, but it didn’t at the time.

However, my light bulb did come on hours later. I realized that the code used for creating reusable functions was primarily focused on Linux/Unix dev enivronments.

Ole’ pesky Windows not wanting to play fair. Truth be told, its completely my fault for not providing both methods in the code, which is why I’m writing this tutorial today. Let’s get started with how to remedy this issue.

Determine which environment is running

The first thing we’ll do is determine which environment you’re running by creating a PHP function that can be reused and included inside of the general.php.

So, crack open a text editor and let’s begin by opening the general.php file in the includes directory. You’ll want to name this function devEnvironment.

Once successfully created, then define debug as a variable and make the value equal to 0. Now create an if statement using the debug variable. Inside the if statement, echo both the php_uname method and PHP_OS method.

Both are built-in php methods for returning the description of operating system PHP is running on, having a long description and a short description respectively. Now close the if statement.

This block of code you just created is used to print the operating system PHP is running on when the debug variable is equal to 1.

Okay now we’re ready for the final if else statement.  This statement uses both strotoupper and substr to determine if the PHP_OS is equal to WIN string.

Simply put, if it’s WIN, then return the string WIN, short for Windows. If not, then return the string LIN, short for Linux.

 

Restructuring ccbDetails function in general.php based on development environment

Quickly, I’m not covering the ins and outs of making the curl call, so you’ll have to look back at Part I and Creating a reusable function for CCB API calls tutorials for in-depth description. Be sure to read both tutorials and pay close attention to their code comments to learn what’s happening.

To restructure your current ccbDetails function, define a devEnv variable and set it equal to the newly created devEnvironment function (see last section).

Make this code addition after the line of code that checks to be sure we have apiUsername, apiPassword and baseAPIUrl variables properly defined.

Now it’s time to define the query_string, url and objData variables as we did in the previous tutorial.

After the objData variable has been defined, create an if else statement that checks to see if the devEnv variable IS NOT equal to the string WIN.

Simply put, we want the first section of this if else statement to run if the environment it’s executing in is not Windows.

In the if section of code, place the previous ccbDetails function’s if posttype statement and output variable.

In the else section of this if statement, place the if posttype statements, query_header, curl call statements, output variable and curl_close statements. Then close the if else statement.

Remaining is the logic to check that there is still output. The only change with the output if else statement is checking for the operating system once more to indicate which error message, whether Windows or Linux, should be displayed to web browser.

Return the response_object and all should be well (*fingers crossed*).  Save the file, and we’re ready to test.

And that’s it! You’ve now remedied the shell_exec() error in a Windows environment

Your code should now execute with ease. Should it not, then please do not hesitate to send me an email or use the contact form on the Contact page.


Please comment if this tutorial has helped you.