Report this

What is the reason for this report?

Curl install in cent os but not working

Posted on March 24, 2021

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 ?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

@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

I am also facing this issue, please let us know if there is any solution. Server - Getting this issue on Cetos server with php version 8.0. If anyone find the resolution for this than please be kind to share info.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.