Question

Curl install in cent os but not working

Posted March 24, 2021 734 views
ApachePHPFirewallCentOS 8Laravel

Hello,

I have install CURL in cent OS.

I can see in my PHPINFO.

I also install LARAVEL.

But when i call curl return 0.

i have also setup firewall and allowed 80 and 443 port

 $html_brand = "www.google.com";
                $ch = curl_init();

                $options = array(
                    CURLOPT_URL            => $html_brand,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_HEADER         => true,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_ENCODING       => "",
                    CURLOPT_AUTOREFERER    => true,
                    CURLOPT_CONNECTTIMEOUT => 120,
                    CURLOPT_TIMEOUT        => 120,
                    CURLOPT_MAXREDIRS      => 10,
                );
                curl_setopt_array( $ch, $options );
                $response = curl_exec($ch); 
                $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

                if ( $httpCode != 200 ){
                    echo "Return code is {$httpCode} \n"
                        .curl_error($ch);
                } else {
                    echo "<pre>".htmlspecialchars($response)."</pre>";
                }

It’s “Return code is 0”;

how i resolved this issue.

I am sure issue from server.

any comments guys ?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
2 answers

@sharmahemal811

Hi, thanks for posting this question here, you can get your code running by assigning false to this parameter

Hope this helps, do not hesitate to contact me of you need more help.

Hi @sharmahemal811,

I tested your script and I can confirm I got a HttpCode 200.

Now, you were wondering why was executing

curl www.google.com

From the command line working but when you put it in a script it didn’t work. The curl you are using from the command line is actually very different than the one you are using in php scripts.

Basically, in your command line, you are using curl and in your php scripts, you are using an extension for php called php-curl. While the command-line curl comes installed by default, that is not the case for php-curl. As such all you need to do is install php-curl.

Depending on the php version installed (7, 7.2, 7.3, 7.4), you need to run the following command:

yum install php-curl

or

yum install php7.2-curl

Now, if you are using another php version just change the number 7.2 as you like.

Regards,
KFSys