0

I have heard it said (and agree) that often the hardest part about programming is the initial set-up to get all the components of the stack installed, connected, and in working order (each with its own bugs to work out) on your local development environment;

The issue here now is how to enable cURL locally on my Windows 10 so that my PHP7 localhost (with or without Apache 2.4 Server) can cURL a third-party API?

The documentation is not so clear as I read that a library libcurl.dll is required, and this is only available from https://curl.se. So I downloaded this, and cURL works on my windows when I type the following command in the Windows PS in the directory with the curl.exe file:

./curl "https://bio.torre.co/api/bios/jerusalemprogrammer"

So cURL works fine locally from my CLI Windows PS, i.e. I am able to query an API and get data back when I cURL in the directory with curl.exe.

I have PHP7+ installed locally and up until this point with cURL was always able to develop locally with local SQL and NoSQL databases with only the PHP7 local development server only (i.e. without Apache Server), and found various PHP scripts that at first said cURL was not installed on my PHP7 Development Localhost Server, but found that this was a common issue, and one of the solutions involved tweaking some files with a separate Apache 2.4 server, so I downloaded, installed, tested, and all works fine on Apache as a server for PHP.

Here is an example of this issue with cURL with Windows 10, PHP 7, Apache 2.4: Enable cURL on PHP7 windows10 64 bit Apache 2.4

I then was able to confirm that I have cURL now installed locally and working, e.g.:

cURL is available on your web server Array ( [0] => Core [1] => bcmath [2] => calendar [3] => ctype [4] => date [5] => filter [6] => hash [7] => iconv [8] => json [9] => SPL [10] => pcre [11] => readline [12] => Reflection [13] => session [14] => standard [15] => mysqlnd [16] => tokenizer [17] => zip [18] => zlib [19] => libxml [20] => dom [21] => PDO [22] => openssl [23] => SimpleXML [24] => xml [25] => xmlreader [26] => xmlwriter [27] => apache2handler [28] => curl [29] => Phar )

However, despite the messages confirming that cURL is installed and working on my local Windows 10 system, and despite the messages on both Apache Server 2.4 and PHP Local Development Server confirming that cURL is installed and working, I am not able to successfully cURL back data.

The following PHP code (below) works perfectly on my web server, but does not work on my localhost. On the web server, data is returned. On the local server, null value is returned,

I have spent now about the last 5 days about 6-8 hours per day on this issue; Does anyone know why it says that cURL is working locally for me on both Windows and 2 localhost servers, yet it does not work locally to cURL data back from within a running PHP/APACHE localhost server, but works fine when this same code is on a remote server LINUX/APACHE web site host?

Since I read in the PHP documentation that the library libcurl.dll is required, do I need to include this somewhere? Currently I have the cURL folder within the root C:\php root directory, and the environment value includes C:\php so any localhost development server theoretically already has access to this C:\php\curl780\bin folder with the files curl.exe and libcurl.dll

What else - and where else - do I need to configure to get this working so that I can continue developing and fleshing out this project?

<?php

    /* WITH MORE TIME, REPLACE THE IF / ELSE CONDITION WITH ANOTHER PRACTICAL CONDITION */
    if (0 != 1) {

        /* TEST PRINT OUTPUT */
        echo "<span style=\"color:green\">Hello World</span>";

        /* DECLARE VARIABLES:  URL STRING WITH API ENDPOINT TO CALL AND QUERY */
        $url = 'https://bio.torre.co/api/bios/jerusalemprogrammer';

        /* CURL INITIATE */
        $curl = curl_init();
    
        /* CURL SET OPTIONS */
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);

        /* CURL EXECUTE */  
        $data = curl_exec($curl);

        /* CONVERT DATA STRING TO JSON OBJECT */
        $JSONObject = json_decode($data, true);

        /* IF JSON OBJECT DOES NOT EXIST... */
        if(!$JSONObject) {
            echo "No data found";
        }

        /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("URL Endpoint: " . $url); /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("URL Endpoint Type: " . gettype($url)); /* TEST PRINT OUTPUT */
 
        /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("Curl: ". $curl); /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("Curl Type: ". gettype($curl)); /* TEST PRINT OUTPUT */
 
        /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("Data Type: " . gettype($data)); /* TEST PRINT OUTPUT */
        // echo($data); /* TEST PRINT OUTPUT */
        
        /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("JSONObject Type: " . gettype($JSONObject)); /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("JSONObject: ". $JSONObject); /* TEST PRINT OUTPUT */

        /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("var_dump :");
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        // var_dump($JSONObject);
        // echo("<pre>" . var_export($JSONObject, true) . "</pre>");
        
        /* TEST PRINT OUTPUT */
        echo("print_r :");
        echo("<br />"); /* TEST PRINT OUTPUT */
        echo("<br />"); /* TEST PRINT OUTPUT */
        // print_r("<pre>" . $JSONObject . "</pre>");
        // echo("<pre>".print_r($JSONObject,true)."</pre>");
 
        /* CURL CLOSE */
        curl_close($curl);

    /* WITH MORE TIME, REPLACE THE IF / ELSE CONDITION WITH ANOTHER PRACTICAL CONDITION */
    } else {
        
        echo "<span style=\"color:green\">Goodbye Cruel World; I'm leaving you today; Goodbye, goodbye, goodbye...</span>";
    } 

?>
273K
  • 29,503
  • 10
  • 41
  • 64
  • 1
    Unless you get any error saying that cURL is not available or similar, it probably works. Have you tried [debugging the cURL request](https://stackoverflow.com/questions/3757071/php-debugging-curl) to see what actually happens? – M. Eriksson Feb 14 '22 at 10:16
  • What have you tried to resolve the problem? What do you mean by "null value is returned"? – Nico Haase Feb 14 '22 at 10:19
  • Also, writing cURL code by hand might be problematic. Why not use any library like Guzzle or Symfony's HttpClient to make your life easier? – Nico Haase Feb 14 '22 at 10:20
  • Thanks @M.Eriksson! Your link led me down a path to find a solution! I will post the answer. – Jerusalem Programmer Feb 14 '22 at 11:03

1 Answers1

0

Thanks to @M.Eriksson (in comments below), I was led to jump down the right rabbit hole to find a solution with info in the link he provided, specifically:

curl_setopt($curlHandle, CURLOPT_VERBOSE, true);

Here was the output of debugging cURL:

cUrl error (#60): SSL certificate problem: unable to get local issuer certificate

This led me to find a reference in an unnoticed answer in the following link: PHP 7 curl not being loaded

And the solution to the problem was to download the cacert.pem and update the php.ini file with the following lines of code, and then restart the Apache server:

curl.cainfo="C:\php\cacert.pem" 
openssl.cafile="C:\php\cacert.pem"