Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

PHP and cURL work fine in a non-webfacing directory in my amazon EC2 micro install, 12.04.2 LTS (GNU/Linux 3.2.0-40-virtual x86_64).

I'm able to run some scripts on the command line using cURL:

    #!/usr/bin/php
    $command = '/absolute/path/to/running/process';
    $url = 'http://example.com';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 400);
    curl_setopt($ch, CURLOPT_TIMEOUT, 400);

    $resp = json_decode(curl_exec($ch), 1);
    $data1 = round($resp["data1"], 6);
    $data2 = round ($resp["data2"], 2);

    curl_close($ch);


    $c = array(
            'var1' => trim(shell_exec($command)),
            'var2' => trim(shell_exec($command)),
            'var3' => trim(shell_exec($command)),
            'var4' => number_format($data1, 6, '.', ''),
            'var5' => number_format($data2, 0, '.', ',')
    );
    var_dump($c);

This works fine.

However, as soon as I put this same script in the web facing directory (I remove #!/usr/bin/php obviously), the page loads (I can type echo 'Hello World!' at the very top and see the text on a browser from a remote machine), but the page is entirely blank and adding echo statements after curl results in nothing. If I comment out the curl function calls and try to simply execute the command, it seems to not work (The entire array of values is populated with NULL or 0.).

  • What's going on with PHP? Why won't cURL run in the /var/www/ folder but runs fine everywhere else? I'm storing information to a database using cURL to get some JSON from public APIs.
  • Why can't I use shell_exec to run a script as I normally could using PHP on the command line?
  • Errors aren't showing up in PHP even though error_reporting(E_ALL) is set. I can't find a log of errors by using locate to find error_log or php.err, which I thought were the default names for the php error log.

I feel like these two issues may be related, but I can't figure out why, maybe permissions? Something I noticed was that I cannot create or modify files in this directory without using the sudo command, which isn't usual.

Another note: phpinfo() says nothing about cURL, but it works on the command line. I'm not sure why.

share|improve this question
add comment (requires an account with 50 reputation)

2 Answers

Ubuntu has separate php.ini files for command line PHP and Apache2 PHP. Your config in those files most likely has different extensions loaded (or different security settings), which makes Apache not have the same functionality as the CLI.

$ ls -l /etc/php5/cli/php.ini
-rw-r--r-- 1 root root 65485 Jan 27 09:20 /etc/php5/cli/php.ini

$ ls -l /etc/php5/apache2/php.ini
-rw-r--r-- 1 root root 65839 Oct 31  2012 /etc/php5/apache2/php.ini
share|improve this answer
I copied all of the settings from the cli/php.ini file and I can't seem to get any of these things to work. – Joey Jul 10 at 6:30
@Joey Does curl show up in phpinfo() on apache after your changes? – Joachim Isaksson Jul 10 at 6:41
No, it doesn't, which makes me wonder how it's working on the CLI – Joey Jul 10 at 14:20
add comment (requires an account with 50 reputation)

Just an observation, there are extra <'> single quotes in these lines:

        'var1' => trim(shell_exec($command')),
        'var2' => trim(shell_exec($command')),
        'var3' => trim(shell_exec($command')),

other than that, I agree with Joachim's suggestion.

share|improve this answer
Fixed, I changed a lot of info for the sake of privacy. – Joey Jul 10 at 6:31
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.