5

I am currently working on a project which makes an ajax call to my API on the same server. All went good and fine till a few hours ago when cURL suddenly stopped working without any reason and it's giving me following error

Protocol https not supported or disabled in libcurl

But it doesn't make any sense because it worked earlier and the only thing I changed was something in the login-screen (only PHP) and the timezone on the server from default to "Europe/Berlin" with

dpkg-reconfigure tzdata
[php.ini] date.timezone = "Europe/Berlin";

I've already restarted apache2, rebooted the server, checked the php.ini file and my phpinfo, it's all the same without any errors or changes, but curl just won't work. I tried to curl other hosts & localhost but nothing works. I also checked /var/mail/root, there aren't any errors.

Few information to my server

  • Debian 8
  • SSL certificate by Symantec
  • full-root-access without any restrictions

That's the cURL-Code I am using

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->url);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_POST, count($params));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

    session_write_close();
    $result = curl_exec($ch);
    curl_close($ch);
    session_start();

Like I said it worked fine and without any problems for longer than 1 month. That's the weirdest thing I've ever seen. I hope somebody can help me. If not, are there any good alternatives to curl? (POST-Requests)

Thanks in advance!

iMostLiked
  • 107
  • 2
  • 10
  • Possible duplicate of http://stackoverflow.com/questions/9595820/protocol-https-not-supported-or-disabled-in-libcurl – k0pernikus Mar 15 '17 at 16:21
  • 1
    Esp. take a look at http://stackoverflow.com/a/28220576/457268 Maybe the way your URL was build changed. – k0pernikus Mar 15 '17 at 16:22
  • `Protocol https not supported or disabled in libcurl` it means the `libcurl` was installed without `https` supporting. Please refer to this post http://stackoverflow.com/questions/19015282/how-do-i-enable-https-support-in-libcurl – Ahmad Mar 15 '17 at 16:23
  • 2
    It worked earlier.. so I don't get why it shouldn't work now? My URL did not changed. I didn't either changed libcurl. – iMostLiked Mar 15 '17 at 16:24
  • What you expect to return back? – Ivijan Stefan Stipić Mar 15 '17 at 16:24
  • `without any reason` that your reason `https not supported or disabled in libcurl` – JustOnUnderMillions Mar 15 '17 at 16:25
  • @IvijanStefanStipić It depends on which function I call. Sometimes it's just "true", "done", a number etc.. – iMostLiked Mar 15 '17 at 16:28
  • 1
    @JustOnUnderMillions, did you read my post? It worked 4 hours ago, I changed the timezone and then it stopped working. – iMostLiked Mar 15 '17 at 16:28
  • Read this http://stackoverflow.com/a/7280074/4916265 and maybe show an example URL. This is very complex. And when something is not any more working then there is always a reason. That was my point. Also do you change the timezone back to check if it wll work again? – JustOnUnderMillions Mar 15 '17 at 16:32
  • @JustOnUnderMillions, yes I tried to change it back, but it didn't worked either. I tried several URLs, not only mine. Nothing works, even google.com or other don't work, but in the past (like 4 hours ago) they worked. – iMostLiked Mar 15 '17 at 16:37
  • Can you host a fresh virtual host with fresh apache and test. Problem is that there are so many places where something can be wrong. You have to locate the bug in a away. How about testing a CURL via command line. And on and on... – JustOnUnderMillions Mar 15 '17 at 16:39
  • With that, @iMostLiked, are you certain that your server has outside connectivity? Not sure if you're talking about a local server or a remote server (like a VPS). – Chris Forrence Mar 15 '17 at 16:39
  • I am on a VPS and my server has outside connectivity, I checked that. And like I said, it worked fine until I changed the timezone. Changing it back, doesn't fix that bug. – iMostLiked Mar 15 '17 at 16:41
  • are you tr restart whole server? – Ivijan Stefan Stipić Mar 15 '17 at 19:10
  • @IvijanStefanStipić, I finally solved it. Check my answer on this question. – iMostLiked Mar 16 '17 at 11:22
  • @iMostLiked Thanks! Interesting problem realy. – Ivijan Stefan Stipić Mar 16 '17 at 14:03

