1

I have a php script by the following address C:\Denwer\home\localhost\www\upload_record_test.php. This script shows some text. But if I try to execute this script from the commmand line by command php C:\Denwer\home\localhost\www\upload_record_test, I get the html response with many errors and the message Call to undefined function curl_init(). The code:

<?php
    $_POST_DATA=array();
    $_POST_DATA['id']='AccountPagesView.a_book/45';
    $_POST_DATA['old_value']='1';
    $_POST_DATA['value']='2';
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/index.php/welcome/update_record');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST_DATA);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    $real_result=curl_exec($ch);
    $expected_result=array('result'=>'LOGIN', 'old_value'=>'1');
    $real_result=json_decode($real_result, true);
    if (count(array_intersect($real_result, $expected_result))==2)
    {
        echo "THE TEST HAS BEEN COMPLETED";
    }
    else 
    {
        echo "THE TEST HASN'T BEEN COMPLETED<br/>";
        echo "RESULT:";
        print_r($real_result);
        echo "<br/>EXPECTED RESULT:";
        print_r($expected_result);
    }
?>

Please, tell me, how can I execute this script from the command line and get only resulted text? Thanks in advance.

Vamsi Krishna B
  • 11,377
  • 15
  • 68
  • 94
user1477886
  • 263
  • 1
  • 6
  • 19
  • Are you able the run the script from browser? – Starx Jul 02 '12 at 06:26
  • possible duplicate of [Call to undefined function curl_init().?](http://stackoverflow.com/questions/6382539/call-to-undefined-function-curl-init) and [curl init function not working ?](http://www.stackoverflow.com/questions/4477535/curl-init-function-not-working). – Starx Jul 02 '12 at 06:27

1 Answers1

0

Check if curl is properly installed. If no check this instruction from php.net.

If Yes, then your curl module may not be enabled. You have to enable the curl module, to use curl methods. You can do this by editing php.ini and enabling the module.

On windows:

  • Open your php.ini file
  • Find a line with ;extension=php_curl.dll
  • Remove the ; infront and save the file
  • Restart apache server

It should work.

Update:

Sometimes safe_mode might be causing this problem. Go to ini file and disable the safe_mode

[php.ini]
safe_mode = Off
Community
  • 1
  • 1
Starx
  • 77,474
  • 47
  • 185
  • 261
  • Sorry, but this script works from my browser, and the cause of the error ins't a disabled curl extension. – user1477886 Jul 02 '12 at 06:28
  • Command line PHP uses a different php.ini, so there is a good chance that curl is disabled there. – Jevgenij Evll Jul 02 '12 at 06:30
  • As @JevgenijEvll said - try typing `php --ini` from your command line to see where your command line .ini file is. – John C Jul 02 '12 at 06:33
  • @user1477886, CLI should use a different php.ini file, try to find a `cli` folder. – Starx Jul 02 '12 at 06:43
  • @user1477886, Another way you can execute the file, is by giving the script name like `php "curlfile.php"`, if the ini's are same. This should work as expected. – Starx Jul 02 '12 at 06:44
  • @user1477886, May be safe_mode is causing this. See the update – Starx Jul 02 '12 at 06:51
  • @user1477886, I am voting this question to catch some attention, then. – Starx Jul 02 '12 at 07:02
  • @user1477886, And once try to dry run the command in CLI form. Let's Say `php -r "curl_init();"` – Starx Jul 02 '12 at 07:03
  • I have got the error "Unable to load dynamic_library php_curl.dll" – user1477886 Jul 02 '12 at 07:13
  • One last thing from me. copy the php_curl.dll from the `ext` folder to the folder where `php_win.exe` is present. And try – Starx Jul 02 '12 at 07:18
  • Sorry, but it hasn't halped me. It doesn't work. I have just seen at results of "phpinfo()" function andd curl is enabled. – user1477886 Jul 02 '12 at 07:25