'php curl does not seem to send the intended POST variables

I am trying to retrieve a post from a simple POST to api.openweathermap.org.

The URL post should like like: api.openweathermap.org/data/2.5/weather?id=4215110&units=metric&appid=my_api_key

The php code I'm using is:

#!/usr/bin/php
  
<?php

define('WEATHER_CITY_CODE', 4215110) ;    // Peachtree City, GA
define('WEATHER_UNITS', 'imperial') ;     // or 'metric' or 'standard'
define('WEATHER_API_KEY', 'my_api_key') ; // Not the real key

$request = "api.openweathermap.org/data/2.5/weather" ;
$requestVariables = Array('id' => WEATHER_CITY_CODE
                        , 'units' => WEATHER_UNITS
                        , 'appid' => WEATHER_API_KEY
                         ) ;
printf("Request query parameters should be: %s?%s\n", $request, http_build_query($requestVariables)) ;
$streamVerboseHandle = fopen('php://temp', 'w+') ;
$ch = curl_init($request) ;
curl_setopt($ch, CURLOPT_STDERR, $streamVerboseHandle) ;
curl_setopt($ch, CURLOPT_VERBOSE, true) ;
curl_setopt($ch, CURLOPT_POST, 1) ;
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( $requestVariables )) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;

$weather_response = curl_exec($ch) ;
//if ($weather_response === FALSE)
    {
    printf("cUrl error (#%d): %s<br>\n",
           curl_errno($ch),
           htmlspecialchars(curl_error($ch)))
           ;
    rewind($streamVerboseHandle);
    $verboseLog = stream_get_contents($streamVerboseHandle);
    echo "cUrl verbose information:\n",
         htmlspecialchars($verboseLog), "\n";
    }
curl_close($ch) ;

try {
    $response_arr = json_decode($weather_response, true, 3, JSON_THROW_ON_ERROR) ;
    print_r($response_arr) ;
    }

catch (Exception $e) {
    $response_file = "weather-response.json" ;
    file_put_contents($response_file, $weather_response) ;
    $lov->writeLogLine("Exception from send-sms.php: " . $e->getMessage()) ;
    $lov->writeLogLine("Response text is: '" . $weather_response . "'"
                     , false, null, false, false) ;
    $lov->writeLogLine("Response saved in " . $response_file) ;
    throw new Exception("Failure in send-sms.php") ;
    }


?>

When I run this code (even if the WEATHER_API_KEY value is set correctly, I get the following response:

Array
(
    [cod] => 401
    [message] => Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.
)

Even if the correct API key is used. (Note: I can copy the value in the "Request query paraemters..." line and paste them into a browser to get the correct results without error.

So I added the curl debug code you see above, and it seems to indicate that curl is not sending my parameters. (see the POST /data/2.5/weather HTTP/1.1 below.) I'm not sure why. While I'm not accustomed to using (nor needing) that curl debug information, it seems to me that I should see /data/2.5/weather?id=4... in its place.

I saw this article, but I don't think it applies here...

Can you help?

Process output is:

> ./getweathermap.php 

Request query parameters should be: api.openweathermap.org/data/2.5/weather?id=4215110&units=imperial&appid=my_api_key
cUrl error (#0): <br>
cUrl verbose information:
*   Trying 192.241.245.161:80...
* TCP_NODELAY set
* Connected to api.openweathermap.org (192.241.245.161) port 80 (#0)
&gt; POST /data/2.5/weather HTTP/1.1
Host: api.openweathermap.org
Accept: */*
Content-Length: 42
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 42 out of 42 bytes
* Mark bundle as not supporting multiuse
&lt; HTTP/1.1 401 Unauthorized
&lt; Server: openresty
&lt; Date: Sun, 27 Feb 2022 01:45:01 GMT
&lt; Content-Type: application/json; charset=utf-8
&lt; Content-Length: 107
&lt; Connection: keep-alive
&lt; X-Cache-Key: /data/2.5/weather?
&lt; Access-Control-Allow-Origin: *
&lt; Access-Control-Allow-Credentials: true
&lt; Access-Control-Allow-Methods: GET, POST
&lt; 
* Connection #0 to host api.openweathermap.org left intact

Array
(
    [cod] => 401
    [message] => Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.
)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source