views:

304

answers:

2

i wanna using curl in php script and run it in command line mode.

here is the script

<?php
 //enable_dl("php_curl.dll");
 $ch = curl_init();
 $options=array(
  CURLOPT_URL=>"http://test.com/wp-content/themes/bluefocus/images/desc_img.jpg",
  CURLOPT_BINARYTRANSFER=>true,
  CURLOPT_VERBOSE=>true
 );
 curl_setopt_array($ch,$options);
    $data = curl_exec($ch);
 $fp=fopen("test.jpg","w");
 fwrite($fp,$data);
 curl_close($ch);
?>

i run it in cmd with command php get.php

the error message:

D:\project>php get.php

Fatal error: Call to undefined function curl_init() in D:\project\gals_curl_batch_download\get.php on line 3

phpinfo() in the webpage output shows curl has been enabled

cURL support  enabled
cURL Information  libcurl/7.19.4 OpenSSL/0.9.8k zlib/1.2.3 

and here is the strange thing phpinfo() int the webpage output show, infact extension enabled in php.ini can be run in web page. the exact directory of the extension is under ./ext

extension_dir ./ext ./ext

but php -i | find "extension_dir" always show this and can't be modified with in php.ini file

extension_dir => C:\php5 => C:\php5

restarted apache serveral times, keeps the same error. so I wonder why the value of extension_dir can't be modified.

thx in advance.

A: 

Did you try adding extension=php_curl.dll to php.ini?

FWIW, the best way to check phpinfo for CLI is probably:

$ php -i | fgrep -i curl
Configure Command =>  '/SourceCache/apache_mod_php/apache_mod_php-44.4/php/configure'  '--prefix=/usr' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--disable-dependency-tracking' '--with-apxs2=/usr/sbin/apxs' '--with-ldap=/usr' '--with-kerberos=/usr' '--enable-cli' '--with-zlib-dir=/usr' '--enable-trans-sid' '--with-xml' '--enable-exif' '--enable-ftp' '--enable-mbstring' '--enable-mbregex' '--enable-dbx' '--enable-sockets' '--with-iodbc=/usr' '--with-curl=/usr' '--with-config-file-path=/etc' '--sysconfdir=/private/etc' '--with-mysql-sock=/var/mysql' '--with-mysqli=/usr/bin/mysql_config' '--with-mysql=/usr' '--with-openssl' '--with-xmlrpc' '--with-xsl=/usr' '--without-pear'
curl
cURL support => enabled
cURL Information => libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3
$

php -i is basically the CLI equivalent of phpinfo()

Also, double check that PHP CLI is configured to use the same php.ini (or if it's using a different file, ensure that you've edited that one as well). Sometimes PHP can be set up with different php.inis depending on how PHP is invoked -- I believe debian and Ubuntu do this, specifically.

Determining the php.ini file in use:

$ php -i | fgrep Configuration
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /etc/php.ini
$

or

$ php --ini
Configuration File (php.ini) Path: /etc
Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: /etc/php.d
Additional .ini files parsed:      /etc/php.d/apc.ini,
/etc/php.d/dbase.ini,
/etc/php.d/dom.ini,
/etc/php.d/gd.ini,
/etc/php.d/imagick.ini,
/etc/php.d/json.ini,
/etc/php.d/mbstring.ini,
/etc/php.d/memcache.ini,
/etc/php.d/mysql.ini,
/etc/php.d/mysqli.ini,
/etc/php.d/pdo.ini,
/etc/php.d/pdo_mysql.ini,
/etc/php.d/pdo_pgsql.ini,
/etc/php.d/pdo_sqlite.ini,
/etc/php.d/pgsql.ini,
/etc/php.d/tidy.ini,
/etc/php.d/xdebug.ini,
/etc/php.d/xmlreader.ini,
/etc/php.d/xmlwriter.ini,
/etc/php.d/xsl.ini,
/etc/php.d/zip.ini
Frank Farmer
i've deleted the semicolon ";" on the line : extension=php_curl.dll,i can clearly say that curl extension did enabled in php.ini.
tunpishuang
Frank Farmer,how do i know which php.ini that php -i command was used?
tunpishuang
Added a note re: determining the php.ini file in use to my answer.
Frank Farmer
+1  A: 

hey guys, i've found the answer to the question. just because php -i and phpinfo() in the web script using different php.ini files.

i seriously take a look at the different output of php -i and phpinfo().

here comes the difference:

phpinfo() :

Server API Apache 2.0 Handler

Configuration File (php.ini) Path C:\Windows

Loaded Configuration File D:\phpnow\php-5.2.10-Win32\php-apache2handler.ini

php -i

Server API => Command Line Interface

Configuration File (php.ini) Path => C:\Windows

Loaded Configuration File => (none)

the key problem is php -i load nothing but expecting a php.ini.

tunpishuang
`php -c D:\phpnow\php-5.2.10-Win32\php-apache2handler.ini get.php` will probably work, then. And/or you can probably `cp D:\phpnow\php-5.2.10-Win32\php-apache2handler.ini C:\Windows\php.ini`. I wouldn't really consider either of those production-grade solutions, but it should at least get you started.
Frank Farmer
i just did these work before ur suggestion and it works.thx.
tunpishuang
On Linux, instead of copy, you can create an symlink to the main php.ini on the CLI directory.
SEQOY Development Team