3 Answers3

2

One thing what can made a problem is

curl_setopt($ch, CURLOPT_POST, count($params));

Try to define like this:

curl_setopt($ch, CURLOPT_POST, true);

Here is one setup what works great for me:

$default = array(
            CURLOPT_POSTFIELDS      => http_build_query($data),
            CURLOPT_POST            => true,
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_CONNECTTIMEOUT  => 10,
            CURLOPT_TIMEOUT         => 10,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_HTTPHEADER      => array('Accept: application/json')
        );

        $ch = curl_init($url);
            curl_setopt_array($ch, $options);
            $output=curl_exec($ch);     
        curl_close($ch);

But note this: CURLOPT_HTTPHEADER is in this example setup to accept JSON.

Also http_build_query can fix some parse problems inside your post data. Is not important to use but can help.

Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78
  • Unfortunately this doesn't work either. It's still returning me `POST APIHandle.php 500 (Internal Server Error)` – iMostLiked Mar 15 '17 at 16:32
  • You have problem in file you visit via CURL not inside CURL. Look inside error.log – Ivijan Stefan Stipić Mar 15 '17 at 16:33
  • 1
    To follow up on why `curl_setopt($ch, CURLOPT_POST, count($params))` can be a problem, that option refers to a boolean field, not a numeric one. – Chris Forrence Mar 15 '17 at 16:34
  • It doesn't matter which file it is. If I try to get another file it returns `Protocol https not supported or disabled in libcurl`. – iMostLiked Mar 15 '17 at 16:35
  • @iMostLiked Try with this `sudo apt-get install curl` :) – Ivijan Stefan Stipić Mar 15 '17 at 16:37
  • Try rreinstall curl, maby you have some issue in server configuration, that also happen. – Ivijan Stefan Stipić Mar 15 '17 at 16:38
  • @IvijanStefanStipić, I think you are not going to believe me, but I tried thousand ways, including this, but it still doesn't work. – iMostLiked Mar 15 '17 at 16:39
  • Error `POST APIHandle.php 500 (Internal Server Error)` is internal error inside `APIHandle.php`, `Protocol https not supported or disabled in libcurl` is server configuration problem. Look inside apache, look all services is running or not. Also look inside `php.ini` is `curl` enabled – Ivijan Stefan Stipić Mar 15 '17 at 16:41
  • It's not a problem with my APIHandle.php, because when I try to curl something without my API (only test.php), it shows me `Protocol https not supported or disabled in libcurl`, so it has to be a problem with curl. When I try to curl over my API, it returns an internal server error, which is normal, because curl can't curl. Curl is enabled, phpinfo and php.ini are saying so. – iMostLiked Mar 15 '17 at 16:46
2

Thanks to all for your help but I found the error. The problem was the following:

curl didn't worked with https protocol, because there was a broken link/file in the libraries folder. If curl isn't working for you, check with curl --version if you get the following error:

curl: /usr/local/lib/libcurl.so.4: no version information available (required by curl)

If yes, go to /usr/local/lib and delete both libcurl links/files. This solved the problem for me. After that just restart apache2 and it should work again.

Note that this only solved the problem for me and it's not a global solving problem. So be aware of what you delete on your server. Always make a backup!

iMostLiked
  • 107
  • 2
  • 10
1

I found out that for some reason my server updated from PHP 7.3 to PHP 7.4. I actually downgraded to 7.3 because whoever compiled that copy of 7.4 botched compiling it to the point where anyone in their right mind would be less embarrassed to go on national TV without wearing pants. No server reboot necessary.

I caught the issue because I implemented detection via function_exists() for some important PHP functions after I encountered that absolutely abysmal copy of PHP.

So make sure if you're running multiple PHP versions that your server did not decide to blindly update to a newer and inferior (compiled) version.

John
  • 1
  • 13
  • 98
  • 177