Running PHP script from command line fails but works using the url

I have a PHP script that syncs up with Active Directory. When I run this script from the webpage it works fine but when I run the command,

C:\Windows\system32> E:\wamp\bin\php\php5.4.3\php.exe -f "E:\wamp\www\ats\sync_us
ers.php"

This fails with the error.

PHP Fatal error:  Uncaught exception 'adLDAPException' with message 'No LDAP sup
port for PHP.  See: http://www.php.net/ldap' in E:\wamp\www\ats\adLDAP\src\adLDA
P.php:602
Stack trace:
#0 E:\wamp\www\ats\sync_users.php(4): adLDAP->__construct()
#1 {main}
  thrown in E:\wamp\www\ats\adLDAP\src\adLDAP.php on line 602

Fatal error: Uncaught exception 'adLDAPException' with message 'No LDAP support
for PHP.  See: http://www.php.net/ldap' in E:\wamp\www\ats\adLDAP\src\adLDAP.php
 on line 602

adLDAPException: No LDAP support for PHP.  See: http://www.php.net/ldap in E:\wa
mp\www\ats\adLDAP\src\adLDAP.php on line 602

Call Stack:
    0.0009     132200   1. {main}() E:\wamp\www\ats\sync_users.php:0
    0.0108     630896   2. adLDAP->__construct() E:\wamp\www\ats\sync_users.php:
4

for some reason its not opening the LDAP files. Anyone run into this issue?
ats2012Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Can you show us the script, please?  You can obscure passwords, but please post the complete PHP script file.
0
ats2012Author Commented:
<?php
error_reporting(E_ALL);
require_once(dirname(__FILE__) . '/adLDAP/src/adLDAP.php');
$adldap = new adLDAP();

	$con = mysql_connect("localhost","root","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db('ats');


$query = mysql_query("SELECT id, login FROM atsusers") or die (mysql_error());

  while ($info = mysql_fetch_assoc($query))
{

$login = $info['login'];
$id = $info['id'];


$userinfo = $adldap->user()->info($login, array("*"));


if (!empty($userinfo[0]["department"][0])) {
$ad_department = 		$userinfo[0]["department"][0];
} else {
$ad_department = "";
}
if (!empty($userinfo[0]["company"][0])) {
$ad_company = 		$userinfo[0]["company"][0];
} else {
$ad_company = "";
}
if (!empty($userinfo[0]["streetaddress"][0])) {
$ad_street = 		$userinfo[0]["streetaddress"][0];
} else {
$ad_street = "";
}
if (!empty($userinfo[0]["l"][0])) {
$ad_city = 		$userinfo[0]["l"][0];
} else {
$ad_city = "";
}
if (!empty($userinfo[0]["st"][0])) {
$ad_state = 		$userinfo[0]["st"][0];
} else {
$ad_state = "";
}
if (!empty($userinfo[0]["postalcode"][0])) {
$ad_zip = 		$userinfo[0]["postalcode"][0];
} else {
$ad_zip = "";
}
if (!empty($userinfo[0]["title"][0])) {
$ad_title = 		$userinfo[0]["title"][0];
} else {
$ad_title = "";
}
if (!empty($userinfo[0]["description"][0])) {
$ad_description = 		$userinfo[0]["description"][0];
} else {
$ad_description = "";
}

mysql_query("UPDATE atsusers SET 

department='".$ad_department."',
job_title='".$ad_title."',
company='".$ad_company."',
street='".$ad_street."',
city='".$ad_city."',
zip='".$ad_zip."'
WHERE id = '".$id."'") or die (mysql_error());

echo $login. " has been synced.";
echo "<br>";
}



?>

Open in new window

0
Ray PaseurCommented:
And even without seeing the script, I have a suggestion that might help isolate the issue.  Run phpinfo() from both the webpage and from the command line and look for differences in things like file paths.

See also the installation instructions here:
http://php.net/manual/en/ldap.installation.php
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Julian HansenCommented:
Are you sure you are using the same version of PHP when running from the command line as from the Web Server?

Try this
<?php
phpinfo();
?>

Open in new window

Call this from a web page and note the version

Run from command line and redirect output to an html file.

View the HTML file in browser and compare versions - are they the same.
0
ats2012Author Commented:
Running PHP Version 5.4.3 on the website. Ran it from command line and got the same version.
0
Ray PaseurCommented:
You will need to look at more than the PHP version!  

Check the installation instructions for LDAP carefully, then go into the phpinfo() outputs and compare the outputs line by line (a text editor can work on a copy of the "view source" for this).  You may find that the file paths are not the same.
0
ats2012Author Commented:
I'm not even seeing Ldap support option in the output from command line.
0
ats2012Author Commented:
Is there some switch I need to use when running the script from command line to enable it?
0
Ray PaseurCommented:
If LDAP is not listed in phpinfo() that's a pretty strong hint :-)  Check the installing and configuring links carefully.  I don't have LDAP on my server, so all I can give you is general guidance, and that's already published by php.net.  There are some installation things that are required and there is run-time configuration information, too.
http://www.php.net/manual/en/ldap.setup.php
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
And thinking about it a little more, is there a functional requirement to run this from the command line -- something that cannot be done from a script in the web page?  If not, maybe just use the web script.  Et voila!
0
Julian HansenCommented:
You will need to look at more than the PHP version!  
@ray - different versions would indicate different configs - which would then mean the version on the command line might not be loading the ldap library.

Try adding your php.ini file to the windows path.
0
Dave BaldwinFixer of ProblemsCommented:
There are two different problems you can run into when running PHP on the command line.  The first is not being able to find a copy of 'php.ini' which means it will run with only basic functions enabled.  The second is finding a copy of 'php.ini' that requires web server functions that aren't available on the command line and thus won't allow PHP to run properly.  This is why there are frequently two different versions of 'php.ini' on a system, to allow it to run both from the web server and the command line.

WAMP compounds the problem even more since it is designed to switch 'php.ini' when you change the version of Apache or PHP.  Julian's suggestion of "adding your php.ini file to the windows path" is probably a good one as long as the version of PHP is not changed in WAMP.
0
Ray PaseurCommented:
different versions would indicate different configs
Of course, but finding the same version would not mean anything.  PHP has about 1,000,000 different combinations of configurations.  That's one of the biggest things wrong with the language!
0
Dave BaldwinFixer of ProblemsCommented:
PHP has about 1,000,000 different combinations of configurations.
So does Windows and Linux and every other programming language.  If it wasn't for details, they wouldn't need us programmers.
0
Ray PaseurCommented:
I suppose so, but take a look at Ruby on Rails.  It's great for prototyping because it favors convention over configuration.  Since everything is an object and there's only one way to do most things, it's pretty straightforward, as programming languages go!
0
Dave BaldwinFixer of ProblemsCommented:
I have successfully avoided Ruby so far.
0
Ray PaseurCommented:
;-)
0
ats2012Author Commented:
The php.ini file that it was using didnt have ldap enabled. I found the ini file it was loading and enabled ldap and the script now works using command line.

Thanks
0
Ray PaseurCommented:
Thanks for the points and thanks for using EE.  Glad you got a solution!  In related news, if you ever have any CRON jobs, you will want to do a similar thing - check phpinfo() for the differences between the web page and the CRON environment.

Best regards, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.