Re: php-fpm doesn't run php-curl curl_exec() succesfully
| From: | kurt thepw . com | Date: | Fri, 12 Jan 2024 20:58:08 +0000 |
| Subject: | Re: php-fpm doesn't run php-curl curl_exec() succesfully | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-328355@lists.php.net to get a copy of this message | ||
Hello, and thanks for the reply!
I did use the curl_error() function, adding it to the program below via:
==========
if( ($result = curl_exec($handle)) === false ) {
echo "curl_exec fail " . curl_error($handle) . "\n";
$status = '';
} else {
. . . . . .
}
==========
When testing it via my webserver, I did see the "curl_exec_fail.." error message, but
sadly the error message was null. And again, the only entry I saw in /var/log/php-fpm/www-error.log
was the same:
PHP Warning: Trying to access array offset on value of type null in /var/www/html/test.php on line
25
I did eventually stumble across the cause of my problem when I tried replacing all the php_curl()
calls with file_get_contents( "https://$user:$pass$$url" );. This failed too, but it
gave me the www-error.log entry:
PHP Warning: file_get_contents(https://...@<my url>): Failed to open stream: Permission
denied
This pointed me in the direction I should've been looking to begin with: selinux! It develops
that the default selinux policies for httpd frown upon web documents opening any sort of file,
including connections to other websites. To get the PHP script to work with selinux enabled I had to
toggle two selinux booleans:
==========
[root@arch php-fpm]# setsebool -P httpd_can_network_connect 1
[root@arch php-fpm]# setsebool -P httpd_unified 1
==========
And then restart httpd.
Then the web version of my PHP script's curl_exec call was able to retrieve the same data as
when run from the command line.
I am not about to try to delve into the internals of the PHP language any time soon, but it is too
bad that curl_error() was unable to capture and provide any error message when the curl_exec() call
failed, and that the php-fpm process logged no error when curl_exec() failed.
In any case, your suggestion to try curl_error() led me to verify that curl_exec() was returning
false, and that eventually led me to selinux. Thanks again!
Yours,
Kurt Reimer
________________________________
From: Jānis Elmeris <janis.elmeris@lv.lv>
Sent: Monday, January 8, 2024 3:16 AM
To: kurt thepw.com <kurt@thepw.com>; php-general@lists.php.net
<php-general@lists.php.net>
Subject: RE: php-fpm doesn't run php-curl curl_exec() succesfully
Hello!
Use "curl_error" to debug CURL-related errors: https://www.php.net/manual/en/function.curl-error.php
Best regards,
Janis
________________________________
No: kurt thepw.com <kurt@thepw.com>
Nosūtīts: svētdiena, 2024. gada 7. janvāris 05:46
Kam: php-general@lists.php.net <php-general@lists.php.net>
Tēma: php-fpm doesn't run php-curl curl_exec() succesfully
Hello,
This php script (with the real URL & credentials put back in) runs successfully from the
command-line. It uses php_curl to retrieve JSON-formatted data, converts the result to an
associative array, and displays one of the entries and the length of the retrieved JSON data:
==========
#!/usr/bin/php
<?php
$url = "https://host.ext";
$user = "<user>";
$pass = "<pw>";
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $user.":".$pass);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
//curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$result = curl_exec($curl);
$resultlen = strlen($result);
curl_close($curl);
$r_ary = array();
$r_ary = json_decode( $result , true , 512 , JSON_OBJECT_AS_ARRAY );
$st = 'status';
$status = $r_ary[$st];
echo "<!doctype html>";
echo "<html>";
echo "<body>";
echo "Length: $resultlen";
echo "Status: $status";
echo "</body>";
echo "</html>";
?>
==========
But if I remove the first line, put the result in the same computer's HTML document root and
try to access via a web browser, it retrieves zero bytes.. It seems the curl_exec() doesn't
retrieve anything. I get the following in /var/log/php-fpm/www-error.log:
PHP Warning: Trying to access array offset on value of type null in /var/www/html/test.php on line
25
I'm running Centos stream 9, with apache2.4 configured for php-fpm. The command-line PHP is
8.0.30. It seems like php-fpm has different behavior with the various curl_xxxx() functions, even
partial broken-ness, when compared to commandline PHP
Any help would be much appreciated.
Yours,
Kurt Reimer
Use