RE: php-fpm doesn't run php-curl curl_exec() succesfully

Jānis Elmeris <[email protected]>
Newsgroups gmane.comp.php.general
Message-ID <AS4PR01MB9936BC92FF17049F9DD90186EC6B2@AS4PR01MB9936.eurprd01.prod.exchangelabs.com>
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 <[email protected]>
Nosūtīts: svētdiena, 2024. gada 7. janvāris 05:46
Kam: [email protected] <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.