ideal-postcodes / postcodes.io

UK postcode & geolocation API, serving up open data
https://postcodes.io
MIT License
1.24k stars 249 forks source link

Connection issues #1182

Closed dogslife closed 9 months ago

dogslife commented 10 months ago

great service via browser but when trying to access via PHP I'm getting SSL issues when trying to connect

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

*   Trying 104.26.12.209:443...
* Connected to api.postcodes.io (104.26.12.209) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL certificate problem: unable to get local issuer certificate
* Closing connection 0
* Hostname api.postcodes.io was found in DNS cache
*   Trying 104.26.12.209:443...
* Connected to api.postcodes.io (104.26.12.209) port 443 (#1)
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL certificate problem: unable to get local issuer certificate
* Closing connection 1
* Hostname api.postcodes.io was found in DNS cache
*   Trying 104.26.12.209:443...
* Connected to api.postcodes.io (104.26.12.209) port 443 (#2)
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL certificate problem: unable to get local issuer certificate
* Closing connection 2

Is it me?

p-glomski commented 10 months ago

HI @dogslife, Could you provide full curl command you're using to make request to api.postcodes.io? I am not having myself any SSL problems while using curl.

dogslife commented 10 months ago

was asked by p-glomski 05/01/2024, 15:17 to provide details of CURL via email which I did, twenty minutes later. Now I see the issue has been closed with no explanation. May I ask why? May I ask for some clarification?

p-glomski commented 10 months ago

was asked by p-glomski 05/01/2024, 15:17 to provide details of CURL via email which I did, twenty minutes later. Now I see the issue has been closed with no explanation. May I ask why? May I ask for some clarification?

Hi,

was a mistake, we didn't mean to close the issue, could you please tell me where have you sent the details? I can't see any email regarding this issue.

dogslife commented 10 months ago

In reply email will post here now My original private function which returned the posted error

private function getlatlng(string $pc){
            $curl = curl_init();
            curl_setopt($curl,CURLOPT_VERBOSE,true);
            $streamVerboseHandle = fopen('php://temp', 'w+');
            curl_setopt($curl,CURLOPT_STDERR,$streamVerboseHandle);
            curl_setopt($curl,CURLOPT_URL,$url);
            curl_setopt($curl,CURLOPT_HEADER,1);
            curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
            curl_setopt($curl,CURLOPT_FAILONERROR,true);

            error_log("RAW: ".curl_exec($curl));
            error_log("JSON: ".json_decode(curl_exec($curl)));
            $response = curl_exec($curl);
            if    ($response===FALSE){
                printf("cUrl error (#%d): %s<br>\n",
                       curl_errno($curl),
                       htmlspecialchars(curl_error($curl)));
                }
            rewind($streamVerboseHandle);
            $verboseLog = stream_get_contents($streamVerboseHandle);
            echo "cUrl verbose information:\n",
                 "<pre>", htmlspecialchars($verboseLog), "</pre>\n";

            error_log("GEOCODE response: ".(!$response||empty($reult)?"EMPTY":$response),0);
            $headerOffset = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
            $header = substr($response,0,$headerOffset);
            echo json_encode($header);
            curl_close($curl);
            return $response;
            }

Also tried class from Ryan Hart 2016 on GitHub and added error logging

public function lookup($postcode){
                $jsonurl = ["https://api.postcodes.io/postcodes/"](https://api.postcodes.io/postcodes/).$postcode;
                $json = $this->request($jsonurl);
                $decoded = json_decode($json);
                error_log($decoded);
                if    ($decoded->status==200){
                    return $decoded->result;
                    }
                else{
                    return false;
                    }
                return false;
                }

public function request($jsonurl){
            error_log("ENTERED REQUEST WITH JSONURL: ".print_r($jsonurl,true),0);
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_VERBOSE,true);
                $streamVerboseHandle = fopen('php://temp', 'w+');
                curl_setopt($ch, CURLOPT_STDERR,$streamVerboseHandle);
                curl_setopt($ch, CURLOPT_URL, str_replace(' ','%20',$jsonurl));
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
                curl_setopt($ch, CURLOPT_HTTPHEADER,[
                  'Content-Type: application/json',
                ]);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
                curl_setopt($ch, CURLOPT_FAILONERROR,true);
                $response = curl_exec($ch);

                if    ($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",
                     "<pre>", htmlspecialchars($verboseLog), "</pre>\n";

                error_log("GEOCODE response: ".(!$response||empty($reult)?"EMPTY":$response),0);
                $headerOffset = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
                $header = substr($response,0,$headerOffset);
                echo json_encode($header);

                curl_close($ch);
                return $response;
                }

[RESPONSE]

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

*   Trying 104.26.13.209:443...
* Connected to api.postcodes.io (104.26.13.209) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL certificate problem: unable to get local issuer certificate
* Closing connection 0

Hope this helps. Really like your service and happy to credit/promote on our web app if we can resolve this.

mfilip commented 9 months ago

Can you send all class you have? I think you implementation is wrong or some php settings. In mean time can you try to run this code:



function get(){
    $curl = curl_init();
    curl_setopt($curl,CURLOPT_VERBOSE,true);
    curl_setopt($curl,CURLOPT_URL,"api.postcodes.io/postcodes/OX495NU");
    curl_setopt($curl,CURLOPT_HEADER,1);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl,CURLOPT_FAILONERROR,true);
    curl_setopt($curl,CURLOPT_SSLVERSION, 3);

    $response = curl_exec($curl);
    var_dump($response);
    if(curl_errno($curl)) {
        var_dump(curl_error($curl));
    }
    curl_close($curl);
};

get();

?>
mfilip commented 9 months ago

Can you also provide php version because I believe you have php misconfigured

dogslife commented 9 months ago

PHP v 7.4.13

dogslife commented 9 months ago

Just ran you code and it worked like a charm. Thank you @mfilip

mfilip commented 9 months ago

Can we mark as closed or you need something else with coding?