2

I have this simple php script:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://www.instagram.com/zuck/'); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$output = curl_exec($ch); 

curl_close($ch);

echo htmlspecialchars($output);

I have Apache 2.4.17, PHP : 5.6.16 (I also tried with PHP 7) I have tried running it on a remote host server and it works just as expected. However it doesn't work at all on my PC. I tried WAMP, XAMPP, disabled firewall, connected directly to my modem (without router), checked the php.ini and c_url is uncomented. I also tried downloading a fix from http://www.anindya.com/. Doesn't work as well. When i try curl_version it works (so i guess c_url is loaded) but this script doesn't. And the strange thing is there are no errors just a blank page. I really don't have any more troubleshooting ideas

Mark Titorenkov
  • 91
  • 2
  • 13
  • add `error_reporting(E_ALL);ini_set('display_errors',1);` just after ` – Alive to die - Anant Jun 17 '16 at 13:04
  • Did you check your apache error.log? – Jason K Jun 17 '16 at 13:06
  • check in php_info curl is enabled or not – Mehul Kuriya Jun 17 '16 at 13:12
  • I tried both of your answers and there is still nothing on the page and nothing related to curl in any log files. Only thing in php_error.log is multiple lines of something simillar to this `[17-Jun-2016 13:09:20 UTC] PHP Warning: unlink(D:/wamp/bin/apache/apache2.4.17/bin/icudt54.dll): Permission denied in D:\wamp\scripts\wampserver.lib.php on line 78` – Mark Titorenkov Jun 17 '16 at 13:14
  • I already checked in php_info() and it's enabled `cURL support enabled cURL Information 7.42.1` – Mark Titorenkov Jun 17 '16 at 13:16
  • I test the code block on a know working lamp stack. No errors. On your wamp stack I would make sure those errors have nothing to do with curl. I would put echo statements on every line to see where the script stops. – Jason K Jun 17 '16 at 13:32
  • Possible duplicate of [How to enable cURL in PHP / XAMPP](http://stackoverflow.com/questions/1347146/how-to-enable-curl-in-php-xampp) – Syed Waqas Bukhary Jun 17 '16 at 13:33
  • I think i found the problem but I still don't know how to fix it. I tried `print_r(curl_error($ch))` just before `print_r(curl_error($ch))` and it gives me "SSL certificate problem: unable to get local issuer certificate". What does this mean? – Mark Titorenkov Jun 17 '16 at 13:37
  • It's not advisable to disable SSL certificate validation. The better solution here would be to get cURL to validate the certificate using actual CA certificates installed on the system. – amphetamachine Jun 17 '16 at 13:55

2 Answers2

3

After some testing I found the problem:

First I checked for erros in the script itself with:

echo curl_error($ch)

which returned this:

SSL certificate problem: unable to get local issuer certificate

Turns out I had to disable SSL certificate verification because all the websites i had tried used SSL (eg instagram, google, etc.)

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false)

Mark Titorenkov
  • 91
  • 2
  • 13
1

This question is marked as answered but for those who see this in future, the comment of @amphetamachine is important. Setting CURLOPT_SSL_VERIFYPEER to false is not a good idea. It will work on your local server but do you really want this on the remote server?

Rather than having to remember to comment out this line for production (or make it conditional on the environment), I suggest you add to your php.ini file the absolute path to the Certificate Authority file (where you will have already uncommented the cURL extension).

curl.cainfo ="your absolute local path\cacert.pem"

This file can be downloaded if you don’t have it.

That way your local test system for will work and you will not compromise your production setup.

You could set the path in the cURL option CURLOPT_CAPATH but again you would not want this in your production code.

Geoff
  • 421
  • 4
  • 4