3

I'm trying to get the content of a site through cURL, it work just fine on terminal, however it doesn't work with PHP, below is my code.

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://example.com/",    
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "referer: https://www.example.com/something",
    "user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Firefox/52.0"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

I have tried the answer on this question, but it didn't work for me, I got the same error..

I have also tried including the settings below in my code, it didn't work either.

  CURLOPT_SSL_VERIFYHOST => false,
  CURLOPT_SSL_VERIFYPEER => false,

I'm using Nginx and Laravel, my PHP version is 7.1.2, my cURL version is 7.51.0 and I'm on macOS Sierra 10.12.3.

How can I get it working?

Community
  • 1
  • 1
Vinny
  • 597
  • 3
  • 11
  • 26
  • Have you tried just `$response = file_get_contents("https://example.com")` – miken32 Mar 22 '17 at 23:29
  • I want to use curl in this case, because after getting the content I must do a POST request. – Vinny Mar 22 '17 at 23:30
  • 3
    ***Error -9806*** is an Apple SecureTransport error, not an OpenSSL error. cURL was built against Apple libraries, not OpenSSL libraries. – jww Mar 23 '17 at 15:04

1 Answers1

6

Reinstall curl with OpenSSL support. If you're using Homebrew:

brew remove curl
brew install openssl
brew install --with-openssl curl
redent84
  • 18,901
  • 4
  • 62
  • 85
  • This yields `Error: invalid option: --with-openssl` – Julian Jul 03 '19 at 22:40
  • 2
    The Homebrew team has recently removed all install options for the cURL formula, which means you will not be able to do `brew install curl --with-openssl` now. Instead, do `brew install curl-openssl`. Make sure to uninstall the old one with `brew uninstall curl` first. – Birkhoff Lee Aug 09 '19 at 15:26