Problems running php from the command line?
Posted by: enogeze (---.b-ras2.chf.cork.eircom.net)
Date: April 04, 2008 11:11AM

Hi.

I've added the path containing php.exe to my PATH environment variable and can execute php files from the commandline as follows...

php somefile.php

However I can't figure out how to use the curl library. I have "enabled" curl from wampserver->php>php extensions but when I execute a php file that uses functions from the curl library I get a "function undefined" error.

Any help would be much appreciated.
Thanks
(I'm running XP SP2)

Options: ReplyQuote
Re: Problems running php from the command line?
Posted by: toivo (---.belrs4.nsw.optusnet.com.au)
Date: April 04, 2008 11:34AM

Hi,

Here is sample code from a subroutine called in a command line script which works under Wampserver 5:


$site_url = 'http://example.com';

$user_agent = ' Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12');

@$ch = curl_init();

if ($ch === false) {
$msg = "\nAn error has occurred in getting a curl handle\n";
$message .= $msg;
$result = array(false, $message);
return $result;
}

// echo "curl_init done\n";

curl_setopt($ch, CURLOPT_URL, $site_url);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_HEADER, 0);
// output to file
curl_setopt($ch, CURLOPT_FILE, $page_write);

@curl_exec($ch);
$errnum = curl_errno($ch);

closeFile($page_write);

// echo "exec closefile done\n";

$status_line = '';
$run_status = true;
if ($errnum != 0) {
$run_status = false;
$status_line = $site_url." : status: ".$errnum." - DOWN\n";
}
$message .= $status_line;

// read file contents
$php_version = PHP_VERSION;
if (substr($php_version, 0, 1) > "4"winking smiley {
$page = file_get_contents($webpage_file);
} else {
$line_array = array();
$line_array = file($webpage_file, FILE_IGNORE_NEW_LINES);
$page = '';
foreach ($line_array as $line) {
$page .= $line;
}
}
echo $page;

Enjoy!

Regards,

toivo
Sydney, Australia



Edited 2 time(s). Last edit at 04/04/2008 11:40AM by toivo.

Options: ReplyQuote
newbie question - what are those '@'s?
Posted by: enogeze (---.b-ras2.chf.cork.eircom.net)
Date: April 04, 2008 12:08PM

Hi there toivo!

Thanks for your speedy reply.

I didn't have '@'s in my code. Seems to work when I put them in following your example code.

What are those '@'s for?
How come you need them for some curl library calls and not others?

Thanks again

Options: ReplyQuote
Re: newbie question - what are those '@'s?
Posted by: enogeze (---.b-ras2.chf.cork.eircom.net)
Date: April 04, 2008 01:20PM

I see from online docs that @ just suppresses any error messages.

Options: ReplyQuote
Re: Problems running php from the command line?
Posted by: toivo (---.belrs4.nsw.optusnet.com.au)
Date: April 04, 2008 01:41PM

... but it works :-)

It would be worth trying the script without @ to see what the actual errors were, if any.

Regards,

toivo
Sydney, Australia

Options: ReplyQuote


Sorry, only registered users may post in this forum.