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>";
}
?>